Tutorial

Java ClassNotFoundException - java.lang.ClassNotFoundException

Published on August 3, 2022
Default avatar

By Pankaj

Java ClassNotFoundException - java.lang.ClassNotFoundException

While we believe that this content benefits our community, we have not yet thoroughly reviewed it. If you have any suggestions for improvements, please let us know by clicking the “report an issue“ button at the bottom of the tutorial.

Java ClassNotFoundException

  • Java ClassNotFoundException occurs when the application tries to load a class but Classloader is not able to find it in the classpath.
  • Common causes of java.lang.ClassNotFoundException are using Class.forName or ClassLoader.loadClass to load a class by passing String name of a class and it’s not found on the classpath.
  • ClassNotFoundException is a checked exception, so it has to be catch or thrown to the caller.
  • ClassNotFoundException always occurs at runtime because we are indirectly loading the class using Classloader. Java compiler has no way to know if the class will be present in the classpath at runtime or not.
  • One of the most common example of ClassNotFoundException is when we try to load JDBC drivers using Class.forName but forget to add it’s jar file in the classpath.

Java ClassNotFoundException Example

Let’s look at a simple example where we will get ClassNotFoundException.

package com.journaldev.exceptions;

public class DataTest {

  public static void main(String[] args) {
    try {
      Class.forName("com.journaldev.MyInvisibleClass");

      ClassLoader.getSystemClassLoader().loadClass("com.journaldev.MyInvisibleClass");

      ClassLoader.getPlatformClassLoader().loadClass("com.journaldev.MyInvisibleClass");
    } catch (ClassNotFoundException e) {
      e.printStackTrace();
    }
  }

}

Note that com.journaldev.MyInvisibleClass doesn’t exist, so When we execute above program, we get following exception stack trace.

java.lang.ClassNotFoundException: com.journaldev.MyInvisibleClass
	at java.base/jdk.internal.loader.BuiltinClassLoader.loadClass(BuiltinClassLoader.java:582)
	at java.base/jdk.internal.loader.ClassLoaders$AppClassLoader.loadClass(ClassLoaders.java:185)
	at java.base/java.lang.ClassLoader.loadClass(ClassLoader.java:496)
	at java.base/java.lang.Class.forName0(Native Method)
	at java.base/java.lang.Class.forName(Class.java:292)
	at com.journaldev.exceptions.DataTest.main(DataTest.java:7)

In above example, all the three statements will throw java.lang.ClassNotFoundException.

How to resolve ClassNotFoundException

It’s very easy to fix ClassNotFoundException because the exception stack trace clearly specifies the class not found. Just check for classpath settings and make sure class it’s present at runtime.

ClassNotFoundException vs NoClassDefFoundError

NoClassDefFoundError is a runtime error thrown when a class is not found at runtime. It’s very similar to ClassNotFoundException. Read more at Java NoClassDefFoundError. Reference: API Doc

Thanks for learning with the DigitalOcean Community. Check out our offerings for compute, storage, networking, and managed databases.

Learn more about us


About the authors
Default avatar
Pankaj

author

Still looking for an answer?

Ask a questionSearch for more help

Was this helpful?
 

Try DigitalOcean for free

Click below to sign up and get $200 of credit to try our products over 60 days!

Sign up

Join the Tech Talk
Success! Thank you! Please check your email for further details.

Please complete your information!

Get our biweekly newsletter

Sign up for Infrastructure as a Newsletter.

Hollie's Hub for Good

Working on improving health and education, reducing inequality, and spurring economic growth? We'd like to help.

Become a contributor

Get paid to write technical tutorials and select a tech-focused charity to receive a matching donation.

Welcome to the developer cloud

DigitalOcean makes it simple to launch in the cloud and scale up as you grow — whether you're running one virtual machine or ten thousand.

Learn more
DigitalOcean Cloud Control Panel