将 javaagent 与 Junit 一起使用会导致 Class.forName 中出现 ClassNotFoundException

发布于 2024-10-08 23:18:57 字数 1045 浏览 0 评论 0原文

java -classpath requiredclasspath org.junit.runner.JUnitCore some.package.HelloWorldTest

结果:

JUnit version 4.8.1
.

Time: 0.005

OK (1 test)

但是:

java -javaagent:agent.jar -classpath requiredclasspath org.junit.runner.JUnitCore some.package.HelloWorldTest    

结果:

JUnit version 4.8.1
Could not find class: some.package.HelloWorldTest

Time: 0.001

OK (0 tests)

核心问题似乎是:

Class.forName("some.package.HelloWorldTest") (runMain method, line 89, JunitCore)

抛出 ClassNotFoundException 如下:

java.lang.ClassNotFoundException: some/package/HelloWorldTest
at java.lang.Class.forName0(Native Method)
at java.lang.Class.forName(Class.java:169)
at org.junit.runner.JUnitCore.runMain(JUnitCore.java:89)
at org.junit.runner.JUnitCore.runMainAndExit(JUnitCore.java:53)
at org.junit.runner.JUnitCore.main(JUnitCore.java:45)

我不知道为什么它找不到该类。请注意,检测代理已成功加载并且不会引发任何异常。

java -classpath requiredclasspath org.junit.runner.JUnitCore some.package.HelloWorldTest

results in:

JUnit version 4.8.1
.

Time: 0.005

OK (1 test)

But:

java -javaagent:agent.jar -classpath requiredclasspath org.junit.runner.JUnitCore some.package.HelloWorldTest    

results in:

JUnit version 4.8.1
Could not find class: some.package.HelloWorldTest

Time: 0.001

OK (0 tests)

The core issue seems to be that:

Class.forName("some.package.HelloWorldTest") (runMain method, line 89, JunitCore)

throws a ClassNotFoundException as follows:

java.lang.ClassNotFoundException: some/package/HelloWorldTest
at java.lang.Class.forName0(Native Method)
at java.lang.Class.forName(Class.java:169)
at org.junit.runner.JUnitCore.runMain(JUnitCore.java:89)
at org.junit.runner.JUnitCore.runMainAndExit(JUnitCore.java:53)
at org.junit.runner.JUnitCore.main(JUnitCore.java:45)

I dont know why it cannot find the class. Note that the instrumentation agent is loaded successfully and does not throw any exceptions.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

定格我的天空 2024-10-15 23:18:57

事实证明,我在为 javaagent 指定的启动类路径中指定了 junit jar,并且通过 -classpath 指定了用户类路径,即 agent.jar 的清单具有以下条目:

Boot-Class-Path: ...junit.jar...

并且 java 命令的 -classpath 参数具有以下内容:

-classpath ...junit.jar...

我能够通过从清单启动类路径条目中删除 junit.jar 来解决该问题。以下是对该问题的简短解释:

由于 junit.jar 包含在引导类路径中,因此首先使用引导类加载器加载 org.junit.runner.JUnitCore。因此,当在 JunitCore 中调用 Class.forName 时,它​​尝试使用引导类加载器查找该类,但由于它不是引导类路径的一部分,因此无法找到该类。

Turns out I had the junit jar in the boot classpath specified for the javaagent as well as the user classpath specified through -classpath i.e. the manifest for agent.jar had the following entry:

Boot-Class-Path: ...junit.jar...

and the -classpath argument to the java command had the following:

-classpath ...junit.jar...

I was able to fix the problem by removing junit.jar from the manifest boot classpath entry. Here is a short explanation of the problem:

Since junit.jar was included in the boot classpath, org.junit.runner.JUnitCore was first loaded using the boot classloader. So when Class.forName was invoked within JunitCore, it tried to find the class using the boot classloader which could not find the class since it was not part of the boot classpath.

惟欲睡 2024-10-15 23:18:57
some.package.HelloWorldTest 

不在 CLASSPATH 中。添加它进去,一切都会好起来的。

some.package.HelloWorldTest 

is not in the CLASSPATH. Add it in and all will be well.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文