Monday, July 26, 2021

How to Fix Exception in thread "main" java.lang.NoClassDefFoundError: Running Java from Command line

The "Exception in thread "main" java.lang.NoClassDefFoundError: helloworldapp/HelloWorldApp" error comes when you are trying to run the HelloWorldApp Java program from the command line but either .class file is not there or Java is not able to find the class file due to incorrect classpath settings. The name of the class could be different in each case, it depends upon which class you are passing to java command for running from the command prompt. Another interesting thing to remember is that this error only comes in Java version less than or equal to Java 6 like Java 1.5 or Java 1.4, if you are running in JDK 7 or Java 8 instead of this you will see "Error: could not able to find or load class HelloWorldApp". Technically, both errors come due to some reason and their solution is also exactly the same.

Since many Java programmer now days starts programming in Java directly from powerful IDEs like Eclipse, Netbeans, or IntelliJ IDEA, they face this kind of error whenever they try to run the Java program from the command line directly or via a script e.g. batch file in Windows or .sh file in Linux.

So, what is the difference in running the same Java program from Eclipse and from the command prompt? the answer is classpath. Eclipse takes care of setting up the classpath for you but when it comes to the command line, It's your job to make sure the required class is in Classpath.

Unfortunately for beginners, the classpath is not an easy concept to grasp and it is also a source of many Java errors e.g. java.lang.ClassNotFoundException or java.lang.NoClassDefFoundError, hence good knowledge of classpath is a must for any Java developer. If you have not read already, I highly recommend reading my post on how classpath works in Java to understand it in a bit more detail.


Cause of Exception in thread "main" java.lang.NoClassDefFoundError: HelloWorldApp

In this tutorial, we will try to find out what causes "Exception in thread "main" java.lang.NoClassDefFoundError: helloworldapp/HelloWorldApp" when you run Java program from the command prompt and how to fix this error.

As I said this error comes when you try to run your Java program from the command line but either doesn't have the class file, maybe not created yet, or Java is not able to find that due to incorrect Classpath settings.

Now let's see what are some common Java beginner's mistake or simply learning experience which causes this "Exception in thread "main" java.lang.NoClassDefFoundError: HelloWorldApp".

It's not that intermediate and advanced Java programmer doesn't make mistakes, they do, but you will mostly see this error when you are trying to learn in Java the hard way i.e. using Notepad and command line.

If this is your first Java program then there could be three main reasons for getting this error:


1. Running without a fully qualified name

Your class is inside a package and you are not providing the fully qualified name to java command while running it from the command prompt. As per my experience of more than 10 years in Java, this is the most common cause of "Exception in thread "main" java.lang.NoClassDefFoundError: HelloWorldApp" error.

For beginners, it's seriously tricky to run a Java program from the command line due to classpath settings, it's even more tricky to run a class that is inside a package. Let's see  an example, suppose you have a class called HelloWorldApp inside a Java package called "beginner" in a working directory "C:\\practice" as shown below:

package beginner;

public class HelloWorldApp{

   public static void main(String... args){
       System.out.println("HelloWorld, why Classpath is so complex?");
   }

}

The easiest way to compile this Java source file is to go inside the "beginner" directory and execute javac HellWorldApp.java command, as shown below:

C:\practice\beginner>javac HelloWorldApp.java
C:\practice\beginner>dir         ..
07/04/2015  07:10 AM               469 HelloWorldApp.class
07/04/2015  07:10 AM               179 HelloWorldApp.java

You can see the class file is created in the same directory by the compiler. Now the most intuitive way to run this program is just typing "java HelloWorldApp" from the same directory because you have just compiled in the same way, all you are doing different is using "java" command instead of "javac", but unfortunately it will not work and throw "Exception in thread "main" java.lang.NoClassDefFoundError: HelloWorldApp".

C:\practice\beginner>C:\"Program Files"\Java\jdk1.6.0_45\bin\java.exe  
HelloWorldApp
Exception in thread "main" java.lang.NoClassDefFoundError: 
HelloWorldApp (wrong name: beginner/HelloWorldApp)
        at java.lang.ClassLoader.defineClass1(Native Method)
        at java.lang.ClassLoader.defineClassCond(ClassLoader.java:631)
        at java.lang.ClassLoader.defineClass(ClassLoader.java:615)
        at java.security.SecureClassLoader
             .defineClass(SecureClassLoader.java:141)
        at java.net.URLClassLoader.defineClass(URLClassLoader.java:283)
        at java.net.URLClassLoader.access$000(URLClassLoader.java:58)
        at java.net.URLClassLoader$1.run(URLClassLoader.java:197)
        at java.security.AccessController.doPrivileged(Native Method)
        at java.net.URLClassLoader.findClass(URLClassLoader.java:190)
        at java.lang.ClassLoader.loadClass(ClassLoader.java:306)
        at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:301)
        at java.lang.ClassLoader.loadClass(ClassLoader.java:247)
