How to resolve java.lang.ClassNotFoundException in Java

来源:互联网 发布:js数字和字符串计算 编辑:程序博客网 时间:2024/06/15 17:32
ClassNotFoundException is one of java nightmare everybody face in there day to day life.this is one error which occurs by and now and chew up of your precious time while finding and fixing cause of it. From the name it looks quite simple but underlying cause of it is always different and which classifies it as an environmental issue. In this java tutorial we will seewhat is ClassNotFoundException in java, what is real cause of it and how to fix it along with some more frequent and infamous examples of java.lang.ClassNotFoundException in Java or J2EE, Don’t mistake this exception withNoClassDefFoundError in Java which is also due to incorrect classpath.


What is java.lang.classNotFoundException in Java

As the name suggests classNotFoundException in Java is a subclass of Exception and Comes when Java Virtual Machine tries to load a particular class and doesn't found the requested class in classpath. It is a checked Exception and you need to provide explicitly Exception handling while using methods which can possibly throw classnotfoundexception in java either by using try-catch block or by using throws clause. Though underlying concept of this exception is simple but it always manifest itself in such format that you need to spend some time to figure out what exactly wrong with your classpath. If you want to know nastysecrets of java classpath  which can cause issue see the link. 
When ClassNotFoundException occurs in Java:

As per java doc java.lang.classNotFoundException comes in following cases:

1) When we try to load a class by using Class.forName () method and Class is not available in classpath.
2) When Classloader try to load a class by using findSystemClass () method.
3) While using loadClass() method of class ClassLoader in Java.

These statements are completely true in terms of theory of ClassNotFoundExcepiton in Java but as per my experience the concept is "ClassNotFoundException will come only when JVM tries to load a class at run-time, nothing related to compile time unlike NoClassDefFoundError". Also since till run time JVM doesn't know about this Class it can only be done by above specified method or by employing Reflection to read the name of class from some configuration file like in case of struts its struts-config.xml file and then load the class specified on those configuration file. Reflection is great power of Java but you need to be aware of java.lang.classNotFoundException while using it or loading class in Java.


Examples of classnotfoundexception in java

though java.lang.classNotFoundException is very common and it can come for any classes, I usually see it while doing JDBC connectivity. I am going to list some of the most common scenario where you will get classnotfoundexception in java.

java.lang.classnotfoundexception com.mysql.jdbc.driver

This is classical and infamous example and also my first encounter with java.lang.ClassNotFoundException and comes when you are writing JDBC connectivity code and trying to load the JDBC driver. In this particular case of ClassNotFoundException looks like mysql driver jar file is missing from Classpath. If you pay attention you will find that we use method Class.forName (“driver”) to load the driver class which resides in a particular jar in case of this its mysql-connector.jar and if that jar is not in classpath or not accessible to JVM it will throw this error.

Here are few more infamous examples of java.lang.ClassnotFoundException which comes here and there while doing any Java J2EE project.
java.lang.classnotfoundexception org.hibernate.hql.ast.hqltoken
java.lang.classnotfoundexception org.springframework.web.context.contextloaderlistener
java.lang.classnotfoundexception org.eclipse.core.runtime.adaptor.eclipsestarter
java.lang.classnotfoundexception org.apache.catalina.startup.catalina
java.lang.classnotfoundexception javax.mail.messagingexception
java.lang.classnotfoundexception oracle.jdbc.driver.oracledriver

More Complicated ClassNotFoundException

With the advent of dynamic library e.g. OSGi and Classloader, this exception could be more tricky and hard to find. Thanks to Mr. Anonymous who has summarized this beautifully, here it is what he says
“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... “



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" itsmysql-connector-java.jar. If you don't know how to find which jar file a particular class you can seeeclipse 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(){
return "StockTrading";
}
}


2)
create a Class called OnlineStockTranding.java and load the classStockTrading.java asClass.forName ("stocktrading");

public class OnlineStockTrading{
public static void main(String args[]) throws ClassNotFoundException{
Class.forName("StockTrading");
System.out.println("StockTrading class successfully loaded");
}
}

3) Compile both class and run the program should run fine.

javin@trading~/java:
javac *.java

javin@trading ~/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

javin@trading ~/java:$
java OnlineStockTrading
StockTrading class successfully loaded


4) Now just
remove the .class file for stocktrading.java and run the program and it will throwjava.lang.ClassNotFoundException in java.

javin@trading ~/java:
rm StockTrading.class

javin@trading ~/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 toClass.forName () orClassloader.loadClass () orClassLoader.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)
UnSupportedClassVersionErroris 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:
How to use Comparator and Comparable in Java? With example
How Classpath work in Java? How to set classpath in Unix Linux
How to override equals method in Java
How to implement Thread in Java ?Example of Runnable interface
Difference between ConcurrentHashMap and Collections.synchronizedMap and Hashtable in Java
How to create update or remove symbolic or soft link in Unix
What is Abstraction in java?
What is the difference between Enumeration and Iterator?

Read more: http://javarevisited.blogspot.com/2011/08/classnotfoundexception-in-java-example.html#ixzz1uG3NGXdQ


Did you use a wrong class that packaged in an irrelevant jar file? Are you trying to search a class from many jar files? Java Class Finder is a free, small Java utility tool to search a given class from all jar files that in a selected directory (and subdirectories).

Click the link below to download Java Class Finder

http://www.idesksoft.com/JavaClassFinder.exe


原创粉丝点击