How To Resolve Java.Lang.Classnotfoundexception Inward Java

What is ClassNotFoundException inward Java
ClassNotFoundException is 1 of Java nightmare every Java developer human face upward inward in that place twenty-four hr catamenia to twenty-four hr catamenia life. java.lang.NoClassDefFoundError in addition to java.lang.ClassNotFoundException are 2 errors  which occurs past times in addition to at 1 time in addition to chew upward of your precious fourth dimension piece finding in addition to fixing beginning cause. From the parent java.lang.ClassNotFoundException looks quite elementary but underlying motility of it is ever unlike and which classifies it every bit an environmental issue. In this coffee tutorial nosotros volition see what is ClassNotFoundException inward java, what is existent motility of it and how to create it along amongst closed to to a greater extent than frequent in addition to infamous examples of java.lang.ClassNotFoundException inward Java or J2EE, Don’t mistake this exception amongst NoClassDefFoundError inward Java which is likewise due to wrong classpath inward Java.  Though both of them are related to missing flat file when Java tries to charge flat inward Java they are completely unlike to each other.  Correct agreement of  When flat is loaded inward Java in addition to How Classpath works  is must to troubleshoot in addition to create this error quickly.


What is java.lang.classNotFoundException inward Java

As the parent suggests classNotFoundException in Java is a subclass of java.lang.Exception in addition to Comes when Java Virtual Machine tries to charge a detail flat in addition to doesn't establish the requested flat inward classpath. Another of import betoken close this Exception is that, It is a checked Exception in addition to yous involve to render explicitly Exception treatment piece using methods which tin mayhap throw classnotfoundexception inward coffee either past times using try-catch block or past times using throws clause. Though underlying concept of this exception is elementary but it ever manifest itself inward such format that yous involve to pass closed to fourth dimension to figure out what just wrong amongst your classpath. If yous desire to know nasty secrets of coffee classpath  which tin motility outcome catch the link




When ClassNotFoundException occurs inward Java:

As per java MD java.lang.classNotFoundException comes inward next cases:

1) When nosotros sweat to charge a flat past times using Class.forName() method in addition to .class file or binary of flat is non available inward classpath.
2) When Classloader try to charge a flat past times using findSystemClass () method.
3) While using loadClass() method of flat ClassLoader in Java.

Reflection is neat ability of Java but yous involve to last aware of java.lang.classNotFoundException piece using it or loading flat inward Java. See Understanding the Java Virtual Machine: Class Loading in addition to Reflection to acquire to a greater extent than close how just flat loading industrial plant within JVM.


Examples of classnotfoundexception inward java

Though java.lang.classNotFoundException is rattling mutual in addition to it tin come upward for whatever classes, I unremarkably catch it piece doing JDBC connectivity similar when I was writing Java plan to connect Oracle database. I am going to listing closed to of the most mutual scenario where yous volition acquire classnotfoundexception inward java.

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

This is classical in addition to most infamous representative of  in addition to likewise my starting fourth dimension come across with java.lang.ClassNotFoundException in addition to comes when yous are writing JDBC connectivity code in addition to trying to charge the JDBC driver. In this detail instance of ClassNotFoundException looks similar mysql driver appall file is missing from Classpath. If yous pay attending yous volition abide by that nosotros purpose method Class.forName (“driver”) to charge the driver flat which resides inward a detail appall inward instance of this its mysql-connector.jar in addition to if that appall is non inward classpath or non accessible to JVM it volition throw  java.lang.ClassNotFoundException: com.mysql.jdbc.Driver

Here are few to a greater extent than infamous examples of java.lang.ClassnotFoundException which comes hither in addition to in that place piece doing whatever 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

This ClassNotfoundException comes when yous are trying to connect Oracle database from Java plan using JDBC but yous don't conduct maintain corresponding Oracle driver e.g.ojdbc6.jar is non inward classpath of your Java program

More Complicated ClassNotFoundException

With the advent of dynamic library e.g. OSGi and ClassLoader inward Java, this exception tin last to a greater extent than tricky in addition to difficult to find. Thanks to Mr. Anonymous who has summarized this beautifully, hither it is what he says
“It tin conk a chip to a greater extent than complicated than that. In truth a flat does non conduct maintain to last just visible past times the JVM through its classpath, but last visible past times the Classloader being used. When yous are inward a multi-classloader environment (In a EE environment, for example, but non express to), each classloader may conduct maintain its ain rules to search for the classes, in addition to this demeanor powerfulness depend on the dynamic hierarchy of the Classloaders.
For example, inward a projection that uses an EAR packaging amongst WARs within it, libraries inward the lib folder of the EAR are visible to classes within a WAR, but whatever classes packaged inward a appall pose inward the WEB-INF/lib on the WAR cannot last seen past times classes inward unlike modules (other WARs, EJB-JARS, etc).
It tin acquire actually complicated every bit its mutual for unlike modules depending on unlike versions of the same libraries every bit unlike modules depend on each other. It tin last a challenge to deal this. Sometimes the classloader can catch multiple versions of the same class; sometimes they tin catch no version at all. Sometimes unlike dependency paths goal inward unlike versions of the same class. And many of this cases goal inward a ClassNotFoundException.
And in addition to therefore nosotros conduct maintain OSGi... “. If a flat is non visible to ClassLoader than it tin likewise throw NoClassDefFoundError in Java every bit explained in  3 ways to resolve NoClassDefFoundError inward Java.

