我正在使用这样的自定义类加载器:
public class MyClassLoader extends URLClassLoader {
public MyClassLoader(ClassLoader parent) {
super(((URLClassLoader) parent).getURLs(), parent);
}
}
我也在使用jmockit。例如考虑以下代码:
public static void main(String[] args) throws Exception {
new MockUp<Main>(){
};
}
当我运行应用程序时 -Djava.system.class.loader=app.MyClassLoader 它抛出此异常:
Exception in thread "main" java.lang.ExceptionInInitializerError
at app.Main.main(Main.java:12)
Caused by: java.lang.IllegalStateException: com.sun.tools.attach.AgentLoadException: Unable to add JAR file to system class path
at mockit.internal.startup.AgentLoader.loadAgentAndDetachFromRunningVM(AgentLoader.java:150)
at mockit.internal.startup.AgentLoader.loadAgent(AgentLoader.java:56)
at mockit.internal.startup.Startup.verifyInitialization(Startup.java:172)
at mockit.MockUp.<clinit>(MockUp.java:94)
... 1 more
Caused by: com.sun.tools.attach.AgentLoadException: Unable to add JAR file to system class path
at sun.tools.attach.HotSpotVirtualMachine.loadAgent(HotSpotVirtualMachine.java:119)
at mockit.internal.startup.AgentLoader.loadAgentAndDetachFromRunningVM(AgentLoader.java:146)
... 4 more
System class loader does not support adding JAR file to system class path during the live phase!
Unable to add C:\Users\hmdha\.m2\repository\org\jmockit\jmockit\1.23\jmockit-1.23.jar to system class path - not supported by system class loader or configuration error!
我应该向我的类加载器添加任何内容以防止出现此错误吗?
最佳答案
您的 junit/nunit jar 之前的类路径上是否有 jmockit?如果没有,您通常会遇到问题。或者,您可以将此注释添加到您的测试类中:@RunWith(JMockit.class)
您可能还想查看jmockit initialization instruction并确保您的设置与说明一致。
关于java - 带有自定义类加载器的 Jmockit : System class loader does not support adding JAR file to system class path during the live phase,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37248543/