0

I have a Java project that while compiling with maven gave warning as in subject, but compilation passed.

[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

But when I switched to JDK 8, compilation failed

[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

So clearly, while using JDK 17, I could not notice, that code was using method, that only appeared since Java 9.

Question: how to correctly solve such warning and specify bootstrap class path when compiling with JDK 17?

(Because we don't want to reconfigure JAVA_HOME several times, just to make sure that project still really runs on 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
Paul Verest
  • 60,022
  • 51
  • 208
  • 332
  • See https://maven.apache.org/plugins/maven-compiler-plugin/examples/set-compiler-release.html – khmarbaise Apr 20 '23 at 17:17
  • Yes, javac compiler `--release` option is the simplest solution, but it will not work on JDK 8 as appeared only in JDK 9 – Paul Verest Apr 20 '23 at 17:29
  • Yes correct... I would ask in the meantime why still using JDK 8 and even for compiling using JDK11 or JDK17 etc.... – khmarbaise Apr 20 '23 at 18:10
  • For example, some hardcoded version values in infrastructure. And it looks it is real headache to make a project possible to compile with both JDK 8 and JDK 17.... – Paul Verest Apr 20 '23 at 18:42
  • 1
    Just get it compiling with JDK17 (--release 8) ...and in september JDK21.. prepared for future ;-) – khmarbaise Apr 20 '23 at 18:59

1 Answers1

0

One need to pass option to compiler like

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

(Search for bootclasspath for javac 8 docs on https://docs.oracle.com/javase/8/docs/technotes/tools/windows/javac.html page)

However with JDK 17 this option should not be used when targeting Java 17 (from https://docs.oracle.com/en/java/javase/17/docs/specs/man/javac.html#compiling-for-earlier-releases-of-the-platform ):

When compiling for JDK 9 and later releases, you cannot use any option that is intended to configure the boot class path. This includes all of the following options: -Xbootclasspath/p:, -Xbootclasspath, -Xbootclasspath/a:, -endorseddirs, -Djava.endorsed.dirs, -extdirs, -Djava.ext.dirs, -profile

(Found via bootstrap class path not set in conjunction with -source 1.6 and http://blog.vanillajava.blog/2012/02/using-java-7-to-target-much-older-jvms.html)

Need some time to figure out how to pass this via 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]
                -->
Paul Verest
  • 60,022
  • 51
  • 208
  • 332
  • simply no ... use for JDK9+ `--release` which means for the maven-compiler-plugin: `9` if you like to compile via JDK17 to target 9 ... etc... or if you like to build for target 8 simply use `8` (do not use source/target anymore).. – khmarbaise Apr 20 '23 at 17:16