Categories
incompatibility java jvm unsupported-class-version

How to fix java.lang.UnsupportedClassVersionError: Unsupported major.minor version

1685

I am trying to use Notepad++ as my all-in-one tool edit, run, compile, etc.

I have JRE installed, and I have setup my path variable to the .../bin directory.

When I run my “Hello world” in Notepad++, I get this message:

java.lang.UnsupportedClassVersionError: test_hello_world :
 Unsupported major.minor version 51.0
    at java.lang.ClassLoader.defineClass1(Native Method)
    at java.lang.ClassLoader.defineClassCond(Unknown Source)
       .........................................

I think the problem here is about versions; some versions of Java may be old or too new.

  1. How do I fix it?
  2. Should I install the JDK, and setup my path variable to the JDK instead of JRE?
  3. What is the difference between the PATH variable in JRE or JDK?

10

  • did you install jdk as well ? jre is Java RunTime environment , To create java apps , you need jdk as well

    – CyprUS

    Apr 30, 2012 at 11:30

  • yes i did, and now my path variable points to where jdk is. yet i still have this “unsupported minor version 51.0” error do you think i should install older version of jdk? i m using jdk 1.7.0

    – ERJAN

    May 1, 2012 at 8:30


  • 20

    This happened to me on Mac OS X when I was having problems due to the weird Apple java 6 and Oracle java 7 situation on OSX these days. short version: Compile with JDK7, Run with JDK6, get this.

    – Warren P

    Oct 7, 2013 at 4:00

  • 7

    In java ClassName should Be Start with Capital letter 😉

    Feb 3, 2014 at 16:47

  • 6

    For El Captain, this answer fixed it for me: stackoverflow.com/a/34201991/1615594

    – Taku

    Jan 24, 2016 at 11:35

2067

The version number shown describes the version of the JRE the class file is compatible with.

The reported major numbers are:

Java SE 17 = 61,
Java SE 16 = 60, 
Java SE 15 = 59,
Java SE 14 = 58,
Java SE 13 = 57,
Java SE 12 = 56,
Java SE 11 = 55,
Java SE 10 = 54,
Java SE 9 = 53,
Java SE 8 = 52,
Java SE 7 = 51,
Java SE 6.0 = 50,
Java SE 5.0 = 49,
JDK 1.4 = 48,
JDK 1.3 = 47,
JDK 1.2 = 46,
JDK 1.1 = 45

(Source: Wikipedia)

To fix the actual problem you should try to either run the Java code with a newer version of Java JRE or specify the target parameter to the Java compiler to instruct the compiler to create code compatible with earlier Java versions.

For example, in order to generate class files compatible with Java 1.4, use the following command line:

javac -target 1.4 HelloWorld.java

With newer versions of the Java compiler you are likely to get a warning about the bootstrap class path not being set. More information about this error is available in a blog post New javac warning for setting an older source without bootclasspath.

7

  • 177

    “The version number shown describe which version if Java was used to compile the code.” No it does not. It shows the version of the JRE that the class file is compatible with. Using cross-compilation options you can use a 1.7 JDK to compile code with a class version of 1.1 (through 1.7).

    Jan 10, 2013 at 5:31


  • 8

    This is a nice solution for compiling to a previous version from Eclipse directly: stackoverflow.com/a/16345710/2091700

    – Alphaaa

    May 23, 2013 at 14:03

  • 19

    IN eclipse project=>Proerties=>java compiler; set “the compiler compliance level” to the version according to your jre or jdk. As for me, it’s 1.6 for jre 1.6

    – Sam

    Sep 25, 2013 at 14:58


  • 6

    This may occur when compiler source is set in non-compliance with currently compiling JRE/JDK. For example i found the below values from eclipse .settings folder, org.eclipse.jdt.core.compiler.compliance=1.7, org.eclipse.jdt.core.compiler.source=1.7, org.eclipse.jdt.core.compiler.codegen.targetPlatform=1.7. And my compiler was 1.6.0_32. The problem was resolved after changing the values to 1.6. The issue originated after i copied the project from a different workspace where i was using JDK 1.7 🙁

    – Gana

    Dec 22, 2014 at 6:22


  • On eclipse, right click on the project -> click properties -> click Java Build path on the left panel -> Click Libraries tab -> change the ‘JRE System Library’ to the right version

    Jan 27, 2017 at 19:25

389

java.lang.UnsupportedClassVersionError happens because of a higher JDK during compile time and lower JDK during runtime.

8

  • 2

    How can i make above change?

    Aug 14, 2013 at 12:57

  • 9

    Project -> Properties -> Java Compiler Enable project specific settings. Then select Compiler Compliance Level to 1.7, 1.6 or 1.5, build and test your app.

    Aug 16, 2013 at 9:55

  • When I moved to Java 7, somehow Eclipse kept trying to run in Java 6…no idea why. Uninstalled the old JDK did the trick for me.

    – Dave

    Aug 19, 2013 at 15:22

  • No. Compile on 7 for 1.1, run on 1.4.

    – nanofarad

    Sep 9, 2013 at 19:16

  • 9

    @ezdazuzena use sudo update-alternatives --config java to do so.

    Jan 29, 2014 at 10:06

124

In Eclipse, I just went to menu command Window -> Preferences -> Java -> Compiler and then set “Compiler compliance level” to 1.6.

2

  • 3

    In eclipse, sometime project facets also need to be changed to appropriate java version.

    Jul 4, 2013 at 7:59

  • 6

    Thanks. It’s worth noting that setting Eclipse’s default execution environment (the JRE that goes on the build path) is a separate setting. I had set the JRE but not the compiler compliance level; this fixed it.

    – wchargin

    Jan 26, 2014 at 1:52