简体   繁体   中英

Running JVM 6 compiled code on JVM 5 or lower

在JVM版本6上执行编译为字节码的类并在较低版本的JVM(例如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 })
                   ^

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.

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.

PS 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM