CLASSPATH in Java

Prerequisite : Packages in Java

This post targets at telling what the CLASSPATH environment variable if responsible for. While programming in Java, we many times use import statements. An example is:

 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



 Menu menu = new Menu() 

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 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.

Hope it clears 🙂

Note : CLASSPATH is explained in more detail in Packages in Java

Reference:
StackOverflow

This article is contributed by ekta1994. If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.



My Personal Notes arrow_drop_up


Article Tags :
Practice Tags :


4


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.