[英]Maven is using wrong java version
I am trying to build a maven project on centOS 8. I want maven to use Java version 15. When I run mvn package
I get the following error: I am trying to build a maven project on centOS 8. I want maven to use Java version 15. When I run
mvn package
I get the following error:
Failed to execute goal org.apache.maven.plugins:maven-compiler-plugin:3.8.1:compile
(default-compile) on project systembrett-backend: Fatal error compiling: invalid target release: 15 -> [Help 1]
So I suspect that maven is using the wrong Java version, because when i do mvn package -X
for debug logs it start with:所以我怀疑 maven 使用了错误的 Java 版本,因为当我执行
mvn package -X
调试日志时,它以:
Apache Maven 3.5.4 (Red Hat 3.5.4-5)
Maven home: /usr/share/maven
Java version: 1.8.0_275, vendor: Red Hat, Inc., runtime: /usr/lib/jvm/java-1.8.0-openjdk-1.8.0.275.b01-1.el8_3.x86_64/jre
Default locale: en_US, platform encoding: UTF-8
OS name: "linux", version: "4.18.0-240.1.1.el8_3.x86_64", arch: "amd64", family: "unix"
so it looks like, maven is using Java version 1.8.所以看起来,maven 使用的是 Java 版本 1.8。 But
mvn -version
says:但是
mvn -version
说:
Apache Maven 3.5.4 (Red Hat 3.5.4-5)
Maven home: /usr/share/maven
Java version: 15.0.2, vendor: AdoptOpenJDK, runtime: /opt/jdk-15.0.2+7
Default locale: en_US, platform encoding: UTF-8
OS name: "linux", version: "4.18.0-240.1.1.el8_3.x86_64", arch: "amd64", family: "unix"
JAVA_HOME
is /opt/jdk-15.0.2+7
and PATH
is /opt/jdk-15.0.2+7/bin:/home/username/.local/bin:/home/username/bin:/usr/local/bin:/usr/bin:/usr/local/sbin:/usr/sbin
. JAVA_HOME
是/opt/jdk-15.0.2+7
并且PATH
是/opt/jdk-15.0.2+7/bin:/home/username/.local/bin:/home/username/bin:/usr/local/bin:/usr/bin:/usr/local/sbin:/usr/sbin
。
I thought maven is choosing the java version by checking JAVA_HOME
, but apparently it is using an other version for the build.我认为 maven 正在通过检查
JAVA_HOME
选择 java 版本,但显然它正在使用其他版本进行构建。 Does anyone know how to tell maven the correct version?有谁知道如何告诉 maven 正确的版本? Thanks!
谢谢!
Following is a list of steps I use to troubleshoot this kind of issues:以下是我用来解决此类问题的步骤列表:
alternatives
to ensure proper default Java environment is used. alternatives
一起使用,以确保使用正确的默认 Java 环境。 Similar to:$ alternatives --config java
There are 2 programs which provide 'java'.
Selection Command
-----------------------------------------------
+ 1 java-1.7.0-openjdk.x86_64 (/usr/lib/jvm/java-1.7.0-openjdk-1.7.0.191-2.6.15.4.el7_5.x86_64/jre/bin/java)
* 2 java-1.8.0-openjdk.x86_64 (/usr/lib/jvm/java-1.8.0-openjdk-1.8.0.181-3.b13.el7_5.x86_64/jre/bin/java)
Try also for javac
as it may not be configured the same way:也可以试试
javac
,因为它的配置方式可能不同:
$ alternatives --config javac
maven
instance, JAVA_HOME
should be enough.maven
实例, JAVA_HOME
应该足够了。 Your output of mvn -version
testifies that you have configured it correctly.您的
mvn -version
的 output 证明您已正确配置它。 Remove your Java from your PATH
to ensure JAVA_HOME
and mvn
will find the correct one.从您的
PATH
中删除您的 Java 以确保JAVA_HOME
和mvn
会找到正确的。
pom.xml
can also configure the required compiler, similar to: pom.xml
也可以配置需要的编译器,类似:<properties>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
</properties>
You encounter this usually when different JRE vs JDK are used ( more: maven installation has runtime as JRE instead of JDK )当使用不同的 JRE 和 JDK 时,您通常会遇到这种情况( 更多:maven 安装运行时为 JRE 而不是 JDK )
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.