Could not find the main class: HelloWorldApp.  Program will exit.

Why did you get this error? because you need to pass the fully qualified name of the class to the "java" command which contains the main() method for execution. Since in this case, HelloWorldApp class is inside a package, its fully qualified name should be "beginner.HelloWorldApp".



2. Running java command from the incorrect directory

The second reason for "Exception in thread "main" java.lang.NoClassDefFoundError: HelloWorldApp" could be that your class is in the package and you are also running it with the fully qualified name but you are invoking java command from the incorrect directory. Now, you know that in order to run a Java class which is inside a package, you need to provide fully qualified name, let's try that by running command "java beginner.HelloWorldApp"

C:\practice\beginner>C:\"Program Files"\Java\jdk1.6.0_45\bin\java.exe  
beginner.HelloWorldApp
Exception in thread "main" java.lang.NoClassDefFoundError: 
beginner/HelloWorldApp
Caused by: java.lang.ClassNotFoundException: beginner.HelloWorldApp
        at java.net.URLClassLoader$1.run(URLClassLoader.java:202)
        at java.security.AccessController.doPrivileged(Native Method)
        at java.net.URLClassLoader.findClass(URLClassLoader.java:190)
        at java.lang.ClassLoader.loadClass(ClassLoader.java:306)
        at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:301)
        at java.lang.ClassLoader.loadClass(ClassLoader.java:247)
Could not find the main class: beginner.HelloWorldApp.  Program will exit.

hmm, we are still getting the error, but this time it's a little bit different. It's not complaining about the wrong name. So what is the problem? Can you spot? The problem is we are still running this program from the "beginner" directory and without any hint about classpath, Java is searching for beginner\HelloWorldApp.class in the current directory which is "beginner". Obviously, it won't find the class there.

The moral of the story, you must run this command from outside the package, let's go back to one directory up and run this command from the "practice" directory:

C:\practice>C:\"Program Files"\Java\jdk1.6.0_45\bin\java.exe  
beginner.HelloWorldApp
HelloWorld, why Classpath is so complex?

Now it ran fine. You can see, the program has printed the message written inside the main class i.e. "HelloWorld, why Classpath is so complex?".

In short, while running a Java class which is inside a package, you must provide the fully qualified name which is "package.classname" and run the java command just outside the package directory, because without any classpath hint Java search the class in the current directory and when you give the fully qualified name, it expects the class to be in a directory whose name is equal to package. In Java, a package represents a directory in the file system.



3. Incorrect Classpath using -classpath or -cp option

This error can also come when you are running your Java program with -classpath or -cp option but not including the directory where your class file is located.  For example, our class files are located inside the "C:\practice" directory, but when you run this program from "C:\" and give "java -cp. beginner.HelloWorldApp" then Java will only search the main class in the current directory and not in practice directory, hence it will throw the "Exception in thread "main" java.lang.NoClassDefFoundError: beginner/HelloWorldApp" as shown in following example.

C:\>"C:\Program Files\Java\jdk1.6.0_45\bin\java.exe" -cp 
. beginner.HelloWorldApp
Exception in thread "main" java.lang.NoClassDefFoundError: 
beginner/HelloWorldApp
Caused by: java.lang.ClassNotFoundException: beginner.HelloWorldApp
        at java.net.URLClassLoader$1.run(URLClassLoader.java:202)
        at java.security.AccessController.doPrivileged(Native Method)
        at java.net.URLClassLoader.findClass(URLClassLoader.java:190)
        at java.lang.ClassLoader.loadClass(ClassLoader.java:306)
        at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:301)
        at java.lang.ClassLoader.loadClass(ClassLoader.java:247)
Could not find the main class: beginner.HelloWorldApp.  Program will exit.

C:\>"C:\Program Files\Java\jdk1.6.0_45\bin\java.exe" -cp practice
  beginner.HelloWorldApp
HelloWorld, why Classpath is so complex?

As soon as you give the correct directory in the  -cp option program will start running fine.



4. Incorrect CLASSPATH environment variable


If you don't provide -cp or -classpath option while running your main class from command prompt then Java will search the class inside directories listed in the CLASSPATH environment variable.

If the directory like "C:\practice", where your .class files are stored is not listed there, Java will not found them and throw "Exception in thread "main" java.lang.NoClassDefFoundError: beginner/HelloWorldApp" error.

In short, if you are using the CLASSPATH environment variable then you must add the directory where all class files reside, mostly current directory or some specific directory.


5. Running a Java program without compiling

In this case, there is no .class file, hence you can't run the program and java command will throw "Exception in thread "main" java.lang.NoClassDefFoundError: Main class" error.  Let's test it by first deleting the .class file and running the same command again:

