ClassNotFound Exception in JavaOne particular exception that is confusing for beginners is the ClassNotFound Exception in Java. In this tutorial, we are going to get familiar with ClassNotFound exception and the fix for it. ClassNotFound ExceptionThe name itself suggests that when the class is not ClassNotFound exception occurs. The ClassNotFound exception is raised when a Java Virtual Machine (JVM) tries to load a specific class, and that class is not found in the specified classpath. In other words, the classpath is broken. Note that ClassNotFound Exception is a checked exception.
Fix For the ClassNotFoundExceptionOne can take into consideration the following points while tackling the ClassNotFoundException.
Example - 1In this example, we will see how ClassNotFound exception is raised and fixed. We will be using the findSystemClasas() method in this example. FileName: ClassException1.java Output: Exception in thread "main" java.lang.ClassNotFoundException: com.google.common.collect.ImmutableList at java.base/jdk.internal.loader.BuiltinClassLoader.loadClass(BuiltinClassLoader.java:641) at java.base/jdk.internal.loader.ClassLoaders$AppClassLoader.loadClass(ClassLoaders.java:188) at java.base/java.lang.ClassLoader.loadClass(ClassLoader.java:520) at java.base/java.lang.ClassLoader.findSystemClass(ClassLoader.java:1252) at SystemClass.loadSystemClass(ClassException1.java:6) at ClassException1.main(ClassException1.java:28) Explanation: JVM is trying to load ImmutableList (at run time), which is not present. Hence, the ClassNotFound exception is raised. Note that the above exception is raised when we run the above program using the following command Therefore, we need to add the appropriate .jar file in the classpath. We can do so using the following command. We get the following output. Example - 2In the following example, we have used the forName() method of the class Class. FileName: ClassException2.java Output: Exception in thread "main" java.lang.ClassNotFoundException: com.mysql.jdbc.Driver at java.base/jdk.internal.loader.BuiltinClassLoader.loadClass(BuiltinClassLoader.java:641) at java.base/jdk.internal.loader.ClassLoaders$AppClassLoader.loadClass(ClassLoaders.java:188) at java.base/java.lang.ClassLoader.loadClass(ClassLoader.java:520) at java.base/java.lang.Class.forName0(Native Method) at java.base/java.lang.Class.forName(Class.java:375) at ClassException2.main(ClassException2.java:10) Explanation: The issue is the same. JVM is trying to load the Driver class and is unable to do so. Therefore, we have to add the path where JVM can find the Driver.class file. The above code is run using the following command. We need the following modification, in order to include the appropriate .jar file to run the program successfully. We get the following output. Example - 3In the following example, we have used the loadClass() method. FileName: ClassException3.java Output: Exception in thread "main" java.lang.ClassNotFoundException: com.google.common.collect.Constraint at java.base/java.lang.ClassLoader.findClass(ClassLoader.java:718) at java.base/java.lang.ClassLoader.loadClass(ClassLoader.java:587) at java.base/java.lang.ClassLoader.loadClass(ClassLoader.java:520) at SystemClass.loadSystemClass(ClassException3.java:6) at ClassException3.main(ClassException3.java:28) Explanation: Same as the above example. The above runs smoothly if we include the jar file in the classpath. Readers are advised to do it on their own by taking the reference of the example - 1. Classpath and ClassNotFoundExceptionA classpath contains the locations list from where the classes are loaded. These locations include jar files or directories. In the case of directories, the Java Virtual Machine follows a pattern in order to load a class. For example, if one has put the directory as "C:/javaProj/MyClasses" in the classpath, and in the application, it is attempted to load the class com.myproj.myprogram, then JVM looks in the MyClasses directory and searches for the folder called com. Inside com, it looks for the directory called myproj, and inside myproj, it searches for the file called myprogram.class. In the case of jar files, Java Virtual Machine will look in the jar file for that class that has to be loaded. Similar to the above example, a jar file is a collection of different directories in the zipped form. If we unzip a jar file, we'll get different directories and different class files that follow the above-mentioned pattern. The JVM reads a classpath from start to end and looks for the class definition of the class that has to be loaded. For example, in following the classpath : C:/javaProj/MyClasses; C:/javaProj/MyClasses/jkl.jar; C:/javaProj/MyClasses/xyz.jar The JVM looks in the MyClasses directory first, to find the appropriate class that has to be loaded, and then in the jkl.jar and after that in the xyz.jar file. Therefore, if the ClassNotFoundException is raised, then it simply means the required file is neither present in the .jar files nor in the MyClasses directory. Next TopicNull Pointer Exception in Java |
We provides tutorials and interview questions of all technology like java tutorial, android, java frameworks
G-13, 2nd Floor, Sec-3, Noida, UP, 201301, India