Peter is a DZone MVB and is not an employee of DZone and has posted 155 posts at DZone. You can read more from them at their website. View Full User Profile

Using Java 7 to target much older JVMs

  • submit to reddit

Java 5.0 and 6 used to have poor support for compiling classes to target older versions of Java. It always supported the previous version, but often no more.

Even if you could compile for previous version, you had to be careful not to use functionality which did exist in the previous versions.

Java 7

Java 7 addresses both these issues. Firstly, it supports sources back to 1.2 and targets back to Java 1.1. Secondly. it insists you set the bootclasspath so you can include the version of the libraries you will be using for that version.
public class Main {
    public static void main(String[] args) {
        System.out.println("Hello World!");

$ javac -target 1.7 -source 1.7
$ javac -target 1.6 -source 1.6
warning: [options] bootstrap class path not set in conjunction with -source 1.6
1 warning
$ javac -Xbootclasspath:/usr/java/jdk1.6.0_29/jre/lib/rt.jar -target 1.6 -source 1.6
$ javac -Xbootclasspath:/usr/java/jdk1.5.0_22/jre/lib/rt.jar -target 1.5 -source 1.5
$ javac -Xbootclasspath:/usr/java/jdk1.4.0_30/jre/lib/rt.jar -target 1.4 -source 1.4
$ javac -Xbootclasspath:/usr/java/jdk1.3.1_29/jre/lib/rt.jar -target 1.3 -source 1.3
$ javac -Xbootclasspath:/usr/java/jdk1.2.2_017/jre/lib/rt.jar -target 1.2 -source 1.2
$ javac -Xbootclasspath:/usr/java/jdk1.1.8_16/jre/lib/rt.jar -target 1.1 -source 1.2
$ javac -Xbootclasspath:/usr/java/jdk1.1.8_16/jre/lib/rt.jar -target 1.1 -source 1.1
javac: invalid source release: 1.1
Usage: javac  <source>
use -help for a list of possible options
$ javac -Xbootclasspath:/usr/java/jdk1.1.8_16/jre/lib/rt.jar -target 1.0 -source 1.0
javac: invalid target release: 1.0
Usage: javac  <source>
use -help for a list of possible options

BTW: All the previous versions back to 1.1 are available from the Oracle web site Oracle Java Archive



Published at DZone with permission of Peter Lawrey, author and DZone MVB.

(Note: Opinions expressed in this article and its replies are the opinions of their respective authors and not those of DZone, Inc.)



Mohammad Haq replied on Wed, 2012/02/15 - 3:30am

Just wondering if that gives you any benifit?

Like for example java 5 code compiled with java 7 will perform better or blah

Arnaud Des_vosges replied on Wed, 2012/02/15 - 4:20am in response to: Mohammad Haq

It would be a surprise because as Brian Goetz says in a recent video, in java, you don't recompile to have better perfs (like C++,...). Most optimizations occur at runtime (JIT...), not at compile time.

Alejandro Dobniewski replied on Wed, 2012/02/15 - 8:28am

Is it a new feature of Java 7 or we could do the same with previous Java versions?

Mohammad Haq replied on Wed, 2012/02/15 - 5:27pm in response to: Arnaud Des_vosges

So will the JIT on JVM 7 be smarter on java 5/6 code compiled with java 7?

Ronald Miura replied on Wed, 2012/02/15 - 8:21pm

AFAIK, all JDKs support compilation targeting versions down to 1.1, provided it matches the source version (see, for example, the 1.5 javac man page).

It would be awesome if it supported targeting old (1.4-) versions using newer (1.5+) features (generics, outo-boxing, etc.), altough I don't think that's the case.

Peter Lawrey replied on Sun, 2012/02/19 - 8:13am in response to: Mohammad Haq

The compiler does next to no optimisations and what it does do has been there a long time.  Most of the smarts are in the JVM.

Peter Lawrey replied on Sun, 2012/02/19 - 8:16am in response to: Mohammad Haq

If you compare code compiled by Java 1.1 compiler and the Java 7 compiler running on Java 7, the speed will be much the same.  You don't need to re-compile Java to take advantage of the latest performance enhancements. You only need to re-compile it to take advantage of the latest lanuages features e.g. generic, enums, ARM, closures.

Comment viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.