Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why am I getting a NoClassDefFoundError in Java?

I am getting a NoClassDefFoundError when I run my Java application. What is typically the cause of this?

like image 238
John Meagher Avatar asked Aug 29 '08 14:08

John Meagher


People also ask

How do I get rid of NoClassDefFoundError in Java?

NoClassDefFoundError, which means the Class Loader file responsible for dynamically loading classes can not find the . class file. So to remove this error, you should set your classpath to the location where your Class Loader is present.

What causes Java Lang NoClassDefFoundError?

java. lang. NoClassDefFoundError is runtime error thrown when a required class is not found in the classpath and hence JVM is unable to load it into memory.

How do I fix Java Lang NoClassDefFoundError in eclipse?

1) The class is not available in Java Classpath. 2) You might be running your program using the jar command and class was not defined in the manifest file's ClassPath attribute. 3) Any start-up script is an overriding Classpath environment variable. 4) Because NoClassDefFoundError is a subclass of java.

In what all scenarios a NoClassDefFoundError will be thrown?

NoClassDefFoundError is thrown when a class has been compiled with a specific class from the classpath but if same class not available during run-time. Missing JAR files are the most basic reason to get NoClassDefFoundError.


1 Answers

While it's possible that this is due to a classpath mismatch between compile-time and run-time, it's not necessarily true.

It is important to keep two or three different exceptions straight in our head in this case:

  1. java.lang.ClassNotFoundException This exception indicates that the class was not found on the classpath. This indicates that we were trying to load the class definition, and the class did not exist on the classpath.

  2. java.lang.NoClassDefFoundError This exception indicates that the JVM looked in its internal class definition data structure for the definition of a class and did not find it. This is different than saying that it could not be loaded from the classpath. Usually this indicates that we previously attempted to load a class from the classpath, but it failed for some reason - now we're trying to use the class again (and thus need to load it, since it failed last time), but we're not even going to try to load it, because we failed loading it earlier (and reasonably suspect that we would fail again). The earlier failure could be a ClassNotFoundException or an ExceptionInInitializerError (indicating a failure in the static initialization block) or any number of other problems. The point is, a NoClassDefFoundError is not necessarily a classpath problem.

like image 52
Jared Avatar answered Sep 22 '22 14:09

Jared