Programming for fun and profit

Programming tutorials, problems, solutions. Always with code.

Java UnsupportedClassVersionError


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


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

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(
      at java.lang.ClassLoader.defineClass(
      at Method)
      at java.lang.ClassLoader.loadClass(
      at sun.misc.Launcher$AppClassLoader.loadClass(
      at java.lang.ClassLoader.loadClass(
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!