英文:
Inspecting weird ClassNotFoundException allthou it exists
问题
我有一个Maven项目(a)
这是项目树:
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
时,出现了以下异常:
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] ------------------------------------------------------------------------
[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
怎么可能呢?这个类存在于JDK 9.04 中。
英文:
I have a maven project (a)
This is the Project-tree:
grim@hv0053:~/workspace/a$ tree
.
├── pom.xml
└── src
└── test
└── java
└── a
└── MyTest.java
4 directories, 2 files
grim@hv0053:~/workspace/a$
This is the maven/jdk version:
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"
This is my 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>
This is my a/src/test/java/a/MyTest.java
:
package a;
import javax.tools.DiagnosticListener;
public class MyTest {
public void testHi() {
DiagnosticListener.class.toString();
assert true;
}
}
When I run mvn install
this exception 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
答案1
得分: 0
好的,以下是翻译好的部分:
"Ok, there might was a bug in surefire 2.20. It does not work prior to version 2.21.0.
So all I had to do is to
英文:
Ok, there might was a bug in surefire 2.20. It does not work prior to version 2.21.0.
So all I had to do is to
<build>
<pluginManagement>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.21.0</version>
</plugin>
</plugins>
</pluginManagement>
</build>
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论