将 javaagent 与 Junit 一起使用会导致 Class.forName 中出现 ClassNotFoundException
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
事实证明,我在为 javaagent 指定的启动类路径中指定了 junit jar,并且通过 -classpath 指定了用户类路径,即 agent.jar 的清单具有以下条目:
并且 java 命令的 -classpath 参数具有以下内容:
我能够通过从清单启动类路径条目中删除 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:
and the -classpath argument to the java command had the following:
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.
不在 CLASSPATH 中。添加它进去,一切都会好起来的。
is not in the CLASSPATH. Add it in and all will be well.