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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10107831/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-30 23:39:46  来源:igfitidea点击:

Is it possible to compile class files with the Java 7 SDK which can run on Java 6 JVMs?

javajavacjava-7java-6

提问by t3chris

Since the public Java 6 SE JRE is getting closer to it's EOL (Nov '12), I'm considering porting my projects from Java 6 to Java 7. This would'nt be a big deal, if Apple would provide a Java 7 JRE for Mac OS X. But since Apple isn't willing to do so, I still have to support users which only have a Java 6 JRE.

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

Is there a way to compile Java 6 compatible binaries (class files) with the Java 7 javac? Certainly I'm aware that I can't use the new Java 7 features when doing so.

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

Thanks in anticiption!

感谢期待!

回答by Stephen C

It depends. If your program doesn't use the new Java 7 language extensions, then you can run the Java compiler with the -source 1.6and -target 1.6options. But if you use Java 7 language extensions then -source 1.6will result in compilation errors.

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

Certainly I'm aware that I can't use the new Java 7 features when doing so.

当然,我知道这样做时我无法使用新的 Java 7 功能。

That includes Java 7 language features ... and dependencies on Java 7 changes to the standard class library APIs. Also be aware that there are small number of behavioural differences (aka API bug fixes) that may cause code to run differently on Java 6 and Java 7. They should be described in the Java 6 to Java 7 transition document.

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



UPDATE- This probably no longer an issue for you anyway, because Oracle have released Java 7 for Mac OSX.

更新- 无论如何,这对您来说可能不再是问题,因为 Oracle 已经为 Mac OSX 发布了 Java 7。

回答by Kent

i have jdk6 installed. if you check the man page of javac:

我已经安装了 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).

回答by Shawn Shroyer

Yes, but in some cases no. In java 1.6 they didn't have the try with resources, switch with strings, or multi catch statements etc. So those parts of the program will not compile. But the idea of java is compile once, run everywhere; so code can work on old JVMs

是的,但在某些情况下不是。在 java 1.6 中,他们没有使用资源进行尝试、使用字符串切换或多捕获语句等。因此程序的这些部分将无法编译。但是java的思想是一次编译,到处运行;所以代码可以在旧的 JVM 上运行

回答by Bounce

Stephen C's answer is correct, but not complete. Your Java 7 programs won't compile in Java 6 if they use Java 7 language features, but be warned subtle other bugs can still occur with one developer coding in Java 6 and another compiling Java 7.

Stephen C 的回答是正确的,但并不完整。如果您的 Java 7 程序使用 Java 7 语言功能,则它们将无法在 Java 6 中编译,但请注意,一个开发人员在 Java 6 中编码而另一个开发人员在编译 Java 7 时仍可能出现其他细微的错误。

Take for example java.sql.Driver. In Java 7, the interface gained an additional method.

以 java.sql.Driver 为例。在 Java 7 中,接口获得了一个额外的方法。

Java 7 DeveloperThis developer implements the Driver interface and uses the 'Override' annotation on the implemented additional Driver method. The program compiles fine as a Java 6 program because the class that the Java 6 compiler sees does have that method and the code gets checked in. Compiling the program as Java 6 does not mean that Java 6 compiler will automatically switch to use Java 6 source code!

Java 7 开发人员此开发人员实现了驱动程序接口,并在实现的附加驱动程序方法上使用了“覆盖”注释。该程序作为 Java 6 程序编译得很好,因为 Java 6 编译器看到的类确实具有该方法并且代码被检入。将程序编译为 Java 6 并不意味着 Java 6 编译器将自动切换到使用 Java 6 源代码代码!

Java 6 DeveloperThe Java 6 developer attempts to build the code the Java 7 developer committed and gets a compilation error even though the Java 7 developer was not implementing any Java 7 language constructs.

Java 6 开发人员Java 6 开发人员尝试构建 Java 7 开发人员提交的代码并得到编译错误,即使 Java 7 开发人员没有实现任何 Java 7 语言构造。

Consequently, even though you couldcompile it as Java 6, I would recommend not doing this.

因此,即使您可以将其编译为 Java 6,我也不建议这样做。