How to create packages in Java?

Solution:

If you are using the programming IDEs like Eclipse, NetBeans, MyEclipse, etc. click on file->new->project and eclipse will ask you to enter the name of the package. It will create the project package containing various directories such as src, etc.
If you are using an editor like notepad for java programming, use the following steps to create the package.

  • Define a packagepackage_name . Create the class with the nameclass_name and save this file withyour_class_name.java .
  • Now compile the file by running the following command on the terminal.

  1. javac -d . your_class_name.java

The above command creates the package with the name package_name in the present working directory.

  • Now, run the class file by using the absolute class file name, like following.

  1. java package_name.class_name
0 answers