Create User Defined Packages Java


Java is a friendly language and permits to create our own packages and use in programming. We know earlier, Java allows us to create our exceptions. Creating packages are indispensable in project development where number of developers are involved doing different modules and tasks. We know packages avoid name collision problems. Package naming conventions are very helpful to locate the applications developed by a single individual or team.

Now go on reading User Defined Packages Java.
Steps of creating User Defined Packages Java and using them.
  1. Create a package with a .class file
  2. set the classpath from the directory from which you would like to access. It may be in a different drive and directory. Let us call it as a target directory.
  3. Write a program and use the file from the package.

Let us create a package called forest and place a class called Tiger in it. Access the package from a different drive and directory.

1st Step: Create a package (forest) and place Tiger.class in it.

Let us assume C:\snr is the current directory where we would like to create the package.

C:\snr > notepad Tiger.java

package forest;
import java.util.*; 
public class Tiger
{
  public void getDetails(String nickName, int weight)
  {
    System.out.println("Tiger nick name is " + nickName); 
    System.out.println("Tiger weight is " + weight);
  }
}

Order of Package Statement

The above program codingwise is very simple but is important to know the steps of package creation.

package forest;
import java.util.*;
public class Tiger

package is a keyword of Java followed by the package name. Just writing the package statement followed by the name creates a new package; see how simple Java is to practice. For this reason, Java is known as a production language.

While create User Defined Packages Java, the order of statements is very important. The order must be like this, else, compilation error.

  1. Package statement
  2. Import statement
  3. Class declaration

If exists, the package statement must be first one in the program. If exists, the import statement must be the second one. Our class declaration is the third. Any order changes, it is a compilation error.

When the code is ready, the next job is compilation. We must compile with package notation. Package notation uses –d compiler option as follows.

C:\snr > javac -d . Tiger.java

The –d compiler option creates a new folder called forest and places the Tiger.class in it. The dot (.) is an operating system's environment variable that indicates the current directory. It is an instruction to the OS to create a directory called forest and place the Tiger.class in it.

Using User Defined Packages Java

After creating the package let us use it.

2nd step: Set the classpath from the target directory.

Let us assume D:\sumathi is the target directory. Let us access Tiger.class in forest package from here.

From the target directory set the classpath following way.

D:\sumathi> set classpath=C:\snr;%classpath%;

classpath is another environment variable which gives the address of the forest directory to the OS. %classpath% informs the OS to append the already existing classpath to the current classpath that is right now set.

3rd Step: Now finally, write a program from the target directory D:/sumathi and access the package.

D:\sumathi> notepad Animal.java

The above statement creates a file called Animal.java and write the code in it, say, as follows.

import forest.Tiger;
public class Animal
{
  public static void main(String args[])
  {
    Tiger t1 = new Tiger ();
    t1.getDetails("Everest", 50);
  }
}

The compilation and execution is as usual as follows.

D:\sumathi> javac Animal.java
D:\sumathi> java Animal

Following is the output screen.


Importing the Package – Precautions

The following statement raises compilation error.

import forest.*;

To work with the above statement, remove the source file Tiger.java file from C:\snr directory; else use as import forest.Tiger.

16 thoughts on “Create User Defined Packages Java”

  1. well..i searched on youtube too..but t was boring..but here in, you ha explained very nice n deep enough..not very deep..which was very good jut understand..thank you sir..

  2. sir,what is the reason for deleting .java file from C:snr when we try to acceess with ‘ * ‘ operator?assuming there are two or more .class files in the package.

    1. The subpackages always are not imported when you try to import the super package.So..You must specify the exact file to import rather than the * symbol.

  3. sir,

    1.can we method Overload in Tiger.class??

    2.And is it necessary to set classpath after modifying Tiger class each time.(else not compiling)

    please explain…

  4. Sir which file we need to delete the Tiger.java or Tiger.class if we delete Tiger.java how the target directory(sumathi) is accessed the Tiger.java in Animal.java file from forest package??

  5. Sir, can you please rewrite the 3rd step. I am unable to follow it. The output screen is not present over there.

    The other thing is you said that “To work with the above statement, remove the source file Tiger.java file from C:\snr directory” can you please elaborate it. I mean what is the reason behind it? When we are importing forest.* what does the other .java file in parent folder snr has to do with it?

    1. import forest.Tiger;

      In the above statement, only Tiger class from forest package is imported (ofcourse, in this case you have only class in the package).

      import forest.*;

      The above statement imports all the classes present in the forest package. But if you write as above with * mark, the program does not work. To work with, you have got Tiger.java in the C:\snr folder (from where you have compiled earlier). Now delete the Tiger.java file from c:\snr. Then * will work.

Leave a Comment

Your email address will not be published.