Java Exception in thread main Understanding with Examples

Being a Java Programmer, you must have seen “java exception in thread main” sometimes while running a java program. 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 common java exception in thread main exceptions you will see while running a java program from terminal.

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() {
		System.out.println(10);
	}

}

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

pankaj@Pankajs-MacBook-Pro:~/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.

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 .class. Note the .class in the command to run the program in below exception stack trace.

pankaj@Pankajs-MacBook-Pro:~/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 /com/journaldev/util and hence class is not found causing this exception message.

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@Pankajs-MacBook-Pro:~/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)

Exception in thread “main” java.lang.ArithmeticException

Whenever any exception is thrown from main method, it prints the exception is console. The first part explains that exception is thrown from 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 this exception.

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

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

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

Further Reading: Java Exception Handling

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

Current day month ye@r *