3

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.

6
  • Eghh, OpenJDK? Do you need to use that? Otherwise I highly recommend using a real java JDK.
    – Nate W.
    Commented Dec 2, 2011 at 0:36
  • 1
    @Shakedown why? they're equivalent, and that isn't his problem. Commented Dec 2, 2011 at 0:38
  • 1
    Isn't OpenJDK 7 the new real (Sun/Oracle) JDK? And OpenJDK 6 was something in development between Oracle 6 and OpenJDK 7?
    – Dennis
    Commented Dec 2, 2011 at 0:44
  • They're definitely not equivalent...if they were then why would it exist. Chances are you're right that it doesn't have anything to do with this, but I've ran into difficult bugs that were ultimately caused by using OpenJDK.
    – Nate W.
    Commented Dec 2, 2011 at 0:56
  • OpenJDK 6 and Oracle 6 are/were not the same. Correct. I was having problems as well. Therefore I didn't touch OpenJDK 7 until now.
    – Dennis
    Commented Dec 2, 2011 at 0:59

6 Answers 6

6

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.

3

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.

1
  • 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.
    – joelpet
    Commented Nov 27, 2012 at 7:35
2

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.

2
  • 1
    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),
    – Puce
    Commented Dec 2, 2011 at 0:53
  • 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).
    – Dennis
    Commented Dec 2, 2011 at 0:54
1

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.

1

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.

8
  • This should not be a problem, or? I mean, I can run my 1.5 programs with my 1.6.
    – Dennis
    Commented Dec 2, 2011 at 0:41
  • 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.
    – speedRS
    Commented Dec 2, 2011 at 0:43
  • @Dennis under normal circumstances yes, but as always there are some exceptions. This one for example :)
    – vedran
    Commented Dec 2, 2011 at 0:44
  • 2
    @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. Commented Dec 2, 2011 at 0:47
  • 1
    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. Commented Dec 2, 2011 at 0:56
0

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.