开发者

Running JVM 6 compiled code on JVM 5 or lower

开发者 https://www.devze.com 2023-02-10 14:13 出处:网络
What is the effect of executing a class compil开发者_Go百科ed to byte code on a JVM Version 6 and execution the same on a lower version JVM (say 1.4)?Not sure what you\'re asking really, but if you co

What is the effect of executing a class compil开发者_Go百科ed to byte code on a JVM Version 6 and execution the same on a lower version JVM (say 1.4)?


Not sure what you're asking really, but if you compile a Java source file targeting Java 6, it will not execute on an older JVM (say, 1.4).

: ~/tmp > javac -version
javac 1.6.0_22
: ~/tmp > javac Test.java 
: ~/tmp > java Test
Hello World
: ~/tmp > module add jdk/1.4.2    # switching to Java 1.4.2
: ~/tmp > java Test
Exception in thread "main" java.lang.UnsupportedClassVersionError:
                Test (Unsupported major.minor version 50.0)
        at java.lang.ClassLoader.defineClass0(Native Method)
        at java.lang.ClassLoader.defineClass(ClassLoader.java:539)
        at java.security.SecureClassLoader.defineClass(ClassLoader.java:123)
        at java.net.URLClassLoader.defineClass(URLClassLoader.java:251)
        at java.net.URLClassLoader.access$100(URLClassLoader.java:55)
        at java.net.URLClassLoader$1.run(URLClassLoader.java:194)
        at java.security.AccessController.doPrivileged(Native Method)
        at java.net.URLClassLoader.findClass(URLClassLoader.java:187)
        at java.lang.ClassLoader.loadClass(ClassLoader.java:289)
        at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:274)
        at java.lang.ClassLoader.loadClass(ClassLoader.java:235)
        at java.lang.ClassLoader.loadClassInternal(ClassLoader.java:302)
: ~/tmp > 

As pointed out by @The-MeLLeR however, you can still use the 1.6 compiler to compile to 1.4 compatible code, by using the -target option. However keep in mind that this only works if you're not using any 1.5/1.6 specific features or API classes.

If you for instance try to compile a for-each loop, with -target 1.4 you'll get something similar to the following message:

Test.java:3: ';' expected
        for (int i : new int[] { 1, 2, 3 })
                   ^


If you try to use classes which have been compiled for a newer JVM than what the current JVM supports, the JVM will throw an UnsupportedClassVersionError when it tries to load the class. Newer class formats may have features which are not support by the older JVMs, so this is necessary.

It is possible to use javac's -target option to make a newer JDK produce classes using and older class format. But you will still need to be careful to not use classes or methods which exist only on the newer JRE - otherwise the JVM will throw some error on class loading. Using the same JDK version as is the target version is the safest bet.

P.S. IntelliJ IDEA has an inspection to warn if you use too new methods/classes. There is also a tool called Retroweaver to run Java 5 code on Java 1.4.


Unless compiled with the -target command line option, it will not run on a older JVM.

When compiled with the -target option, the compiled classes should run 100% equal on the older (but not older then [target]) jvm.

0

精彩评论

暂无评论...
验证码 换一张
取 消