ERJAN ERJAN - 7 months ago 16
Java Question

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

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 version 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?


Answer

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

The reported major numbers are:

J2SE 8 = 52,
J2SE 7 = 51,
J2SE 6.0 = 50,
J2SE 5.0 = 49,
JDK 1.4 = 48,
JDK 1.3 = 47,
JDK 1.2 = 46,
JDK 1.1 = 45

(Source: http://en.wikipedia.org/wiki/Java_class_file)

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 blog post New javac warning for setting an older source without bootclasspath.