Understanding ClassPath in Java

Introduction

 
This article explains ClassPath in Java.
 

What is ClassPath

 
From the word, we understand that classpath is a path of a class.  Yeah, that is correct; a classpath is a path for the compiler to identify which class, or which package we use in our defined class.  It provides the compiler a way to find classes or packages that are used in our class.
 
In this definition, we use the term "Package". Now understand what a package is.
 
Packages
 
It is a logical container of classes, interfaces, and sub-packages.  A package provides a unique space for classes and provides a scope for classes and interfaces.
 
A class is associated with a package using the following.
 
Syntax
 
packagename.classname
 
To associate a class with a package the following procedure is required.
 
1. The package keyword is used as the first statement in the class definition to specify the package of the class, as in the following:
 
package mypack:
 
2. The source file and generated class file are saved in a folder of the same name as the package.
 
Let's assume that there are two packages named P1 and P2.  Both of these packages contain A and B classes.
 
The classes of these packages can be uniquely identified as:
 
p1.A, p1.B and p2.A, p2.B
 
Note
 
To assert that packages of two different projects have a different name the following convention may be used
 Project-URL Project-name
 http://www.projectname.domainname/ domainName.projectName.packageName 
in the naming of the packages.
Examples
 
1. springFramework
www.springframework.org                            org.springfrmaework.web
                                                                      org.springframework.aop
                                                                      etcetera
2. strutsFramework
http://www.strutsapache.org/                         org.apache.struts.web
                                                                      org.apache.struts.interupts
                                                                      etcetera
3. userDefined
www.c-sharpcorner.com                                com.c-sharpcorner.web
                                                                      com.c-sharpcorner.model
                                                                      etcetera
 
Note - A package name is never associated with the Java file.   In other words, Java files are referred to independently.
 

How to access another package class

 
First understand a scenario; in this we create two Java files, the first one in A.java is contained in "E:\Sandy\java programs\p1" and the other one is "B.java" stored in the "D:\p2" folder.
 
Now we have used the B.java file in the A class.  So, in that order we need to call the B class; that is done using classpath.
 
ClassPath is used to tell the compiler that we are using another class contained in the path we provided.
 
Define package p1 and class A as.
 
This class is saved in the "E:\Sandy\java programs\p1" folder.
 
A.java
  1. package p1;  
  2. Public Class A {  
  3.     public static void main(String args[]) {  
  4.         System.out.println("A of pi is invoked");  
  5.         System.out.println("Instantiating B of p2....");  
  6.         p2.B y = new p2.B();  
  7.         System.out.println("Invoking display() of B of p2...");  
  8.         y.display();  
  9.     }  
  10. }  
 
Define package p2 and class B as.
 
This class is saved in the "D:\p2" folder.
 
B.java
  1. package p2;  
  2. Public Class B {  
  3.     static {  
  4.         System.out.println("B of p2 loaded");  
  5.     }  
  6.     Public B() {  
  7.         System.out.println("B of p2 instantiated");  
  8.     }  
  9.     public void display() {  
  10.         System.out.println("display() of B of p2 invoked");  
  11.     }  
  12. }  
Now compiling A.java
 
When we compile this file directly without giving a path introduction to the Java compiler, then see what happens:
 
Fig-1.jpg
 
As you can see, the compiler says that the p2 package does not exist.  In other words, they are unable find package p2 itself, so in this order we need to tell the compiler that you need to find package p2 in the given path.
 
The ClassPath environment variable is used by the compiler and JRE to locate user-defined classes.
 
Fig-2.jpg
 
1.0: Path of user defined packages are listed in the ClassPath variable.
2.0: References of classes are encountered during compilation execution.
2.1: The value of the classpath environment variable is read.
2.2: The folders listed in classpath are searched to locate the referenced classes.
2.3: If a referenced class is not found in a user defined package then it is searched for in the rt.jar file. If not found then a ClassNotFound exception is thrown.
 
Note - The default value of the class environment variable is the current class.
 
The value of the classpath environment variable is set permanently or can be specified for compilation and execution for a class.
 
Complete syntax for compilation and execution
 
1. Compiling a source file
 
javac -cp FoldertoLocateReferencedPackage NameOrPathOfJavaFile
 
2. Executing a Java class
 
javac -cp FoldertoLocateReferencedPackage NameOfClass
 
Note - A classpath is not applied to a Java file.  A Java file is loaded at a given path.
 
Now compile our class A using a class path.
 
Command
 
E:\Sandy\java program\p1> javac -cp D:\ A.java
 
Understand the command
 
1.  "-cp" is the syntax for the use classpath.
 
2. "D:\" tells the compiler that you need to find the package p2 in this folder. 
 
Fig-3.jpg
 
Now, we see that there is no error generated because we are providing a path for package p2.
 
Execute class A
 
Command
 
java -cp D:\;.. p1.A
 
The following explains what we wrote.
 
1. "D:\" is used to find package p2.
 
2. ".." is used to find the package p1. Since it is contained in the parent folder we use "..". If it exists in the current folder then we use "."
 
3. "p1.A" tells the compiler to execute class A in the p1 package.
 
Fig-4.jpg
 
Similarly we can compile and execute our class from any location.
 
Let's see an example.
 
The following are compilation commands for various locations.
 
1. E:\Sandy\java program\p1>
 
javac -cp D:\ A.java
 
Fig-5.jpg
 
2. D:\>
 
javac -cp D:\ E:\Sandy\javaprogram\p1\A.java
 
Fig-6.jpg
 
or
 
javac E:\Sandy\javaprogram\p1\A.java
 
Fig-7.jpg
 
3. C:\>
 
javac -cp D:\ E:\Sandy\javaprogram\p1\A.java
 
Fig-8.jpg
 
The following are compilation commands for executing our Class A, from various locations.
 
1. E:\Sandy\javaprogram\p1>
 
java -cp D:\;.. p1.A
 
Fig-9.jpg
 
2. D:\>
 
java -cp .;E:\Sandy\javaprogram\ p1.A
 
Fig-10.jpg
 
3. C:\>
 
java -cp D:\;E:\Sandy\javaprogram\ p1.A
 
Fig-11.jpg
 
Note
 
Similarly, we can compile and execute more than one referred class.


Similar Articles