vedran
vedran

Reputation: 2188

Java, UnsupportedClassVersionError. How can I fix this

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.

Upvotes: 3

Views: 15275

Answers (6)

SrimathyGanesh
SrimathyGanesh

Reputation: 9

If solutions above are all set , if u still have the same issue and if your using MAVEN then check in the pom.xml. JAVA ASSIST JAR should point 16.1-GA if your using jdk 1.6 else corresponding version should be added for the jdk that you are pointing to.(eg : 3.17.1 for jdk 7).For jdk 6 add the dependency with the following details

  1. groupId : org.javassist
  2. artifactId: javassist
  3. version : 3.16.1-GA

Upvotes: 0

speedRS
speedRS

Reputation: 1240

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.

Upvotes: 1

Dennis
Dennis

Reputation: 4177

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.

Upvotes: 3

Travis Webb
Travis Webb

Reputation: 15018

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.

Upvotes: 1

Kaleb Brasee
Kaleb Brasee

Reputation: 51925

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.

Upvotes: 6

fvu
fvu

Reputation: 32953

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.

Upvotes: 2

Related Questions