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
org.springframework.aop
etcetera
2. strutsFramework
http://www.strutsapache.org/ org.apache.struts.web
org.apache.struts.interupts
etcetera
org.apache.struts.interupts
etcetera
3. userDefined
www.c-sharpcorner.com com.c-sharpcorner.web
com.c-sharpcorner.model
etcetera
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
- package p1;
- Public Class A {
- public static void main(String args[]) {
- System.out.println("A of pi is invoked");
- System.out.println("Instantiating B of p2....");
- p2.B y = new p2.B();
- System.out.println("Invoking display() of B of p2...");
- y.display();
- }
- }
Define package p2 and class B as.
This class is saved in the "D:\p2" folder.
B.java
- package p2;
- Public Class B {
- static {
- System.out.println("B of p2 loaded");
- }
- Public B() {
- System.out.println("B of p2 instantiated");
- }
- public void display() {
- System.out.println("display() of B of p2 invoked");
- }
- }












Join the conversation! Your thoughts help the community grow.