5

I was considering if a java project could produce 2 jars: one for java7 and one for java6, yes, the source code might use some some java7 new features.

so to generate the java6 jar, the command line would be like:

javac -target 1.6 -bootclasspath jdk1.6.0\lib\rt.jar -extdirs "" MyApp.java

Unfortunately, It simply emits an error:

javac: target release 1.6 conflicts with default source release 1.7

According to this document, it should be possible for jdk6 vs jdk5, anybody knows why it doesn't work in jdk7 vs jdk6? did I do something wrong, or is it just officially not supported?

Thanks.

Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
Baiyan Huang
  • 6,463
  • 8
  • 45
  • 72
  • 2
    [Here is the cross-compilation documentation for Java 7.](http://docs.oracle.com/javase/7/docs/technotes/tools/windows/javac.html#crosscomp-example) `OldCode.java` is expected to be written to the target source level. IIRC, Java 5 and Java 6 syntax are the same. – McDowell Aug 31 '12 at 08:07
  • @McDowell that means the source code should not contain any java7 new features, right? – Baiyan Huang Aug 31 '12 at 08:08
  • Correct. If you're looking to target Java 6 with Java 7 code you'll likely need to look at a specialist 3rd party tool. – McDowell Aug 31 '12 at 08:30
  • exactly what features are you looking for? Some things, like ForkJoinPool can be added via backport libraries. – Matt Aug 31 '12 at 12:40

2 Answers2

5

AFAIK, the source and target have to be the same. Even for Java 6. The only exception is the source can be 1.1 and the target 1.0.

Given there is little difference in the JVM between the latest JVM for Java 6 and Java 7, I suggest you consider upgrading. Also Java 6 will be, End Of (free) Service in Nov 2012, which three months from now...

Peter Lawrey
  • 525,659
  • 79
  • 751
  • 1,130
  • 1
    agree. upgrade or downgrade. cross-compiling is a lot of pain. – Thilo Aug 31 '12 at 08:01
  • 1
    @PeterLawrey "Nov 2010, which three months from now." Have we traveled back in time? and why am I stuck in 2012?? :) sorry couldn't help myself. – JTMon Aug 31 '12 at 08:05
  • @JTMon Just got back! Not in China of course as its illegal there. ;) – Peter Lawrey Aug 31 '12 at 08:10
  • @PeterLawrey Hmm, I actually have problem understanding your joke...? – Baiyan Huang Sep 18 '13 at 12:47
  • @lzprgmr Time travel is illegal in China as is depicting time travel. http://techland.time.com/2011/04/13/china-decides-to-ban-time-travel/ http://www.dailymail.co.uk/news/article-1376771/Great-Scott-China-bans-time-travel-cinema-TV.html – Peter Lawrey Sep 18 '13 at 13:10
  • @PeterLawrey well, the only reason why " State Administration of Radio, Film and Television" exist in China, is to making jokes regularly, so we could have fun:) – Baiyan Huang Sep 22 '13 at 11:14
  • @lzprgmr every country has some laws which don't make much sense ;) In South Korean, it is illegal to sell a fan without a timer to prevent people dying of asphyxiation or hypothermia. England has so many old laws which make no sense that they are routinely ignored. They have a law which makes it the death penalty to be a monarch or to pretend to be one. (ignored rather than officially revoked) In the USA it is still illegal to be a member of a communist party, despite China being a major trading partner. – Peter Lawrey Sep 22 '13 at 11:19
2

Even if it were possible it's generally a bad idea - if you want to be sure your code will work on java 6 then you have to build it on java 6. Each new version of java introduces new classes in the class library, and adds new methods to existing classes, and even if you set your java 7 compiler to generate 6-compatible bytecode it won't catch cases where you call a 7-only method.

Ian Roberts
  • 120,891
  • 16
  • 170
  • 183
  • *"won't catch cases where you call a 7-only method."* It certainly will if using the `bootclasspath` option correctly. – Andrew Thompson Aug 31 '12 at 08:28
  • 1
    True, but if you have a Java 6 installation available to you so you can put its class libraries on the Java 7 bootclasspath then why not just keep it simple and compile using that Java 6 in the first place... – Ian Roberts Aug 31 '12 at 09:37
  • 1
    *"Java 6 installation"* By installation you mean JDK. Bootclasspath can work with the `rt.jar` for an earlier installed **JRE** or more importantly, the `rt.jar`of the earlier version, no 'install' needed. If you are compiling code for 1.7, and 1.6, as well as some code for legacy devices that run 1.3, it is much simpler dealing with a group of run-time Jars than a group of installed JDKs (with a group of `java.home` values and..). – Andrew Thompson Aug 31 '12 at 09:42
  • If the JDK could do it it would be a good idea for compiling an applet with java 7 source features that you want to be compatible with browsers that haven't upgraded their plugin from java 6. – Stan Kurdziel Sep 17 '13 at 21:28