Java

What is java lang NoClassDefFoundError?

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.

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 [email protected]:~$ directory. In the directory, dir1, the java source file, TheClass.java, has the main Java class, TheClass. Now, at the command prompt, [email protected]:~$, the programmer compiles the source file, TheClass.java, with the command:

    javac dir1/TheClass.java

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:

    java dir1/TheClass

at the terminal, he would receive the error message:

    Error: Could not find or load main class dir1.TheClass
    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:

    java dir1/TheClass.class

If he did that, he would get the error message:

    Error: Could not find or load main class dir1.TheClass.class
    Caused by: java.lang.ClassNotFoundException: dir1.TheClass.class

This article is on NoClassDefFoundError, and so ClassNotFoundException would not be addressed. The command,

    java dir1/TheClass

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:

    cd dir1
    java TheClass

Missing Bytecode class Scenario

In this section, the directory [email protected]:~/dir1$, will be used exclusively. Consider the following Java program:

    class AClass {
    }

    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, [email protected]:~/dir1$. The following command will compile the file:

    user@hostName:~/dir1$ javac MainClass.java

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:

    user@hostName:~/dir1$ java MainClass

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:

    user@hostName:~/dir1$ javac TheClass.java

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:

    user@hostName:~/dir1$ java TheClass

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:

    Exception in thread "main" java.lang.NoClassDefFoundError: AClass
        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

    user@hostName:~/dir1$ javac TheClass.java

and a new Aclass.class in the directory, dir1, would be created. And the command,

    user@hostName:~/dir1$ java TheClass

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.

About the author

Chrysanthus Forcha

Discoverer of mathematics Integration from First Principles and related series. Master’s Degree in Technical Education, specializing in Electronics and Computer Software. BSc Electronics. I also have knowledge and experience at the Master’s level in Computing and Telecommunications. Out of 20,000 writers, I was the 37th best writer at devarticles.com. I have been working in these fields for more than 10 years.