Java, UnsupportedClassVersionError. How can I fix this

14,674

Solution 1

The Test.class file has been compiled in Java 7 (major/minor version 51.0), so it's incompatible with the Java 6 (major/minor version 50.0) runtime. Either compile the .java file in Java 6 (or earlier), or run the .class in a Java 7 runtime.

Solution 2

Maybe your Compiler in eclipse is different? Preferences -> Compiler: Compiler level. Maybe Java 7?

If you are under Linux, you can have a look for all your installed runtime environments: update-alternatives --config java. Here you can choose the correct one. Here you should be able to find the OpenJDK 7.

Solution 3

51.0 indicates Java version 7, so the class file you're trying to run was compiled with a version 7 compiler. If you need to run the code with a version 6 JVM you should instruct the compiler to emit version 6 compatible byte code.

javac -version 6 ...

That command line argument will force a higher version compiler to restrict its output to bytecode that's compatible with a version 6 runtime environment.

Solution 4

Is it possible you've compiled your Test program Java 7 and are now attempting to run it against Java 6 in the terminal? I would try recompiling in the terminal (ie. Java 6) if that's the case and then attempt to re-run the program.

Solution 5

In Eclipse, go to Window-->Preferences-->Java-->Compiler and you will see a field labeled "Compiler compliance level". Set it to 1.6, and recompile in Eclipse.

There is a Java version mismatch between Eclipse and your command-line javac. Specifically, your javac seems to be using 64-bit 1.6. Eclipse apparently is using 1.7.

Share:
14,674
vedran
Author by

vedran

Nothing special

Updated on June 06, 2022

Comments

  • vedran
    vedran almost 2 years

    I was doing my homework in eclipse and it reported no errors, not even warnings. When I tried to compile it from terminal I got following error. It runs and compiles just fine with eclipse. I take it it has something to do with java version? Anyway to fix it or try to bypass it?

    vedran@vedran-debian:~/java/oop/Aufgabe6$ java Test 
    Exception in thread "main" java.lang.UnsupportedClassVersionError: Test : Unsupported major.minor version 51.0
        at java.lang.ClassLoader.defineClass1(Native Method)
        at java.lang.ClassLoader.defineClass(ClassLoader.java:634)
        at java.security.SecureClassLoader.defineClass(SecureClassLoader.java:142)
        at java.net.URLClassLoader.defineClass(URLClassLoader.java:277)
        at java.net.URLClassLoader.access$000(URLClassLoader.java:73)
        at java.net.URLClassLoader$1.run(URLClassLoader.java:212)
        at java.security.AccessController.doPrivileged(Native Method)
        at java.net.URLClassLoader.findClass(URLClassLoader.java:205)
        at java.lang.ClassLoader.loadClass(ClassLoader.java:321)
        at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:294)
        at java.lang.ClassLoader.loadClass(ClassLoader.java:266)
    Could not find the main class: Test. Program will exit.
    

    Java version:

    java version "1.6.0_23" 
    OpenJDK Runtime Environment (IcedTea6 1.11pre) (6b23~pre11-1) 
    OpenJDK 64-Bit Server VM (build 20.0-b11, mixed mode) 
    

    EDIT:

    Thank you all for your explanations. It seems to be a java6/7 issue. I just compiled it with 1.6 and it worked like charm.

  • Dennis
    Dennis over 12 years
    This should not be a problem, or? I mean, I can run my 1.5 programs with my 1.6.
  • speedRS
    speedRS over 12 years
    But can you run 1.6 programs with your 1.5 runtime? I think that's the issue here - backwards compatibility, unless you go with the suggestions to emit 1.6 compatible byte code. I could be wrong though so happy to be corrected.
  • vedran
    vedran over 12 years
    @Dennis under normal circumstances yes, but as always there are some exceptions. This one for example :)
  • Travis Webb
    Travis Webb over 12 years
    @vedran is wrong. You can never run 1.7-compiled programs with a 1.6 runtime. You can run 1.5 programs because the featureset of 1.5 is a subset of 1.6, whereas the featureset of 1.7 is a superset of 1.6.
  • Dennis
    Dennis over 12 years
    @speedRS correct, he compiled against 7 and runs with 6. I was reading the opposite in your comment. Sorry.
  • vedran
    vedran over 12 years
    @TravisWebb Never? That seems a bit odd. I only recently installed 1.7, but this was the first time I tried to run it jre1.6 ... I thought it was due to a custom made collectors.
  • Puce
    Puce over 12 years
    Note however that in this case you have to make sure that you are only calling Java SE 6 API. Here some tips for cross-compilation: docs.oracle.com/javase/7/docs/technotes/tools/windows/… The easiest way to cross compile is probably to compile it once with JDK 6 and once with JDK 7 (if needed), each by setting up accordant jobs on your contiuous integration server (such as Jenkins),
  • Dennis
    Dennis over 12 years
    If you're using -version 5 with the Java 6 compiler, you can get troubles (I don't remember what exactly. It was some years ago).
  • Travis Webb
    Travis Webb over 12 years
    Why is it odd? If you're trying to use a newer compiler than your runtime environment, then you're doing it wrong. JDKs are forward compatible, JREs are backward compatible. This is by design.
  • vedran
    vedran over 12 years
    @TravisWebb And I'm a second year student and far from en expert. I though simple stuff could work in 1.6 tough...
  • joelpet
    joelpet over 11 years
    I had some trouble compiling Java 6 compatible class files despite running update-alternatives to use Java 6 installation for all Java related commands. Then I realized that I had set the JAVA_HOME environment variable (in /etc/environment) to my Java 7 installation. Changing that to the corresponding directory for Java 6 fixed the problem.