In this article we’ll see what is java.lang.ClassNotFoundException
and how to resolve ClassNotFoundException in Java.
ClassNotFoundException in Java
ClassNotFoundException is a child class of Exception class. Being a descendant of Exception class also makes it a checked exception that means it needs to be either caught in a method (with in a try-catch block), or the method needs to specify that exception in a throws clause declaration.
When is ClassNotFoundException thrown
ClassNotFoundException is thrown when a Java application tries to load in a class through its string name but definition for the class with the specified name could not be found.
Methods that can be used for loading a class through its String name are as following-
- The Class.forName method in class Class.
- The findSystemClass method in class ClassLoader.
- The loadClass method in class ClassLoader.
ClassNotFoundException is thrown at runtime since classloader tries to load the class at runtime using the String name.
Java ClassNotFoundException example
As the name of the exception itself suggests this exception is thrown when the class that has to be loaded is not found. One scenario where you may find this exception is if you try to load JDBC drivers using Class.forName() but the required jar is not in the classpath.
I have also encountered ClassNotFoundException when upgrading to new version of jar in my system and using some new classes available in that jar but not upgrading the jar in the Test server or production. With that you get the scenario which is often quoted by the developers “In my system it is working fine”!!!
For example following program tries to load Oracle driver but the required ojdbcxx.jar is not present in the classpath.
public class JDBCCon { public static void main(String[] args) { Connection connection = null; try { // Loading driver Class.forName("oracle.jdbc.driver.OracleDriver"); // Creating connection connection = DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:xe", "root", "admin"); // creating Statement Statement stmt = connection.createStatement(); // Executing Query ResultSet rs = stmt.executeQuery("Select * from Employee"); // Processing Resultset while(rs.next()){ System.out.println("id : " + rs.getInt("id") + " Name : " + rs.getString("name") + " Age : " + rs.getInt("age")); } } catch (ClassNotFoundException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); }finally{ if(connection != null){ //closing connection try { connection.close(); } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } } } } }
Output
java.lang.ClassNotFoundException: oracle.jdbc.driver.OracleDriver at java.base/jdk.internal.loader.BuiltinClassLoader.loadClass(BuiltinClassLoader.java:583) at java.base/jdk.internal.loader.ClassLoaders$AppClassLoader.loadClass(ClassLoaders.java:178) at java.base/java.lang.ClassLoader.loadClass(ClassLoader.java:521) at java.base/java.lang.Class.forName0(Native Method) at java.base/java.lang.Class.forName(Class.java:332) at org.netjs.prgrm.JDBCCon.main(JDBCCon.java:16)
As evident from the exception stack trace OracleDriver is not found so the ClassNotFoundException is thrown.
Resolving ClassNotFoundException in Java
As seen in the above example it is easy to see from the error log or exception stacktrace which class is causing the exception. Then you need to check whether the required jar (where the class resides) is present in the classpath or not.
If jar is there then ensure that you have the correct version and the class you are trying to load is part of that jar version.
If you are packaging your application and putting that jar or war in a server or another system to run make sure that all the classes are packaged properly and there are no omissions.
If class is there in the classpath then do ensure that there is no classpath change as part of some startup script.
That's all for this topic java.lang.ClassNotFoundException - Resolving ClassNotFoundException in Java. If you have any doubt or any suggestions to make please drop a comment. Thanks!
Related Topics
You may also like-
No comments:
Post a Comment