5 most common CLASSPATH problems with solutions

With the advent of tools like Maven, Apache IVY etc. managing CLASSPATH has become super easy. But the downside is that many developers aren’t able to understand the underlying concept of CLASSPATH. I see a lot of developers who struggle a lot if asked to set a new project (Java Project/Web Application) in CLASSPATH.

Definition of CLASSPATH

CLASSPATH refers to an environment variable which stores semi colon (;) separated paths of third party JAR files. Whenever a Java program tries to use classes not present in the project or JDK, the JAVA Virtual Machine (JVM) goes and looks for these classes in the paths mentioned in the CLASSPATH environment variable. If a match is found between the class being requested and the classes present in the CLASSPATH, the class is loaded into the JVM memory. This process of loading the class into memory is done by a special component known as class loader.

Common issues with CLASSPATH

Here we list the most common problems regarding the use of CLASSPATH entry in Java:

5) Forgetting that Eclipse IDE manages its own CLASSPATH

The IDE’s like Eclipse, NetBeans and IntelliJ Idea etc. maintain their own version of classpath entries for each project. These IDE don’t look at the value of CLASSPATH eviornment variable which is set at the operating system level. To see the complete classpath entry for say a project in Eclipse IDE, you need to browse to the project folder and open a file named as .classpath. The contents of this file is the classpath entry for that project in Eclipse IDE.

Many beginners forget the above point and keep on struggling to find the root cause of the problem. As per their understanding, they have set the classpath in OS but IDE is having an issue (as if it has gone corrupt). Once they read about the concept of IDE level classpath, they either want to bang their head or abuse the IDE.

4) Clashes in Classpath:

Consider a situation when two JAR files have been added to CLASSPATH and both these JAR files have some set of common classes (along with their package name). As soon as the JVM starts up, it tries to load classes from java.lang package and also from JAR files present in CLASSPATH.
In the case described above, the class loader will run into issues as soon as it sees that the same class is present in multiple JAR files. The exact behavior of classpath depends upon the JVM implementation. Some class-loaders, ignore the second occurrence of same class if it has been loaded into the memory. The problem in this case is that if you have just placed a new version of JAR file at the end of CLASSPATH, it will be ignored and the earlier version of the JAR file shall be used.

The moral of the story is to be careful when adding JAR files to classpath. Any wrong or duplicate entry in the CLASSPATH can lead to the above problem which is also known as JAR hell.

3) System and User level classpath:

Most of the operating systems have two levels viz. the system and user levels for creating the enviornment variables. If an environment variable with the name CLASSPATH is defined at the system as well the user level, then the entry defined at the system level takes precedence/priority. The following screenshot shows the CLASSPATH environment variable being defined at both these levels in Windows 7:

The problem is again with respect to misunderstanding about the fact that the entry defined at user level doesn’t have any significance when the user level entry has been defined. Developers usually think that JAR files should get loaded into JVM because it has been added at the user level environment variable, whereas the loading doesn’t happen because JVM has ignored the user level definition.

2) JAR files present in lib folder:

If you have imported a new JAVA project into Eclipse IDE and are seeing a lot of compiler errors due to missing class files then the problem is not about adding the supporting JAR files to the classpath of Eclipse. The underlying problem is that while you think that the JAR’s in lib folder will get loaded (as with Tomcat/JBoss server), they are not. In order to resolve the compiler error, you need to right click on the project and configure the build path which opens up. The following screenshot shows the popup in Eclipse:

Now you click Add JAR button to add the jar files from lib folder to your project’s classpath. The add external JAR file button is used to add JAR files from hard drive to the classpath.

1) Overwriting the current value of CLASSPATH in command line:

In *nix operating systems, many a times the developer needs to set the classpath by using CLI. A typical UNIX command to set the classpath looks like:

$ export CLASSPATH=/usr/bin/;/usr/lib/;

The problem here is that if this command is executed, it will simply replace the classpath value which was earlier assigned to it. When intention is to append paths to the CLASSPATH variable using command line, it is very important to make sure that you do not overwrite the previous value.

Bonus: When CLI is used for setting classpath, the changes done to classpath environment variable are not propagated to system settings. This means that if you open another CLI window then the changes done to the value of CLASSPATH in first window won’t be visible in second window.

With the advent of tools like Maven, Apache IVY etc. managing CLASSPATH has become super easy. But the downside is that many developers aren't able to understand the underlying concept of CLASSPATH. I see a lot of developers who struggle a lot if asked to set a new project...

