How to resolve java.lang.ClassNotFoundException in Java
What is ClassNotFoundException in Java
More Complicated ClassNotFoundException
“It can become a bit more complicated than that. In truth a class does not have to be just visible by the JVM through its classpath, but be visible by the Classloader being used. When you are in a multi-classloader environment (In a EE environment, for example, but not limited to), each classloader may have its own rules to search for the classes, and this behavior might depend on the dynamic hierarchy of the Classloaders.
For example, in a project that uses an EAR packaging with WARs inside it, libraries in the lib folder of the EAR are visible to classes inside a WAR, but any classes packaged in a jar put in the WEB-INF/lib on the WAR cannot be seen by classes in different modules (other WARs, EJB-JARS, etc).
It can get really complicated as its common for different modules depending on different versions of the same libraries as different modules depend on each other. It can be a challenge to manage this. Sometimes the classloader can see multiple versions of the same class; sometimes they can see no version at all. Sometimes different dependency paths end in different versions of the same class. And many of this cases end in a ClassNotFoundException.
And then we have OSGi. “. If a class is not visible to ClassLoader than it can also throw NoClassDefFoundError in Java as explained in 3 ways to resolve NoClassDefFoundError in Java.
How to fix java.lang.ClassNotFoundException in Java
As you have seen from above examples its clear problem of classpath, so here is my approach to fix or resolve java.lang.ClassNotFoundException :
1) First find out the jar file on which problematic class file is present for example in case of " com.mysql.jdbc.driver " its mysql-connector-java.jar. If you don't know how to find which jar file a particular class you can see eclipse shortcuts to do that or you can simply do "Ctrl+T" in Eclipse and type the name of class, It will list all the jar in the order they appear in eclipse classpath.
2) Check whether your classpath contains that jar, if your classpath doesn't contain the jar then just add that class in your classpath.
3) If it’s present in your classpath then there is high chance that your classpath is getting overridden or application is using classpath specified in jar file or start-up script and to fix that you need to find the exact classpath used by your application.
Live example of reproducing and Fixing ClassNotFoundException in java
I think if we are able to reproduce and solve certain problem we become more comfortable dealing with that, that’s why here we will reproduce java.lang.ClassNotFoundException and solve it by following the concept we have discussed so far.
1) Create a Class called StockTrading.java
public class StockTrading <
public String getDescription()<
2) create a Class called OnlineStockTranding.java and load the class StockTrading.java as Class.forName ("stocktrading");
public class OnlineStockTrading <
public static void main(String args) throws ClassNotFoundException <
System.out.println("StockTrading class successfully loaded");
3) Compile both Java source file which will create two class files and run the program should run fine.
/java: javac *.java
/java: ls –lrt
-rw-r--r-- 1 javin None 90 Aug 21 09:27 StockTrading.java
-rw-r--r-- 1 javin None 208 Aug 21 09:28 OnlineStockTrading.java
-rwxr-xr-x 1 javin None 282 Aug 21 09:28 StockTrading.class
-rwxr-xr-x 1 javin None 638 Aug 21 09:28 OnlineStockTrading.class
/java:$ java OnlineStockTrading
StockTrading class successfully loaded
4) Now just remove the .class file for stocktrading.java and run the Java program and it will throw java.lang.ClassNotFoundException in java.
/java: java OnlineStockTrading
Exception in thread "main" java.lang.ClassNotFoundException: StockTrading
at java.net.URLClassLoader$1.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
ClassFoundException vs NoClassDefFoundError vs UnSupportedClassVersionError
There are lots of exceptions in java but these three are the one who most haunted the java developer most mainly because these three are mostly related to environment issues and they all depends upon JVM and Classpath behaviour. Though they look similar there is slight difference between ClassFoundException and NoClassDefFoundError and UnSupportedClassVersionError and we will highlight those differences here for easy understanding and differentiating these three:
1) ClassNotFoundException comes on Runtime when requested class is not available in classpath and mainly due to call to Class.forName () or Classloader.loadClass () or ClassLoader.findSystemClass () .
2) NoClassDefFoundError comes when problematic class was present when your compiled your application but they are not in classpath while you running your program.
3) UnSupportedClassVersionError is easy to differentiate because it’s related to version of classpath and usually comes when you compile your code in higher Java version and try to run on lower java version. Can be resolved simply by using one java version for compiling and running your application.
So that's all on ClassNotFoundException in java for now. please let me know if you have any tip or any personal experience on solving java.lang.ClassNotFoundException in Java which you would like to share.
Some more Interesting tutorials: