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.

4
  • 2
    Here is the cross-compilation documentation for Java 7. OldCode.java is expected to be written to the target source level. IIRC, Java 5 and Java 6 syntax are the same. Commented Aug 31, 2012 at 8:07
  • @McDowell that means the source code should not contain any java7 new features, right? Commented Aug 31, 2012 at 8: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. Commented Aug 31, 2012 at 8:30
  • exactly what features are you looking for? Some things, like ForkJoinPool can be added via backport libraries. Commented Aug 31, 2012 at 12:40

2 Answers 2

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

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

7 Comments

agree. upgrade or downgrade. cross-compiling is a lot of pain.
@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 Just got back! Not in China of course as its illegal there. ;)
@PeterLawrey Hmm, I actually have problem understanding your joke...?
@lzprgmr Time travel is illegal in China as is depicting time travel. techland.time.com/2011/04/13/china-decides-to-ban-time-travel dailymail.co.uk/news/article-1376771/…
|
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.

4 Comments

"won't catch cases where you call a 7-only method." It certainly will if using the bootclasspath option correctly.
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...
"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.jarof 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..).
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.

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.