Fix Java NoClassDefFoundError Error

Rashmi Patidar Jan 14, 2022 Nov 25, 2021
Fix Java NoClassDefFoundError Error

In the Java language, errors are anomalies that mainly occur due to a lack of resources. Additionally, errors cannot be caught at the compile time and occur at run time.

The errors are also called unchecked exceptions. One cannot check if any error can occur or not even the compilation gets done successfully.

The variety of errors that are present under Error class are AssertionError, NoClassDefFoundError, OutOfMemoryError, etc.

The NoClassDefFoundError is a sub-type of the Error class that says or denotes the Java Runtime Environment or (JRE) cannot load the class definition, which was present at the time of compilation but is not available at runtime. The Error class resides inside the javas.lang package.

The NoClassDefFoundError class resides under LinkageError. The Error is a type of linkage issue that comes into action or encounters when we do not use proper IDE (Integrated Development Environment) like IntelliJ IDEA, Eclipse, etc., and use the console to run the program.

The IDE’s work in a way that compiles the classes that are dependent on the main class beforehand. But in a console or terminal, it is the responsibility of the user to compile all the dependent classes and then run the main class, thereby reducing the risk of the linkage type error.

Let’s understand the error using a Java code block.

public class NoClassDefinitionError {
    public static void main(String[] args) {
        A a = new A();;

class A {
    void show() {
        System.out.println("In class A");

In the above code block, create a Java class file with the NoClassDefinitionError name. Save the file with the .java extension.

Then, open the class in the notepad, copy and paste the code block into the class file. Open a command prompt, and go to the location where the file gets saved.

Run the first command to compile the code block javac NoClassDefinitionError command.

The command requires the PATH environment variable to be set by default before using the javac command. Make sure the PATH variable has java’s bin location already added beforehand.

Else it will give an error as javac gets not recognized as an internal or external command, operable program, or batch file issue.

The javac command allows the compiler to create a .class file that is nothing but the byte code version of the java file. This file is easily understandable by Java Virtual machine or JVM to run the program.

Now, reach the location where the .java file gets saved. It seems easy to locate the .class file, which gets created after running the javac command. Manually remove the A.class file from the location.

Try running command java NoClassDefinitionError.

The java command internally runs the Java application by loading all the dependent class files and searching for the main method. When it is unable to find the class definition, it shows up runtime exception.

Below is a small code of error block that gets populated when this Error comes.

Exception in thread "main" java.lang.NoClassDefFoundError: A
	at NoClassDefinitionError.(

Various resolutions to eradicate the problem when we encounter any error is listed below.

  • Check for the class name that gets to run is at the exact and desired location.
  • Check if .class files are present for the java class that one is trying to run. If the class file is not present then, first compile the other class and get the class file to generate.
  • Check for transitive dependencies and compile the classes that get needed to run the main class file. A->B and B-> C In this case, A is transitive dependent on the C class. The transitive dependency says an indirect relationship that exists among three different files that are functionally dependent on one another.
  • Direct use of the IDE environment reduces chances of avoiding noClassDefFoundError issues, as the dependency resolution and transitive dependencies will be handled before the actual run.
  • Examine the stack trace properly, as it gives a proper understanding of where the issue starts populating and its impact in the code-flow from what all places it gets thrown. The stack trace should help in getting the issue resolved.
  • Add the location where the Java command runs into the CLASSPATH variable. The variable helps locate the path where the class files are to be picked and loaded when the program runs in Java Runtime Environment.
Rashmi Patidar avatar Rashmi Patidar avatar

Rashmi is a professional Software Developer with hands on over varied tech stack. She has been working on Java, Springboot, Microservices, Typescript, MySQL, Graphql and more. She loves to spread knowledge via her writings. She is keen taking up new things and adopt in her career.


Related Article - Java Error