How to create java.lang.ClassNotFoundException inward Java
As yous conduct maintain seen from to a higher house examples its clear employment of classpath, therefore hither is
my approach to create or resolve java.lang.ClassNotFoundException:

1) First abide by out the appall file on which problematic flat file is acquaint for representative inward instance of "
com.mysql.jdbc.driver" its mysql-connector-java.jar. If yous don't know how to abide by which jar file a detail flat yous tin catch eclipse shortcuts to arrive at that or yous tin only arrive at "Ctrl+T" inward Eclipse in addition to type the parent of class, It volition listing all the appall inward the guild they appear inward eclipse classpath.

2) Check whether your classpath contains that jar, if your classpath doesn't incorporate the appall in addition to therefore just add together that flat inward your classpath.

3) If it’s acquaint inward your classpath in addition to therefore in that place is high gamble that your classpath is getting overridden or application is using classpath specified inward appall file or start-up script in addition to to create that yous involve to abide by the exact classpath used past times your application.


Live representative of reproducing in addition to Fixing ClassNotFoundException inward java
I recall if nosotros are able to reproduce in addition to solve surely employment nosotros conk to a greater extent than comfortable dealing amongst that, that’s why hither nosotros volition reproduce java.lang.ClassNotFoundException and solve it past times next the concept nosotros conduct maintain discussed therefore far.

1)
Create a Class called StockTrading.java

public flat StockTrading{
   populace String getDescription(){
   render "StockTrading";
  }
}


2)
create a Class called OnlineStockTranding.java in addition to charge the flat StockTrading.java every bit Class.forName ("stocktrading");

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

3) Compile both Java source file which volition create 2 flat files in addition to run the plan should run fine.

javin@trading /java:
javac *.java

javin@trading /java:
ls –lrt
-rw-r--r-- 1 javin None  xc 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 flat successfully loaded


4) Now just
remove the .class file for stocktrading.java in addition to run the Java program in addition to it volition throw java.lang.ClassNotFoundException inward java.

javin@trading /java:
rm StockTrading.class

javin@trading /java:
java OnlineStockTrading
Exception inward 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 inward coffee but these 3 are the 1 who most haunted the coffee developer most mainly because these 3 are generally related to environs issues in addition to they all depends upon JVM in addition to Classpath behaviour. Though they expect similar in that place is slight difference betwixt ClassFoundException in addition to NoClassDefFoundError in addition to UnSupportedClassVersionError in addition to nosotros volition highlight those differences hither for slowly agreement in addition to differentiating these three:

1)
ClassNotFoundException comes on Runtime when requested flat is non available inward classpath in addition to mainly due to telephone outcry upward to Class.forName () or Classloader.loadClass () or ClassLoader.findSystemClass ().

2)
NoClassDefFoundError comes when problematic flat was acquaint when your compiled your application but they are non inward classpath piece yous running your program.

3)
UnSupportedClassVersionError is slowly to differentiate because it’s related to version of classpath in addition to unremarkably comes when yous compile your code inward higher Java version in addition to sweat to run on lower coffee version. Can last resolved only past times using 1 coffee version for compiling in addition to running your application.

So that's all on
ClassNotFoundException inward java for at 1 time , delight allow me know if yous conduct maintain whatever tip or  whatever personal sense on solving java.lang.ClassNotFoundException inward Java which yous would similar to share.


Further Learning
How to purpose Comparator in addition to Comparable inward Java? With example
How Classpath run inward Java? How to laid classpath inward Unix Linux
How to override equals method inward Java
How to implement Thread inward Java ?Example of Runnable interface
Difference betwixt ConcurrentHashMap in addition to Collections.synchronizedMap in addition to Hashtable inward Java
How to create update or take symbolic or soft link inward Unix
What is Abstraction inward coffee ?
What is the departure betwixt Enumeration in addition to Iterator?

Komentar

Postingan populer dari blog ini

Common Multi-Threading Mistakes Inwards Coffee - Calling Run() Instead Of Start()

3 Examples Of Parsing Html File Inwards Coffee Using Jsoup

Why You Lot Should Command Visibility Of Shape Too Interface Inward Java