With the advent of tools like Maven, Apache IVY etc. managing CLASSPATH has become super easy. But the downside is that many developers aren't able to understand the underlying concept of CLASSPATH. I see a lot of developers who struggle a lot if asked to set a new project (Java Project/Web Application) in CLASSPATH.
<h2>Definition of CLASSPATH</h2>
CLASSPATH refers to an environment variable which stores semi colon (;) separated paths of third party JAR files. Whenever a Java program tries to use classes not present in the project or JDK, the JAVA Virtual Machine (JVM) goes and looks for these classes in the paths mentioned in the CLASSPATH environment variable. If a match is found between the class being requested and the classes present in the CLASSPATH, the class is loaded into the JVM memory. This process of loading the class into memory is done by a special component known as class loader.
For details on how to set PATH, CLASSPATH and JAVA_HOME environment variables in various operating systems, check out our blog post on <a href="http://www.javaexperience.com/setting-path-classpath-and-java_home-enviornment-variables/">Setting PATH, CLASSPATH and JAVA_HOME</a>
<h2>Common issues with CLASSPATH</h2>
Here we list the most common problems regarding the use of CLASSPATH entry in Java:
<h3>5) Forgetting that Eclipse IDE manages its own CLASSPATH</h3>
The IDE's like Eclipse, NetBeans and IntelliJ Idea etc. maintain their own version of classpath entries for each project. These IDE don't look at the value of CLASSPATH eviornment variable which is set at the operating system level. To see the complete classpath entry for say a project in Eclipse IDE, you need to browse to the project folder and open a file named as .classpath. The contents of this file is the classpath entry for that project in Eclipse IDE.
Many beginners forget the above point and keep on struggling to find the root cause of the problem. As per their understanding, they have set the classpath in OS but IDE is having an issue (as if it has gone corrupt). Once they read about the concept of IDE level classpath, they either want to bang their head or abuse the IDE.
<h3>4) Clashes in Classpath:</h3>
Consider a situation when two JAR files have been added to CLASSPATH and both these JAR files have some set of common classes (along with their package name). As soon as the JVM starts up, it tries to load classes from java.lang package and also from JAR files present in CLASSPATH.
In the case described above, the class loader will run into issues as soon as it sees that the same class is present in multiple JAR files. The exact behavior of classpath depends upon the JVM implementation. Some class-loaders, ignore the second occurrence of same class if it has been loaded into the memory. The problem in this case is that if you have just placed a new version of JAR file at the end of CLASSPATH, it will be ignored and the earlier version of the JAR file shall be used.
The moral of the story is to be careful when adding JAR files to classpath. Any wrong or duplicate entry in the CLASSPATH can lead to the above problem which is also known as JAR hell.
<h3>3) System and User level classpath:</h3>
Most of the operating systems have two levels viz. the system and user levels for creating the enviornment variables. If an environment variable with the name CLASSPATH is defined at the system as well the user level, then the entry defined at the system level takes precedence/priority. The following screenshot shows the CLASSPATH environment variable being defined at both these levels in Windows 7:
<a href="http://www.javaexperience.com/wp-content/uploads/2013/09/classpath.png"><img src="http://www.javaexperience.com/wp-content/uploads/2013/09/classpath-234x300.png" alt="classpath" width="234" height="300" class="alignnone size-medium wp-image-3641" /></a>
The problem is again with respect to misunderstanding about the fact that the entry defined at user level doesn't have any significance when the user level entry has been defined. Developers usually think that JAR files should get loaded into JVM because it has been added at the user level environment variable, whereas the loading doesn't happen because JVM has ignored the user level definition.
<h3>2) JAR files present in lib folder:</h3>
If you have imported a new JAVA project into Eclipse IDE and are seeing a lot of compiler errors due to missing class files then the problem is not about adding the supporting JAR files to the classpath of Eclipse. The underlying problem is that while you think that the JAR's in lib folder will get loaded (as with Tomcat/JBoss server), they are not. In order to resolve the compiler error, you need to right click on the project and configure the build path which opens up. The following screenshot shows the popup in Eclipse:
<a href="http://www.javaexperience.com/wp-content/uploads/2013/09/add_jar.png"><img src="http://www.javaexperience.com/wp-content/uploads/2013/09/add_jar-300x205.png" alt="add_jar" width="300" height="205" class="alignnone size-medium wp-image-3642" /></a>
Now you click Add JAR button to add the jar files from lib folder to your project's classpath. The add external JAR file button is used to add JAR files from hard drive to the classpath.
<h3>1) Overwriting the current value of CLASSPATH in command line:</h3>
In *nix operating systems, many a times the developer needs to set the classpath by using CLI. A typical UNIX command to set the classpath looks like:
<blockquote>$ export CLASSPATH=/usr/bin/;/usr/lib/;</blockquote>
The problem here is that if this command is executed, it will simply replace the classpath value which was earlier assigned to it. When intention is to append paths to the CLASSPATH variable using command line, it is very important to make sure that you do not overwrite the previous value.
Bonus: When CLI is used for setting classpath, the changes done to classpath environment variable are not propagated to system settings. This means that if you open another CLI window then the changes done to the value of CLASSPATH in first window won't be visible in second window.

adminhttps://plus.google.com/108156674671499593770/posts[email protected]AdministratorI run this blog with lots of passion. In this website, you will find tutorials on Core Java, Spring, Struts, Web Applications, Portals and Database.
Please support me and the website by sharing the posts on your facebook / twitter. You can tap the share button at the top of each post.
Thanks for the support.Java Experience

I run this blog with lots of passion. In this website, you will find tutorials on Core Java, Spring, Struts, Web Applications, Portals and Database.
Please support me and the website by sharing the posts on your facebook / twitter. You can tap the share button at the top of each post.
Thanks for the support.