12

My JDK version is jdk1.8.0_45 and I have built an application using the default JDK Compliance setting in eclipse.

But my colleague is having a JRE with version jre1.8.0_20. So when my application is run in his system, it is throwing an

java.lang.UnsupportedClassVersionError Unsupported major.minor version 52.0

If I build the application with JDK compliance set to 1.7, it works fine obviously. But I don't want to lose all the features of JDK 8 just for a minor version mismatch.

And in eclipse I am not able to set a minor version compliance level. Is it possible to set it???

Or is Java forward compatible with minor version releases?? If so, obviously this error should not occur I believe.

Codebender
  • 14,221
  • 7
  • 48
  • 85
  • this is strange. ` I have built an application using the default JDK Compliance setting in eclipse` that was which version? Are you sure your colleague is using jre8? have you checked the output of `java -version`? – Paizo Jul 03 '15 at 13:45
  • @Paizo, My JDK is `1.8.0_45` and my colleague's version is `1.8.0_20`. I have specified all the information in the question... – Codebender Jul 03 '15 at 13:51

8 Answers8

6

According to this Version 52.0 is JDK-8. It seems that its a Number compiled into the .class files depending on the JDK. JDK-7 has 51, JDK-6 has 50 etc.

I would state the execution on your collegue's machine didn't use a JDK/JRE 8.

Try running java -XshowSettings on your collegues pc and check the output of the line java.class.version - if its a JDK 7 it will show java.class.version = 51.0.

Community
  • 1
  • 1
hinneLinks
  • 3,673
  • 26
  • 40
  • The fact that the OP is getting a java.lang.UnsupportedClassVersionError (and not an Eclipse exception of some sort) supports this answer. – Erick G. Hagstrom Jul 05 '15 at 23:06
3

You should be specifying a runtime "Execution-environment" version dependency rather than a specific java version.

For a plugin, in the manifest.mf, specify execution environment: JavaSE-1.8

For a plain java project, in "Build Path...->Configure BuildPath" go to "Libraries" then "Add Library", "JRE System Library", "Execution Environment" and select JavaSE-1.8. Remove any other JRE on path.

This says to eclipse, find a Java >= 1.8 compliant JRE.

Yann TM
  • 1,942
  • 13
  • 22
  • It's also possible that your colleague has several JRE installed and configured in his eclipse (check in "Window->Preferences->Java->Installed JRE") , some of them older, and that the default JRE setting on his machine is in fact <= 1.7 (e.g. because it has JDK included for dev), even though he has more recent Java. – Yann TM Jul 03 '15 at 15:19
2
  1. Download JRE 1.8.0_20

  2. Then specify JRE 1.8.0_20 to Eclipse IDE, something like this:

    enter image description here

  3. Then re-build your application.

(Your problem isn't a bug: http://bugs.java.com/bugdatabase/view_bug.do?bug_id=8124517)

Vy Do
  • 46,709
  • 59
  • 215
  • 313
  • Good link to the java bug. But your answer won't solve the problem. If his colleague was really using JRE 1.8.0_20 it wouldn't throw this exception. Per your link: "This problem seems to be caused by running Eclipse Java 1.7 with the Java 1.8 jfxrt.jar." – Erick G. Hagstrom Jul 06 '15 at 21:14
1

Java is known for being backward compatible meaning that any application you build in an older jdk will be runnable in the newer jdk :

Building an app in jdk1.6 or jdk1.7 will work when running with jdk1.8 ; sadly it doesn't work the other way around ...

Minor releases are seen as new versions of jdk and the problems of compatilibity do apply .

Kibadachi
  • 155
  • 6
  • You could try using -target compiler flag that tells it to generate code that will run on an older (target) JVM and the -source compiler flag tells it to only accept the older JVM's language features. Note that this won't always work, depending on the Java language features used by your code. – Kibadachi Jul 01 '15 at 14:30
  • I am not able to give the whole version information in that... If you look at my question, both the JDK and JRE are version 1.8... Only difference is the last version part. If I give that in `-source` option it's giving **invalid source release option: 1.8.0_45** – Codebender Jul 01 '15 at 14:39
1

No way.

Just install jdk1.8.0_20 and select it in Installed JRE's.

andronix
  • 124
  • 9
1

The error only occurs when you have major version changes like from 6 to 7 or 7 to 8.It does not occur for minor version change as you have. And Error 52 is for running JDK8 code in JRE7. So double check settings and set JRE8 as runtime environment.

1

Me and my colleague face this when using x32 and x64 versions of JDK. He uses Mac, therefore only has x64 available (for Java 8), I use M$ and have both versions. We must develop on 64bit Versions, otherwise we see the same error as you do...

mambax
  • 1,079
  • 8
  • 17
  • That is interesting and weird since unless you have native code (JNI/JNA access or machine code compiled for a specific arcitechture) java code will run equally well in a 32-bit and 64-bit JVM. – Lonzak Jul 10 '15 at 12:10
  • Yes, our mentor of our work also could not tell. We had the problem a while ago (approx. 6 months back). We indeed used a third party library (which was only usable in x32 mode) but none the less we saw this error message more than once. It was generally a chaos back there with MacOSX which only allows x64 Java (except bundled) and libraries from stoneage allowing only x32, all after all we somehow managed to run it, although having to publish a guide how someone ever can start it :) – mambax Jul 10 '15 at 13:24
1

You can

(a) install 1.8.0_20 on your system and check the class version of the resulting class files with the code below or

(b) build your application on your colleague's system and check the resulting class version with the code below.

public void checkClassVersion(File classFile) throws IOException{

    DataInputStream in = new DataInputStream(new FileInputStream(file));

    int magic = in.readInt();
    if(magic != 0xcafebabe) {
        System.err.println(file.getName() + " is not a valid class!");;
    }
    int minor = in.readUnsignedShort();
    int major = in.readUnsignedShort();
    in.close();

    System.out.println(classFile.getName()+" = "+major+"."+minor);
}
Lonzak
  • 9,334
  • 5
  • 57
  • 88