Java Programming Tutorials

Java programming tutorials with many code examples!

Java UnsupportedClassVersionError

Problem:

What means java.lang.UnsupportedClassVersionError: Unsupported major.minor version 52.0 and how to deal with it?

Solution:

The problem is that a class has been compiled with newer Java version and you try to run it with some older JVM.

In the following example I’ll create a “Hello world” program, compile it with Java 8 and then will try to run on Java 6, which will result in the error.

Here’s a sample class:

public class JavaSolved {
  public static void main(String[] args) {
      System.out.println("Hello, world!");
  }
}

Let’s compile it using Java 8:

$> /usr/lib/jvm/java-8-oracle/bin/javac JavaSolved.java

And try to run it using Java 6:

$> /usr/lib/jvm/java-6-oracle/bin/java -cp . JavaSolved
Exception in thread "main" java.lang.UnsupportedClassVersionError: JavaSolved : Unsupported major.minor version 52.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)
Could not find the main class: JavaSolved.  Program will exit.

Of course the version after Unsupported major.minor version is different for Java versions, but the problem is the same.

There are two solutions to this problem:

  1. Run compiled code with newer version of JVM.
  2. Compile code with javac -target parameter set to the same version as JVM you would like to use for running the compiled code.
Share with the World!