Open In App

CLASSPATH in Java

Package in Java is a mechanism to encapsulate a group of classes, sub-packages, and interfaces. Packages are used for:

Packages can be considered as data encapsulation (or data-hiding). Here we will be discussing the responsibility of the CLASSPATH environment variable while programming in Java as we move forward we for sure short need usage of importing statements.



Illustration:

 import org.company.Menu 

What does this import mean? It makes the Menu class available in the package org.company to our current class. Such that when we call the below command as follows: 



Menu menu = new Menu();   

Example




// Java Program to Illustrate Usage of importing
// Classes from packages and sub-packages
  
// Here we are importing all classes from
// java.io (input-output package)
import java.io.*;
  
// Main class
class GFG {
  
    // Main driver method
    public static void main(String[] args)
    {
  
        // Print statement
        System.out.println("I/O classes are imported from java.io package");
    }
}

Output
I/O classes are imported from java.io package

This package provides for system input and output through data streams, serialization, and the file system. Unless otherwise noted, passing a null argument to a constructor or method in any class or interface in this package will cause a NullPointerException to be thrown. All the classes listed here are imported or if we want to import a specific one then do use it as stated below.

import java.util.Scanner ;

The JVM knows where to find the class Menu. Now, how will the JVM know this location?

It is impractical for it to go through every folder on your system and search for it. Thus, using the CLASSPATH variable we provide it the place where we want it to look. We put directories and jars in the CLASSPATH variable.

Let’s say the above package resides in the directory dir. The complete path of the Menu class file would be dir/org/company/Menu. We’ll specify only the directory dir in our classpath variable, as the rest information regarding the path is provided by the import statements. Similar for jar, if you create a jar and mention its path in the variable, the VM will look inside the jar file and find the class. 

One should know how to set a classpath if not done after configuring JDK in respecting operating systems in order to see it or to it and play with multiple IDE, versions game altogether. One must have an absolutely clear understanding of it.

Article Tags :