检查奇怪的ClassNotFoundException,尽管它存在。

huangapple go评论86阅读模式
英文:

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 &quot;9.0.4&quot;
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: &quot;linux&quot;, version: &quot;5.4.0-40-generic&quot;, arch: &quot;amd64&quot;, family: &quot;unix&quot;

This is my pom:

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

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] --------------------------------&lt; a:a &gt;---------------------------------
[INFO] Building a 0.0.1-SNAPSHOT
[INFO] --------------------------------[ jar ]---------------------------------
[INFO] 
[INFO] --- maven-resources-plugin:2.6:resources (default-resources) @ a ---
[INFO] Using &#39;UTF8&#39; 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 &#39;UTF8&#39; 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 &lt;&lt;&lt; FAILURE!
a.MyTest.testHi()  Time elapsed: 0 sec  &lt;&lt;&lt; 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] -&gt; [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

How can that even be? The Class exists inside JDK9.04
检查奇怪的ClassNotFoundException,尽管它存在。

答案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

org.apache.maven.plugins
maven-surefire-plugin
2.21.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

&lt;build&gt;
	&lt;pluginManagement&gt;
		&lt;plugins&gt;
			&lt;plugin&gt;
				&lt;groupId&gt;org.apache.maven.plugins&lt;/groupId&gt;
				&lt;artifactId&gt;maven-surefire-plugin&lt;/artifactId&gt;
				&lt;version&gt;2.21.0&lt;/version&gt;
			&lt;/plugin&gt;
		&lt;/plugins&gt;
	&lt;/pluginManagement&gt;
&lt;/build&gt;

huangapple
  • 本文由 发表于 2020年7月22日 03:50:53
  • 转载请务必保留本文链接:https://go.coder-hub.com/63022078.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定