2016-05-16 178 views
1

我使用的是自定义类加载器是这样的:Jmockit使用自定义类装载器:系统类加载器不支持添加JAR文件到系统类路径在现场阶段

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! 

我应该添加任何东西到我的Class Loader来防止这个错误?

回答

0

你的junit/nunit jar之前的classpath上是否有jmockit?如果没有,你通常会遇到问题。或者,您可以将此注释添加到测试类中:@RunWith(JMockit.class)

您也可以查看jmockit initialization instruction并确保您的设置与说明一致。

相关问题