Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can we use jdk7 javac to compile code with java7 features into java6 bytecode

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.

like image 596
Baiyan Huang Avatar asked Aug 31 '12 07:08

Baiyan Huang


2 Answers

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

like image 150
Peter Lawrey Avatar answered Sep 30 '22 04:09

Peter Lawrey


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.

like image 22
Ian Roberts Avatar answered Sep 30 '22 03:09

Ian Roberts