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

跟風遠走 提交于 2019-12-07 06:15:36

问题


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.


回答1:


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




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



来源:https://stackoverflow.com/questions/12211094/can-we-use-jdk7-javac-to-compile-code-with-java7-features-into-java6-bytecode

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!