ClassNotFoundException vs NoClassDefFoundError in Java

Introduction

I will show an example on ClassNotFoundException vs NoClassDefFoundError. ClassNotFoundException is an exception whereas, NoClassDefFoundError is an error. I will discuss the difference between ClassNotFoundException and NoClassDefFoundError with examples.

ClassNotFoundException

ClassNotFoundException is thrown when an application tries to load in a class through its string name using

  • The forName() method in class Class.
  • The findSystemClass() method in class ClassLoader .
  • The loadClass() method in class ClassLoader.

but no definition for the class with the specified name could be found.

For ClassNotFoundException, it appears that it may stem from trying to make reflective calls to classes at runtime, but classes the program is trying to call do not exist.

ClassNotFoundException is an Exception, so it is somewhat expected, and is something that is recoverable.

Example

package com.roytuts.classexception;
public class ClassNotFoundExceptionTest {
	public static void main(String[] args) {
		try {
			Class.forName("ClassA"); //No definition for ClassA exists
		} catch (ClassNotFoundException e) {
			e.printStackTrace();
		}
	}
}

Output

By executing the above program you will see below output:

java.lang.ClassNotFoundException: ClassA
	at java.net.URLClassLoader.findClass(URLClassLoader.java:381)
	at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
	at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:331)
	at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
	at java.lang.Class.forName0(Native Method)
	at java.lang.Class.forName(Class.java:264)
	at com.roytuts.classexception.ClassNotFoundExceptionTest.main(ClassNotFoundExceptionTest.java:7)

NoClassDefFoundError

NoClassDefFoundError is thrown if the Java Virtual Machine or a Class Loader instance tries to load in the definition of a class (as part of a normal method call or as part of creating a new instance using the new expression) and no definition of the class could be found.

The searched-for class definition exist when the currently executing class was compiled, but the definition can no longer be found.

So, it appears that the NoClassDefFoundError occurs when the source was successfully compiled, but at runtime, the required class files were not found. This may be something that can happen in the distribution or production of JAR files, where not all the required class files were included.

NoClassDefFoundError is an Error and it arises from the Java Virtual Machine having problems finding a class it expected to find.

Example

package com.roytuts.classexception;
import javax.jms.ConnectionFactory;
import org.apache.activemq.ActiveMQConnection;
import org.apache.activemq.ActiveMQConnectionFactory;
public class NoClassDefFoundErrorTest {
	public static void main(String[] args) {
		ConnectionFactory connectionFactory = new ActiveMQConnectionFactory(
				ActiveMQConnection.DEFAULT_BROKER_URL);
	}
}

Output

By executing the above class you will see below output:

Exception in thread "main" java.lang.Error: Unresolved compilation problems:
	ConnectionFactory cannot be resolved to a type
	ActiveMQConnectionFactory cannot be resolved to a type
	ActiveMQConnection cannot be resolved to a variable
	at com.roytuts.classexception.NoClassDefFoundErrorTest.main(NoClassDefFoundErrorTest.java:11)

Related Posts:

Similarities

Both NoClassDefFoundError and ClassNotFoundException are related to unavailability of a class at run-time.

Both ClassNotFoundException and NoClassDefFoundError are related to Java classpath.

That’s all about ClassNotFoundException and NoClassDefFoundError.

Leave a Reply

Your email address will not be published. Required fields are marked *