无法使用源代码选项编译Java 11代码。

huangapple go评论152阅读模式
英文:

Can't Compile Java 11 Code With Source Options

问题

我正在尝试测试模块之间的交叉编译。我有一个使用Java 1.8的模块,另一个模块使用Java 11。我试图将Java 11模块用作Java 1.8项目的依赖。当我尝试编译Java 11模块时,Maven显示错误。

主要模块

<project>
    <dependencies>
        <dependency>
            <groupId>com.berkin</groupId>
            <artifactId>java</artifactId>
            <version>1.0-SNAPSHOT</version>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>3.8.1</version>
                <configuration>
                    <source>1.8</source>
                    <target>1.8</target>
                </configuration>
            </plugin>
        </plugins>
    </build>
</project>

次要模块命名为JAVA

<project>
    <modelVersion>4.0.0</modelVersion>

    <groupId>com.berkin</groupId>
    <artifactId>java</artifactId>
    <version>1.0-SNAPSHOT</version>

    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>3.8.1</version>
                <configuration>
                    <source>11</source>
                    <target>1.8</target>
                </configuration>
            </plugin>
        </plugins>
    </build>
</project>

次要模块中的代码

public class Test {
    public void test(){
        System.out.println(" ".isEmpty()); //true
    }
}

理论上,我应该能够使用Java 11编译器编译次要模块,并在最低为Java 8的JRE中使用它。因此,我还可以将它用作Java 8项目的依赖。我漏掉了什么地方?

> 错误信息

 Failed to execute goal org.apache.maven.plugins:maven-compiler-plugin:3.8.1:compile (default-compile) on project java: Fatal error compiling: invalid source release: 11 -&gt; [Help 1]

谢谢。

英文:

I am trying to test cross compilation between modules. I have a module which uses java 1.8 and another module uses Java 11. I am trying to use Java 11 module as dependency on Java 1.8 project. When I try to compile Java 11 module, maven shows an error.

#MAIN MODULE

&lt;project&gt;
        &lt;dependencies&gt;
            &lt;dependency&gt;
                &lt;groupId&gt;com.berkin&lt;/groupId&gt;
                &lt;artifactId&gt;java&lt;/artifactId&gt;
                &lt;version&gt;1.0-SNAPSHOT&lt;/version&gt;
            &lt;/dependency&gt;
        &lt;/dependencies&gt;
    
        &lt;build&gt;
            &lt;plugins&gt;
                &lt;plugin&gt;
                    &lt;groupId&gt;org.apache.maven.plugins&lt;/groupId&gt;
                    &lt;artifactId&gt;maven-compiler-plugin&lt;/artifactId&gt;
                    &lt;version&gt;3.8.1&lt;/version&gt;
                    &lt;configuration&gt;
                        &lt;source&gt;1.8&lt;/source&gt;
                        &lt;target&gt;1.8&lt;/target&gt;
                    &lt;/configuration&gt;
                &lt;/plugin&gt;
            &lt;/plugins&gt;
        &lt;/build&gt;
&lt;/project&gt;

#SECONDARY MODULE NAMED JAVA

&lt;project&gt;
    &lt;modelVersion&gt;4.0.0&lt;/modelVersion&gt;

    &lt;groupId&gt;com.berkin&lt;/groupId&gt;
    &lt;artifactId&gt;java&lt;/artifactId&gt;
    &lt;version&gt;1.0-SNAPSHOT&lt;/version&gt;
  
    &lt;build&gt;
        &lt;plugins&gt;
            &lt;plugin&gt;
                &lt;groupId&gt;org.apache.maven.plugins&lt;/groupId&gt;
                &lt;artifactId&gt;maven-compiler-plugin&lt;/artifactId&gt;
                &lt;version&gt;3.8.1&lt;/version&gt;
                &lt;configuration&gt;
                    &lt;source&gt;11&lt;/source&gt;
                    &lt;target&gt;1.8&lt;/target&gt;
                &lt;/configuration&gt;
            &lt;/plugin&gt;
        &lt;/plugins&gt;
    &lt;/build&gt;
&lt;/project&gt;

#CODE ON SECONDARY MODULE

public class Test {
    public void test(){
        System.out.println(&quot; &quot;.isEmpty()); //true
    }
}

In theory I should able to compile secondary module with java 11 compiler and use it in min Java 8 JRE. Thus I can also use it in Java 8 project as a dependency. Where am I missing?

> ERROR

 Failed to execute goal org.apache.maven.plugins:maven-compiler-plugin:3.8.1:compile (default-compile) on project java: Fatal error compiling: invalid source release: 11 -&gt; [Help 1]

Thanks.

答案1

得分: 1

主要原因是您不能使用比“目标”值更低的“源”值。如果您希望代码与Java 1.8兼容,请将源版本降低为1.8。

英文:

The main reason is that you cannot use a source value that is lower than the target value. If you want the code to be compatible with Java 1.8, lower the source to 1.8

答案2

得分: 0

我明白了,感谢@Elliott Frisch的解答。

有几个术语:

Java编译器:它由JAVA_HOME定义,或者如果你选择另一个Java版本用于Maven,那就是它了。你可以使用Java 11编译器编译Java 6代码,因为它具有向后兼容性。

Source:你想要使用Java功能的最低Java版本。例如,如果我想使用lambda表达式,至少我必须使用source 8。如果我想要使用Java 11的功能,我需要将它设置为11。这完全关乎你想要使用的Java功能

Target:能够运行我的功能的最低JRE版本。例如,如果我设置为Java 8,我至少需要JRE 8来使用lambda表达式。我不能使用JRE 7或更低版本,因为它们不知道什么是lambda表达式。

此外,

如果Java编译器版本是8,它无法编译源代码为11的类。因为编译器不知道如何编译Java 11。一开始我失败了,因为我的源代码版本低于要求。当我改成11和11时,我再次失败,因为这次编译器版本更旧(它是Java 8)。

谢谢

英文:

I get it thanks to @Elliott Frisch

There are several terms;

Java Compiler : It is defined by JAVA_HOME or if you override by selecting another Java version for Maven thats it. You can compile Java 6 codes with Java 11 compiler. Because it has backward compatibility.

Source : Minimum Java version which you want to use java features. For example, if I want to use lambdas, at least I have to use source 8. If I want to use Java 11 features, I need to set it to 11. Its all about Java Features that you want to use

Target : Minimum JRE version which can run my features. E.g. if I set Java 8, I need at least JRE 8 to use lambdas. I can't use JRE 7 or lower because they don't know what lambda is.

Also

If java compiler version is 8, it can't compile source 11 classes. Because compiler don't know how to compile Java 11. I failed at the beginning because I had lower than source. When I changed into 11 & 11, I failed again because this time compiler was older(It was Java 8)

Thanks

huangapple
  • 本文由 发表于 2020年3月4日 02:53:18
  • 转载请务必保留本文链接:https://go.coder-hub.com/60513814.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定