Exception in thread main – Java

Filed Under: Java

Being a Java Programmer, you must have seen exception in thread main sometimes while running a java program.

Exception in thread main

If you are using Java IDE like Eclipse or Netbeans to run a java program, you might not face some of these issues because IDE takes care of running the class with proper syntax and correct command.

Here I am explaining some common java exception in thread main exceptions you will see while running a java program from the terminal.

1. Exception in thread main java.lang.UnsupportedClassVersionError

This exception comes when your java class is compiled from another JDK version and you are trying to run it from another java version. Let’s understand this with a simple example.

package com.journaldev.util;

public class ExceptionInMain {

	public static void main() {


When I created the project in Eclipse, I kept JRE version as Java 7 but in my terminal java version is Java 1.6. Because of Eclipse IDE JDK settings, the class file generated is compiled with Java 1.7.
Now when I try to run this class from the terminal, I get the following exception message.

pankaj@Pankaj:~/Java7Features/bin$java com/journaldev/util/ExceptionInMain
Exception in thread "main" java.lang.UnsupportedClassVersionError: com/journaldev/util/ExceptionInMain : Unsupported major.minor version 51.0
	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)

If I run the class with Java 1.7, I won’t get this exception. The reason for this exception is that we can’t compile a java source file from higher version and then run it on lower version of JRE.

2. Exception in thread main java.lang.NoClassDefFoundError

There are two variants of this exception. The first one is where you provide the class full name, remember that when running a Java Program, you just need to give the class name and not the extension.

Notice that the .class in the command below to run the program causes NoClassDefFoundError. The reason for this error is when java is unable to find the class file to execute.

pankaj@Pankaj:~/CODE/Java7Features/bin$java com/journaldev/util/ExceptionInMain.class
Exception in thread "main" java.lang.NoClassDefFoundError: com/journaldev/util/ExceptionInMain/class
Caused by: java.lang.ClassNotFoundException: com.journaldev.util.ExceptionInMain.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)

The second type of exception is thrown when Class is not found.

pankaj@Pankajs-MacBook-Pro:~/CODE/Java7Features/bin/com/journaldev/util$java ExceptionInMain
Exception in thread "main" java.lang.NoClassDefFoundError: ExceptionInMain (wrong name: com/journaldev/util/ExceptionInMain)
	at java.lang.ClassLoader.defineClass1(Native Method)
	at java.lang.ClassLoader.defineClass(ClassLoader.java:791)
	at java.security.SecureClassLoader.defineClass(SecureClassLoader.java:142)
	at java.net.URLClassLoader.defineClass(URLClassLoader.java:449)
	at java.net.URLClassLoader.access$100(URLClassLoader.java:71)
	at java.net.URLClassLoader$1.run(URLClassLoader.java:361)
	at java.net.URLClassLoader$1.run(URLClassLoader.java:355)
	at java.security.AccessController.doPrivileged(Native Method)
	at java.net.URLClassLoader.findClass(URLClassLoader.java:354)
	at java.lang.ClassLoader.loadClass(ClassLoader.java:423)
	at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:308)
	at java.lang.ClassLoader.loadClass(ClassLoader.java:356)
	at sun.launcher.LauncherHelper.checkAndLoadMain(LauncherHelper.java:480)

Note that ExceptionInMain class is in the package com.journaldev.util, so when Eclipse compiles this class, it’s placed inside <Project_Classes_Directory>/com/journaldev/util and hence class is not found causing this error message.

3. Exception in thread main java.lang.NoSuchMethodError: main

This exception comes when you are trying to run a class that doesn’t have main method. In Java 7, the error message is changed to make it more clear.

pankaj@Pankaj:~/CODE/Java7Features/bin$ java com/journaldev/util/ExceptionInMain
Error: Main method not found in class com.journaldev.util.ExceptionInMain, please define the main method as:
   public static void main(String[] args)

Read more at java.lang.NoSuchMethodError

4. Exception in thread “main” java.lang.ArithmeticException

Whenever an exception is thrown from the main method, it prints the exception in the console. The first part explains that exception is thrown from the main method, second part prints the exception class name and then after a colon, it prints the exception message.

For example, if I change the initial class print statement to System.out.println(10/0);, it will throw ArithmeticException.

Exception in thread "main" java.lang.ArithmeticException: / by zero
	at com.journaldev.util.ExceptionInMain.main(ExceptionInMain.java:6)

How to fix java exception in thread main?

Above are some of the common java exceptions in thread main, whenever you face any one of these check following:

  1. Same JRE version is used to compile and run the java program
  2. You are running java class from the classes directory and package is provided as directory.
  3. Your java classpath is set properly to include all the dependency classes
  4. You are using only file name without .class extension while running a java program
  5. Java class main method syntax is correct

Further Reading: Exception Handling in Java.

Did you noticed that in all the above exceptions, it’s mentioned as “thread main”. It’s because java main method is the first thread in java. Read more at multithreading in java.


  1. Sidharth Raj says:

    This is a life saver for all who are new to JAVA,
    Thanks for this

  2. Vanessa Wong says:

    This does not work anymore since JDK 11.0.1 does not have any compatible JRE package to be downloaded.

  3. InnerPeace says:

    Tusi Great ho Sir!!!!

  4. Naresh says:

    Good Artical ,very interesting, Please post more like same.

  5. amit rathod says:

    thank you very nice understandable article

  6. laiixntao says:

    thank you.I have learned a lot from this article.

Comments are closed.

Generic selectors
Exact matches only
Search in title
Search in content