Exception in thread "main" java.lang.NoClassDefFoundError:  Running Java from Command line


6. Running a Java program with .class extension instead of the name

You can also get this error when you run your Java program by giving .class extension instead of just name e.g. instead of $java abc, you give $java abc.class. For example, if you run the program as shown in the following example, you will get the "Exception in thread "main" java.lang.NoClassDefFoundError: Main class" error, Caused by: java.lang.ClassNotFoundException: beginner.HelloWorldApp.class.

C:\practice>"C:\Program Files\Java\jdk1.6.0_45\bin\java.exe" 
beginner.HelloWorldApp.class
Exception in thread "main" java.lang.NoClassDefFoundError: 
beginner/HelloWorldApp/class
Caused by: java.lang.ClassNotFoundException: beginner.HelloWorldApp.class
        at java.net.URLClassLoader$1.run(URLClassLoader.java:202)
        at java.security.AccessController.doPrivileged(Native Method)
        at java.net.URLClassLoader.findClass(URLClassLoader.java:190)
        at java.lang.ClassLoader.loadClass(ClassLoader.java:306)
        at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:301)
        at java.lang.ClassLoader.loadClass(ClassLoader.java:247)
Could not find the main class: beginner.HelloWorldApp.class.  Program will exit.


7. Running Java program with .java file instead of just name

If you run the Java program by giving the name of your Java source file you will get the following error:

C:\practice\beginner>C:\"Program Files"\Java\jdk1.6.0_45\bin\java.exe  
HelloWorld.java
Exception in thread "main" java.lang.NoClassDefFoundError: HelloWorld/java
Caused by: java.lang.ClassNotFoundException: HelloWorld.java
        at java.net.URLClassLoader$1.run(URLClassLoader.java:202)
        at java.security.AccessController.doPrivileged(Native Method)
        at java.net.URLClassLoader.findClass(URLClassLoader.java:190)
        at java.lang.ClassLoader.loadClass(ClassLoader.java:306)
        at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:301)
        at java.lang.ClassLoader.loadClass(ClassLoader.java:247)
Could not find the main class: HelloWorld.java.  Program will exit.


That's all about how to solve "Exception in thread "main" java.lang.NoClassDefFoundError: HelloWorld/java" error in Java.  Here class could be anything, basically, the main class which you are trying to run from the command line. I will keep this post updated with the new information arises due to change in every Java version.  If you are a beginner, I also suggest joining these free Java courses to improve your fundamentals.

Here is the snapshot of how we manage to run a Java program from the command line successfully:

Solving "Exception in thread "main" java.lang.NoClassDefFoundError: HelloWorld/java" error in Java




Other Java troubleshooting guides you may like:
  • How to solve java.lang.ClassNotFoundException: oracle.jdbc.driver.OracleDriver in Java? [solution]
  • 2 Reasons of org.springframework.beans.factory.BeanCreationException: Error creating bean with name [Solution]
  • How to fix java.lang.ClassNotFoundException: org.postgresql.Driver error in Java? [solution]
  • SQLServerException: The index 58 is out of range - JDBC [solution]
  • Spring - java.lang.ClassNotFoundException: org.springframework.web.context.ContextLoaderListener [solution]
  • How to fix java.lang.ClassNotFoundException: com.microsoft.sqlserver.jdbc.SQLServerDriver in Java? [solution]
  • java.sql.SQLException: No suitable driver found for 'jdbc:mysql://localhost:3306/mysql [Solution]
  • java.lang.ClassNotFoundException: com.mysql.jdbc.Driver [Solution]
  • java.lang.ClassNotFoundException: org.springframework.web.servlet.DispatcherServlet in Java Spring MVC Application [solution]
  • 3 ways to solve "No JVM Installation Found. Please install a 64-bit JDK" error [guide]
  • How to solve OutOfMemoryError in Eclipse? [guide]
  • How to solve javax.net.ssl.SSLHandshakeException: unable to find valid certification path to requested target? [solution]
  • Solving org.springframework.beans.factory.BeanCreationException: Error creating bean with name [Solution]
  • Solving java.sql.SQLException: No suitable driver found for jdbc:mysql://localhost:3306/test [steps]
  • java.lang.numberformatexception for input string null - Cause and Solution [guide]
  • Maven Eclipse Error - "No compiler is provided in this environment. Perhaps you are running on a JRE rather than a JDK" [guide]
  • Minecraft - java.lang.UnsatisfiedLinkError: lwjgl64.dll : Access Denied Solution [solution]
  • Solving java.lang.ArrayindexOutOfBoundsException: 1 in Java  [guide]
  • java.lang.ClassNotFoundException: com.sun.jersey.spi.container.servlet.ServletContainer [Solution]

Thanks for reading this article so far. If you find this Java troubleshooting guide useful then please share it with your friends and colleagues.

2 comments: