是否可以使用可在Java 6 JVM上运行的Java 7 SDK编译类文件?

t3c*_*ris 23 java javac java-6 java-7

由于公共Java 6 SE JRE越来越接近它的EOL(11月12日),我正在考虑将我的项目从Java 6移植到Java 7.如果Apple提供Java 7 JRE,这将不是什么大问题.对于Mac OS X.但由于Apple不愿意这样做,我仍然需要支持只拥有Java 6 JRE的用户.

有没有办法用Java 7 javac编译Java 6兼容的二进制文件(类文件)?当然,我知道在这样做时我不能使用新的Java 7功能.

谢谢你的期待!

Ste*_*n C 21

这取决于.如果您的程序不使用新的Java 7语言扩展,那么您可以使用-source 1.6-target 1.6选项运行Java编译器.但是如果使用Java 7语言扩展,-source 1.6则会导致编译错误.

当然,我知道在这样做时我不能使用新的Java 7功能.

这包括Java 7语言特性......以及对Java 7的依赖性对标准类库API的更改.另请注意,存在少量行为差异(即API错误修复)可能导致代码在Java 6和Java 7上以不同方式运行.应在Java 6到Java 7转换文档中对其进行描述.


更新 - 无论如何,这可能不再是一个问题,因为Oracle已经发布了适用于Mac OSX的Java 7.


Ken*_*ent 5

我安装了jdk6.如果你查看javac的手册页:

Cross-Compilation Options
          By default, classes are compiled against the bootstrap and extension classes of the platform that javac shipped with. But javac also supports cross-compil?
          ing, where classes are compiled against a bootstrap and extension classes of a different Java platform implementation. It is important to use -bootclasspath
          and -extdirs when cross-compiling; see Cross-Compilation Example below.

         -target version
            Generate class files that target a specified version of the VM. Class files will run on the specified target and on later versions, but not on earlier
            versions of the VM. Valid targets are 1.1 1.2 1.3 1.4 1.5 (also 5) and 1.6 (also 6).
Run Code Online (Sandbox Code Playgroud)