A Java package is basically a directory that contains Java classes that serve similar purposes. It is a mechanism used to group similar Java classes into separate sets to avoid confusion. Just like a directory on a computer’s operating system, a Java package may contain multiple classes and interfaces. It is a common practice to put classes that are created for similar purposes in the same package. Typically, the prefix of a package name is all lower case and is one of the top-level domain names, such as com or net. The subsequent components of a package name can be determined by the company’s own naming conventions. For example, for a company called mycompany, the classes that have the service code can be in com.mycompany.service package while the view classes can be in com.mycompany.view package. A package cannot have two classes with the same, however different packages may have classes that share the same name. For example, both com.mycompany.service and com.mycompany.view packages can have a MyComponent class that serve different purposes. Because the classes are in different packages, there will be no confusion. The classes will be referred to as
com.mycompany.service.mycomponent.MyComponent
and
com.mycompany.view.mycomponent.MyComponent
What is classpath?
Classpath is a system variable used by JVM to find classes defined by the user. It is set as an environment variable. By default, it is ., meaning the current directory.
Going back to HelloWorld class, the class was run by executing the command
java HelloWorld
from the directory that contained HelloWorld.class file. With this command, we mean, “Run the HelloWorld class’s byte code that is in the current directory.
Now, let’s define HelloWorld in a package. Because similar classes are packaged together, using the default package is strongly discouraged by the Java community. For this example, let’s say, we want to define HelloWorld class in com.mycompany.examples directory. For this, from the current directory, we need to create com/mycompany/examples directory. So, if we are inside C:\java_examples directory, we need to create C:\java_examples\com\mycompany\examples directory and inside this directory we save HelloWorld with the package name defined. Package declaration is the first line in a Java program. Wherever we save our code, it needs to be in a com\mycompany\examples directory.
package com.mycompany.examples
public class HelloWorld {
public static void main(String [] args) {
System.out.println(“Hello World”);
}
}
Following this, from C:\java_examples directory, we can compile HelloWorld class with the command
javac com\mycompany\examples\HelloWorld.java
This means “compile the HelloWorld class that is in the com\mycompany\examples directory relative to my current location”.
Alternatively, we can define the classpath inside the compile command and not enter the whole path to the file name.
javac -classpath “C:\java_examples” com\mycompany\examples\HelloWorld.java
By this we mean, “Set the file search path for Java compilation to C:\java_examples and compile com\mycompany\examples\HelloWorld.java file that is on the file search path”. Because HelloWorld.java is directly on the search path, it will succeed.
To execute the main method, we need to provide the fully qualified classname while running the class. From any location on the computer, if we run the following command
java -classpath “C:\java_examples” com.mycompany.examples.HelloWorld
we will get the same output. This command basically says to JVM “Set the file search path for Java compilation to C:\java_examples and execute the bytecode in the file com\mycompany\examples\HelloWorld.class that is on the file search path”.
Because now HelloWorld class is defined inside a package, it always needs to be called with its fully qualified name.
Instead of defining inside the run command itself, the classpath can be defined as an environment variable. This way, we don’t need to set it each time we execute a java command.
To define the classpath on Windows:
- Right click on My Computer and from the menu select Properties
- Click on Advanced system settings on the left menu
- Switch to Advanced tab
- Click Environment variables
- Select New, enter “classpath” for the name property and where you want your classpath to point to for the value property.
To define the classpath on Linux or MacOS, enter this to your .profile file.
export $classpath=<where you want your classpath to point to>