1

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?

4
  • 4
    what ide are you using? You could easily set the project language level to 6 while your project is using JDK 7. Commented Dec 21, 2015 at 16:03
  • 1
    Pretty sure that rt.jar exists in Java 8 too...it would live in the JRE subdirectory though. Commented Dec 21, 2015 at 16:06
  • @JasonZ: I don't use any IDE. Commented Dec 22, 2015 at 6:52
  • @Makoto: I do have a rt.jar (it is in jdk1.7.0_79/jre/lib/rt.jar). However, I would interpret the Oracle docs (in particular the last paragraph on the page I mentioned), that I must not use a rt.jar from Java 7, if the application is supposed to run on Java 6. Commented Dec 22, 2015 at 6:58

2 Answers 2

7

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":

enter image description here

Sign up to request clarification or add additional context in comments.

2 Comments

With 'add the appropriate "JRE System Library"', do you mean adding the rt.jar from Java 6 to the build path?
No, you must replace (or "edit") the "JRE System Library". I have added a screenshot.
0

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.

5 Comments

Hmmm.... But the documentation explicitly states: You must specify the -bootclasspath option to specify the correct version of the bootstrap classes (the rt.jar library). Under what conditions can I ignore this restriction? I'm not quite sure what rt.jar actually contains.
"rt" stands for "runtime". It contains the Java API and most of its implementation. As such, it is very advisable to compile against the same API used at runtime, lest one inadvertently use an API not yet available on the target Java version.
@user1934428 Finally I have found a link here that explains the significance of using -bootstrap flag and helps answer your question. It also demonstrates what could go wrong at the runtime if -bootstrap is not specified, by using StringBuffer API as an example. I think I need to correct my answer above to reflect the same.
Even with -bootstrap you still need the 1.6 rt.jar
@ErickG.Hagstrom That is true. -bootstrap and -extdirs flags just lets us specify, against which rt.jar file and ext dirs we want to compile the source code. For this question we need to use 1.6 specific rt.jar and extdirs file to ensure that we don't see any surprises at runtime, as demonstrated in the following [link]javaworld.com/article/2077388/core-java/….

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.