使用 -source 和 -target javac 选项编译

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/21204299/
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-08-13 07:18:30  来源:igfitidea点击:

Compile using -source and -target javac options

javamavencompiler-construction

提问by M Sach

I have upgraded my web application to Java 7 with JAVA_HOME pointing to 1.7. My Maven plugin is reading the Java version from java_home. But I am bit confused after seeing the below setting in pom.xml:

我已将我的 Web 应用程序升级到 Java 7,其中 JAVA_HOME 指向 1.7。我的 Maven 插件正在从 java_home 读取 Java 版本。但是在 pom.xml 中看到以下设置后,我有点困惑:

<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-compiler-plugin</artifactId>
    <version>2.3.2</version>
    <configuration>
        <fork>true</fork>
        <compilerVersion>1.6</compilerVersion>
        <source>1.6</source>
        <target>1.6</target>
    </configuration>
</plugin>

If I keep the above settings as it is, will Maven compile the Java code with 1.6 or 1.7? As per my understanding based on this link, the above settings will get preference and the project will be compiled with 1.6 instead of 1.7. Is that correct?

如果我保持上述设置不变,Maven 会用 1.6 还是 1.7 编译 Java 代码?根据我基于此链接的理解,上述设置将获得优先权,项目将使用 1.6 而不是 1.7 进行编译。那是对的吗?

If I give a setting like below and if I have code specific to JDK 1.7, will my code compile now?

如果我给出如下设置并且我有特定于 JDK 1.7 的代码,我的代码现在可以编译吗?

 <source>1.6</source>
  <target>1.7</target>

I am not sure; what do the above settings actually mean?

我不确定; 上述设置实际上是什么意思?

采纳答案by MariuszS

Compiler configuration without plugin

没有插件的编译器配置

<properties>
  <maven.compiler.source>1.7</maven.compiler.source>
  <maven.compiler.target>1.7</maven.compiler.target>
</properties>

javac -help

javac -help

javac -help

  -source <release>          Provide source compatibility with specified release
  -target <release>          Generate class files for specific VM version

javac - Java programming language compilerdocumentation

javac - Java 编程语言编译器文档

sourceSpecifies the version of source code accepted. The following values for release are allowed:

  • 1.6 No language changes were introduced in Java SE 6. However, encoding errors in source files are now reported as errors instead of warnings as in previous releases of Java SE.
  • 6 Synonym for 1.6.
  • 1.7 This is the default value. The compiler accepts code with features introduced in Java SE 7.
  • 7 Synonym for 1.7.

targetGenerate 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), 1.6 (also 6), and 1.7 (also 7). The default for -target depends on the value of -source:

source指定接受的源代码版本。允许使用以下发布值:

  • 1.6 Java SE 6 中没有引入任何语言更改。但是,源文件中的编码错误现在报告为错误而不是Java SE 以前版本中的警告。
  • 6 1.6 的同义词。
  • 1.7 这是默认值。编译器接受具有 Java SE 7 中引入的特性的代码。
  • 7 1.7 的同义词。

target生成以指定版本的 VM 为目标的类文件。类文件将在指定的目标和更高版本上运行,但不会在 VM 的早期版本上运行。有效目标是 1.1、1.2、1.3、1.4、1.5(也是 5)、1.6(也是 6)和 1.7(也是 7)。-target 的默认值取决于 -source 的值:

Compatibility

兼容性

回答by Tobias Kremer

Yes, this will compile your code to Java 1.6 code (which will also run in a Java 7 environment).

是的,这会将您的代码编译为 Java 1.6 代码(也将在 Java 7 环境中运行)。

回答by A4L

With the settings you have, the source code is interpreted as Java 1.6 compliant and the generated classes will target 1.6 JVM. JDK 1.7 can deal with it. But if your code is 1.7 and want to target only 1.7 then you should write 1.7 in both. Note that if your source code includes features that are only in 1.7 available, diamond operator for instance, then you'll get compile error.

使用您的设置,源代码被解释为符合 Java 1.6,生成的类将针对 1.6 JVM。JDK 1.7 可以处理它。但是,如果您的代码是 1.7 并且只想针对 1.7,那么您应该同时编写 1.7。请注意,如果您的源代码包含仅在 1.7 中可用的功能,例如菱形运算符,那么您将收到编译错误。

With the settings

随着设置

<source>1.6</source>
<target>1.7</target>

the following 1.7 code (diamond operator)

以下 1.7 代码(菱形运算符)

List<String> l = new ArrayList<>();

will fail to compile with the following error:

将无法编译并出现以下错误:

... Compilation failure
... App.java:[14,40] diamond operator is not supported in -source 1.6
(use -source 7 or higher to enable diamond operator)

Whereas the following code will compile just fine

而以下代码将编译得很好

List<String> l = new ArrayList<String>();

with the settings:

使用设置:

<source>1.7</source>
<target>1.6</target>

The following 1.7 code (diamond operator)

下面1.7代码(菱形算子)

List<String> l = new ArrayList<>();

will also fail to compile with the following error:

也将无法编译,并出现以下错误:

source release 1.7 requires target release 1.7

So if you want to use source features of 1.7 (diamond operator, try-with-resources, etc.) then you have to have the following in your pom.xml.

因此,如果您想使用 1.7 的源功能(菱形运算符、try-with-resources 等),那么您必须在 pom.xml 中包含以下内容。

<source>1.7</source>
<target>1.7</target>