Java Eclipse 中的“编译器合规级别”是什么?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/22584427/
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
What is "compiler compliance level" in Eclipse?
提问by Aviv Cohn
For some time I tried to understand, but I still don't get exactly what "compiler compliance level" for a project in Eclipse means. I looked all over this site and Google and couldn't find an answer I could understand.
有一段时间我试图理解,但我仍然不明白 Eclipse 中项目的“编译器合规级别”到底是什么意思。我浏览了这个网站和谷歌,找不到我能理解的答案。
Let's say I want my program to be able to run on JRE 6.
假设我希望我的程序能够在 JRE 6 上运行。
I can do: Project > Preferences > Java Build Path > Libraries, and set the JRE library I use to JRE 6.
我可以这样做:项目 > 首选项 > Java 构建路径 > 库,并将我使用的 JRE 库设置为 JRE 6。
Why isn't this enough?
为什么这还不够?
I never understood why I also need to set the compiler compliance setting to JRE 6.
我一直不明白为什么我还需要将编译器合规性设置设置为 JRE 6。
I'd like to understand the difference between using JRE 6 in a project, and setting the project's compiler compliance setting to JRE 6.
我想了解在项目中使用 JRE 6 与将项目的编译器合规性设置设置为 JRE 6 之间的区别。
What exactly does compiler compliance level mean?
编译器合规级别究竟是什么意思?
采纳答案by user253751
The compiler compliance setting tells the compiler to pretend it's a different version of Java.
编译器合规性设置告诉编译器假装它是不同版本的 Java。
The Java 8 compiler will produce class files in the Java 8 version of the class file format, and accept Java 8 source files. JRE 6 can't load this version, because it was created after JRE 6 was.
Java 8 编译器将生成 Java 8 版本的类文件格式的类文件,并接受 Java 8 源文件。JRE 6 无法加载此版本,因为它是在 JRE 6 之后创建的。
If you set the compliance level to "JRE 6", it will instead compile Java 6 source files into Java 6 class files.
如果您将合规性级别设置为“JRE 6”,它将改为将 Java 6 源文件编译为 Java 6 类文件。
It's like saving a Word document as "Word 97-2003 format" - so that Word 97-2003 can read your document. You're saving the class files in Java 6 format so that Java 6 can read them.
这就像将 Word 文档另存为“Word 97-2003 格式”——这样 Word 97-2003 就可以阅读您的文档。您正在以 Java 6 格式保存类文件,以便 Java 6 可以读取它们。
回答by Brendan Lesniak
It tells you what version of the JDK you are adhering to; specifically, which JRE are you targeting with your build.
它会告诉您所使用的 JDK 版本;具体来说,您的构建目标是哪个 JRE。
It is the MINIMUM JRE needed to run your code
这是运行代码所需的最小 JRE
回答by djmorton
The Java compiler can compile code that will run on prior versions of the JVM. Even if you have JDK 6 installed, you could be writing code targeted for people that use JDK 5. The Compiler Compliance level tells Eclipse to use appropriate settings when compiling your project to ensure you code will work on the target JVM you specify. By default, if I recall, Eclipse picks Java 5 Compliance. If you want to use code features specific to Java 6 or Java 7, you will have to change the compliance level.
Java 编译器可以编译将在先前版本的 JVM 上运行的代码。即使您安装了 JDK 6,您也可能正在为使用 JDK 5 的人编写代码。Compiler Compliance 级别告诉 Eclipse 在编译您的项目时使用适当的设置,以确保您的代码可以在您指定的目标 JVM 上运行。默认情况下,如果我记得,Eclipse 会选择 Java 5 Compliance。如果要使用特定于 Java 6 或 Java 7 的代码功能,则必须更改合规性级别。
回答by ErstwhileIII
The simplest way to describe the "Java compiler compliance" setting is that it determines which Java Virtual Machine instructions may be used in the "compiled" Java code ... and which library class versions are to be used. Each release of the JVM (and its libraries) may introduce new instructions (the VM) or adjust the class libraries that are delivered along with the JVM.
描述“Java 编译器合规性”设置的最简单方法是它确定哪些 Java 虚拟机指令可用于“已编译”Java 代码……以及要使用哪些库类版本。JVM(及其库)的每个版本都可能引入新指令(VM)或调整与 JVM 一起交付的类库。
You should set the setting to the lowest level of JVM that you expect your product to be run on.
您应该将设置设置为您希望运行产品的最低 JVM 级别。