[警告]引导类路径未与 -source 8 一起设置的 Maven 解决方案(在 JDK17 和 JDK 8 上为 Java 8 构建))

问题描述 投票:0回答:2

我有一个Java项目,在使用maven编译时给出了主题中的警告, 但编译通过了。

[INFO] --- compiler:3.11.0:compile (default-compile) @ ******* ---
[INFO] Changes detected - recompiling the module! :source
[INFO] Compiling 10 source files with javac [debug target 1.8] to target/classes
[WARNING] bootstrap class path not set in conjunction with -source 8

但是当我切换到JDK 8时,编译失败了

[ERROR] Failed to execute goal org.apache.maven.plugins:maven-compiler-plugin:3.11.0:compile (default-compile) on project ******-service: Compilation failure
[ERROR] /Users/******/Workspaces/****/src/main/java/com/*****/*****Service.java:[92,28] cannot find symbol
[ERROR]   symbol:   method readAllBytes()
[ERROR]   location: variable resource of type java.io.InputStream

很明显,在使用 JDK 17 时,我没有注意到, 该代码使用的是 Java 9 以来才出现的方法。

问题:如何正确解决此类警告 并在使用 JDK 17 编译时指定引导类路径?

(因为我们不想多次重新配置JAVA_HOME, 只是为了确保该项目仍然真正在 JDK/JRE 8 上运行)

mvn --version
Apache Maven 3.9.1 (2e178502fcdbffc201671fb2537d0cb4b4cc58f8)
Maven home: /opt/homebrew/Cellar/maven/3.9.1/libexec
Java version: 17.0.6, vendor: Amazon.com Inc., runtime: /Users/****/Library/Java/JavaVirtualMachines/corretto-17.0.6-1/Contents/Home
java maven java-8 java-17
2个回答
1
投票

需要将选项传递给编译器,例如

-bootclasspath $JAVA8_HOME/jre/lib/rt.jar

(在

https://docs.oracle.com/javase/8/docs/technotes/tools/windows/javac.html
页面上搜索 bootclasspath for javac 8 文档)

但是对于 JDK 17,在面向 Java 17 时不应使用此选项 (来自 https://docs.oracle.com/en/java/javase/17/docs/specs/man/javac.html#compiling-for-earlier-releases-of-the-platform):

针对 JDK 9 及更高版本进行编译时,不能使用任何用于配置引导类路径的选项。这包括以下所有选项: -Xbootclasspath/p:、-Xbootclasspath、-Xbootclasspath/a:、-endorseddirs、-Djava.endorsed.dirs、-extdirs、-Djava.ext.dirs、-profile

(通过未与 -source 1.6 一起设置的 bootstrap 类路径找到http://blog.vanillajava.blog/2012/02/using-java-7-to-target-much-older-jvms.html

需要一些时间来弄清楚如何通过maven传递这个

                <!--
                <configuration>
                    <compilerArgs>
                        <arg>-bootclasspath ${env.JAVA8_HOME}/jre/lib/rt.jar</arg>
                    </compilerArgs>
                </configuration>
                 get error
                [ERROR] Failed to execute goal org.apache.maven.plugins:maven-compiler-plugin:3.11.0:compile (default-compile) on project ***: 
    Fatal error compiling: invalid flag: -bootclasspath /Users/***/Library/Java/JavaVirtualMachines/corretto-1.8.0_362/Contents/Home/jre/lib/rt.jar -> [Help 1]
                -->

© www.soinside.com 2019 - 2024. All rights reserved.