Exception in thread main - Java With Examples

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.

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.

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.

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

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.

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.

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.

By admin

Leave a Reply

%d bloggers like this: