简体   繁体   中英

Compiling for Java 6 using Java 7 without installing Java 6

The target system, on which my application is supposed to run, uses Java 6. On my development machine, I have Java 7. Can I do the development, without downloading Java 6?

I found on http://docs.oracle.com/javase/7/docs/technotes/tools/windows/javac.html one example for cross compilation:

 javac -source 1.6 -target 1.6 -bootclasspath C:\jdk1.6.0\lib\rt.jar -extdirs "" OldCode.java

However, this too requires the existence of a rt.jar, which belongs to Java 6. Is there a simpler way?

New Java versions generally change both the Java Language (source and class file format) and the Java API.

The Java compiler can emit class files in the old format, even if the source is in a new format (these versions are specified by -target and -source , respectively). Therefore, you don't need the old compiler to target an old JVM.

However, the changes to the Java API are somewhat harder to account for. The easiest is to compile using the API of the Java version you target ( -bootclasspath ). Of course, you may feel confident that you are not using newer APIs, and skip this step, but the only way to make sure is actually compiling against, and testing on, the old runtime library.

In short, while cross compilation is helpful in that the same source can be used with different Java versions, you should compile and test against the actual Java version you intend to use, which does require the old JRE (or JDK).

BTW, all of these settings are also available in Java IDEs. For instance, in eclipse, one would set the compliance level in the project's compiler settings, and add the appropriate "JRE System Library" to the project's "Build Path":

在此处输入图片说明

The below command should suffice to meet your requirement. 'javac -source 1.6 -target 1.6 OldCode.java'

With this command you are telling that the compiler should generate class file that is compatible with java 6. Any java 7 specific will result in compilation error. Regarding rt.jar, you don't need to have java6 specific version. As mentioned the above command automatically ensures output is java6 compatible.

UPDATE/CORRECTION:

After going through the following link http://www.javaworld.com/article/2077388/core-java/what-version-is-your-java-code.html it is clear why it is recommended and is important to use -bootstrap flag along with -source and/or -target flags.

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