What is java.lang.ClassNotFoundException and how to resolve?

Source: Internet
Author: User
Keywords java.lang.ClassNotFoundException

What is java.lang.ClassNotFoundException?


The ClassNotFoundException is thrown to indicate that the specified class cannot be found in the classpath. The following piece of code tries to load the class using forName(), but the class name cannot be found. Thus, the exception is thrown.

How to Resolve java.lang.ClassNotFoundException


In older days, there are no editors like Eclipse are available. Even in Notepad, people have done java coding and by using the “javac” command to compile the java files, and they will create a ‘.class’ file. Sometimes accidentally the generated class files might be lost or set in different locations and hence there are a lot of chances of “ClassNotFoundException” occurs. After the existence of editors like Eclipse, Netbeans, etc., IDE creates a “ClassPath” file kind of entries.
 
From the above image, we can see that many jar files are present. They are absolutely necessary if the java code wants to interact with MySQL, MongoDB, etc., kind of databases, and also few functionalities need these jar files to be present in the build path. If they are not added, first editors show the errors themselves and provide the option of corrections too.

Implementation: Sample program of connecting to MySQL database and get the contents
2. How to deal with the java.lang.ClassNotFoundException
• Verify that the name of the requested class is correct and that the appropriate .jar file exists in your classpath. If not, you must explicitly add it to your application’s classpath.
• In case the specified .jar file exists in your classpath then, your application’s classpath is getting overridden and you must find the exact classpath used by your application.
• In case the exception is caused by a third party class, you must identify the class that throws the exception and then, add the missing .jar files in your classpath.
Below is the simple example to illustrate ClassNotFoundException and a way to fix it.
MainClass is dependent on DependentClass for the successful execution, if everything is there as expected then you will see below output,
1 Hello from main class
2 Hello from main class
3Loading dependent class
4Hello From Dependent Class


Dependent class loaded successfully
For the demo purpose, I have removed DependentClass.class from the output directory. Now if you try to run the MainClass you get below output,
1 Hello from main class
2Loading dependent class
3Exception in thread "main" java.lang.NoClassDefFoundError: com/jcg/DependentClass
4at com.jcg.MainClass.main(MainClass.java:7)
5Caused by: java.lang.ClassNotFoundException: com.jcg.DependentClass
6 at java.base/jdk.internal.loader.BuiltinClassLoader.loadClass(BuiltinClassLoader.java:581)
7 at java.base/jdk.internal.loader.ClassLoaders$AppClassLoader.loadClass(ClassLoaders.java:178)
8   at java.base/java.lang.ClassLoader.loadClass(ClassLoader.java:521)

9   ... 1 more


To fix this we need to make DependentClass.class available. This can be done by rebuilding the project in our case. Otherwise you need to verify the classes and libraries in your class path and fix the same.
Below is the code for our example,
DependentClass.java
1public class DependentClass {
2public void sayHello() {
3System.out.println("Hello From Dependent Class");
4 }

5MainClass.java

1 public class MainClass {

2    public static void main(String[] args) {
3        System.out.println("Hello from main class");
4      System.out.println("Loading dependent class");
5      DependentClass dClass = new DependentClass();
6      dClass.sayHello();
7      System.out.println("Dependent class loaded successfully");
8  }
9 }

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.