检查所有存在的奇怪的 ClassNotFoundException

Inspecting weird ClassNotFoundException allthou it exists

我有一个maven项目(一)

这是项目树:

grim@hv0053:~/workspace/a$ tree
.
├── pom.xml
└── src
    └── test
        └── java
            └── a
                └── MyTest.java

4 directories, 2 files
grim@hv0053:~/workspace/a$ 

这是 maven/jdk 版本:

grim@hv0053:~/workspace/a$ java -version
java version "9.0.4"
Java(TM) SE Runtime Environment (build 9.0.4+11)
Java HotSpot(TM) 64-Bit Server VM (build 9.0.4+11, mixed mode)

grim@hv0053:~/workspace/a$ mvn -version
Apache Maven 3.6.3 (cecedd343002696d0abb50b32b541b8a6ba2883f)
Maven home: /usr/local/share/apache-maven-3.6.3
Java version: 9.0.4, vendor: Oracle Corporation, runtime: /usr/local/share/jdk-9.0.4
Default locale: de_DE, platform encoding: UTF-8
OS name: "linux", version: "5.4.0-40-generic", arch: "amd64", family: "unix"

这是我的 pom:

<project xmlns="http://maven.apache.org/POM/4.0.0"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <groupId>a</groupId>
    <artifactId>a</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <properties>
        <maven.compiler.source>1.9</maven.compiler.source>
        <maven.compiler.target>1.9</maven.compiler.target>
        <forkMode>never</forkMode>
    </properties>
</project>

这是我的 a/src/test/java/a/MyTest.java:

package a;
import javax.tools.DiagnosticListener;
public class MyTest {
    public void testHi() {
        DiagnosticListener.class.toString();
        assert true;
    }
}

当我运行mvn install这个异常occourse:

grim@hv0053:~/workspace/a$ mvn install
[INFO] Scanning for projects...
[INFO] 
[INFO] --------------------------------< a:a >---------------------------------
[INFO] Building a 0.0.1-SNAPSHOT
[INFO] --------------------------------[ jar ]---------------------------------
[INFO] 
[INFO] --- maven-resources-plugin:2.6:resources (default-resources) @ a ---
[INFO] Using 'UTF8' encoding to copy filtered resources.
[INFO] Copying 0 resource
[INFO] 
[INFO] --- maven-compiler-plugin:3.1:compile (default-compile) @ a ---
[INFO] Nothing to compile - all classes are up to date
[INFO] 
[INFO] --- maven-resources-plugin:2.6:testResources (default-testResources) @ a ---
[INFO] Using 'UTF8' encoding to copy filtered resources.
[INFO] Copying 0 resource
[INFO] 
[INFO] --- maven-compiler-plugin:3.1:testCompile (default-testCompile) @ a ---
[INFO] Nothing to compile - all classes are up to date
[INFO] 
[INFO] --- maven-surefire-plugin:2.12.4:test (default-test) @ a ---
[WARNING] useSystemClassloader setting has no effect when not forking
[INFO] Surefire report directory: /home/grim/workspace/a/target/surefire-reports

-------------------------------------------------------
 T E S T S
-------------------------------------------------------
Running a.MyTest
Tests run: 1, Failures: 1, Errors: 0, Skipped: 0, Time elapsed: 0.001 sec <<< FAILURE!
a.MyTest.testHi()  Time elapsed: 0 sec  <<< FAILURE!
java.lang.NoClassDefFoundError: javax/tools/DiagnosticListener
    at a.MyTest.testHi(MyTest.java:8)
Caused by: java.lang.ClassNotFoundException: javax.tools.DiagnosticListener
    at java.base/java.net.URLClassLoader.findClass(URLClassLoader.java:466)
    at java.base/java.lang.ClassLoader.loadClass(ClassLoader.java:563)
    at java.base/java.lang.ClassLoader.loadClass(ClassLoader.java:496)
    at org.apache.maven.surefire.booter.IsolatedClassLoader.loadClass(IsolatedClassLoader.java:97)
    ... 44 more


Results :

Failed tests:   a.MyTest.testHi(): javax/tools/DiagnosticListener

Tests run: 1, Failures: 1, Errors: 0, Skipped: 0

[INFO] ------------------------------------------------------------------------
[INFO] BUILD FAILURE
[INFO] ------------------------------------------------------------------------
[INFO] Total time:  0.466 s
[INFO] Finished at: 2020-07-21T21:37:50+02:00
[INFO] ------------------------------------------------------------------------
[ERROR] Failed to execute goal org.apache.maven.plugins:maven-surefire-plugin:2.12.4:test (default-test) on project a: There are test failures.
[ERROR] 
[ERROR] Please refer to /home/grim/workspace/a/target/surefire-reports for the individual test results.
[ERROR] -> [Help 1]
[ERROR] 
[ERROR] To see the full stack trace of the errors, re-run Maven with the -e switch.
[ERROR] Re-run Maven using the -X switch to enable full debug logging.
[ERROR] 
[ERROR] For more information about the errors and possible solutions, please read the following articles:
[ERROR] [Help 1] http://cwiki.apache.org/confluence/display/MAVEN/MojoFailureException

怎么可能呢? Class存在于JDK9.04中

好的,surefire 2.20 中可能存在错误。它在 2.21.0 之前的版本不工作。

所以我所要做的就是

<build>
    <pluginManagement>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-surefire-plugin</artifactId>
                <version>2.21.0</version>
            </plugin>
        </plugins>
    </pluginManagement>
</build>