This article illustrates what NoClassDefFoundError is and provides resolutions. It provides two important scenarios for the exception to be thrown and gives their respective resolutions.
Command Line Scenario
An example situation where this can occur is as follows: Assume that the directory dir1 exists in the user@hostName:~$ directory. In the directory, dir1, the java source file, TheClass.java, has the main Java class, TheClass. Now, at the command prompt, user@hostName:~$, the programmer compiles the source file, TheClass.java, with the command:
The compilation will go through successfully to have a bytecode file, TheClass.class, which would be produced in the dir1 directory. If the programmer then continues to run the file, TheClass.class, with the following command:
at the terminal, he would receive the error message:
Caused by: java.lang.NoClassDefFoundError: TheClass (wrong name: dir1/TheClass)
The programmer may think that this is because he did not write the whole bytecode filename at the command prompt. So he might try to run the program with the following command:
If he did that, he would get the error message:
Caused by: java.lang.ClassNotFoundException: dir1.TheClass.class
This article is on NoClassDefFoundError, and so ClassNotFoundException would not be addressed. The command,
is supposed to work, but it did not work. In the author’s opinion, the real problem in this situation is with the java language and not that of the programmer.
NoClassDefFoundError in Java occurs when Java Virtual Machine is not able to find a particular class at runtime. This can also happen within a running program – see below.
Resolution
To resolve this problem, go to the directory, dir1 and run the program from there, with the following commands, at the terminal, from the user directory:
java TheClass
Missing Bytecode class Scenario
In this section, the directory user@hostName:~/dir1$, will be used exclusively. Consider the following Java program:
}
public class MainClass {
public static void main(String[] args) {
AClass obj = new AClass();
}
}
Assume that this is in one file and saved with the name, MainClass.java in the directory, user@hostName:~/dir1$. The following command will compile the file:
The result will be two files, MainClass.java and MainClass.class, in the same directory, dir1. MainClass.java is the source file, and MainClass.class is the bytecode file. To run a program in Java, it is the byte code file that is run. The following command at the terminal will run the program:
Note that “.class” is not typed, though it is its file that is engaged. There should be no output because there is no print command in the program. There should be just the new command prompt, indicating that the MainClass class has successfully executed the program. That is how Java works.
Having the Classes as two File-Pairs
The above two classes can be saved as two different source files, with the names, Aclass.java and TheClass.java. Aclass.java would have the code for AClass, and TheClass.java would have the code for MainClass, with its filename renamed to TheClass.
When these two files are in the same directory, dir1, only TheClass.java has to be in the compilation command. It would integrate Aclass.java . The following command suffices:
In the directory, dir1, two new files will result: TheClass.class and Aclass.class . These are bytecode files. TheClass.class corresponds to TheClass.java and Aclass.class corresponds to TheClass.class.
Now, to run the program, only the TheClass.class file has to be commanded (without the extension, “.class”). It will integrate the bytecode file, Aclass.class. The following command suffices to run the class:
Like before, there is no output. The new command prompt should appear, showing that the program has been executed successfully.
NoClassDefFoundError in Java occurs when Java Virtual Machine is not able to find a particular class at runtime. This can also happen within a running program, as it is illustrated in this section.
Now, Aclass.class is an integral part of TheClass.class. In other words, TheClass.class cannot run without Aclass.class. So, if Aclass.class is deleted or renamed, NoClassDefFoundError would be thrown. The terminal error display, for the above command, would be:
at TheClass.main(TheClass.java:9)
Caused by: java.lang.ClassNotFoundException: AClass
at java.base/jdk.internal.loader.BuiltinClassLoader.loadClass(BuiltinClassLoader.java:581)
at java.base/jdk.internal.loader.ClassLoaders$AppClassLoader.loadClass(ClassLoaders.java:178)
at java.base/java.lang.ClassLoader.loadClass(ClassLoader.java:522)
... 1 more
Resolution
This problem can be resolved as follows: If the Aclass.class was transferred away from its directory, it should be brought back. If it was deleted, assuming that Aclass.java and Aclass.java were not deleted, then the program just needs to be recompiled, with
and a new Aclass.class in the directory, dir1, would be created. And the command,
would not issue the above long error message for NoClassDefFoundError.
Possibility of Recovering
NoClassDefFoundError is a runtime error, so it is not really up to the programmer to recover from it. As explained above, the best way to handle the problem is by resolution.
Conclusion
In the java.lang.* package, there is a class named, NoClassDefFoundError. Any class’s description is the class’s definition. NoClassDefFoundError stands for No Class Definition Found Error. This is thrown when the instance of the Java Virtual Machine (JVM) or a ClassLoader tries to load in the definition of a class, but no definition of the class could be found.
NoClassDefFoundError is a runtime error, so it is not really up to the programmer to recover from it. The best way to handle the problem is by resolution: Use the command line command to execute the command in the directory of interest. Replace any .class file; that is not where it was supposed to be.