带自定义类加载器的Jmockit:系统类加载器不支持在实时阶段将JAR文件添加到系统类路径

时间:2016-05-16 07:12:00

标签: java classloader jmockit

我正在使用这样的自定义类加载器:

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添加任何内容以防止出现此错误吗?

1 个答案:

答案 0 :(得分:0)

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

您可能还想查看jmockit initialization instruction并确保您的设置与说明一致。