3

我已经学会了如何将“.class”文件动态加载到我的程序中,但是如果我在 tomcat id 上运行它就不起作用。这是代码:

package testPackage;

public class ModuleEngine {

    public static void main(String pth, String name) {

        ModuleLoader loader = new ModuleLoader(pth, ClassLoader.getSystemClassLoader());

        try {
            Class clazz = loader.loadClass(name);
            Protocol execute = (Protocol)clazz.newInstance(); 
            execute.run();
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        } catch (InstantiationException e) {
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        }
    }
}

public class ModuleLoader extends ClassLoader {

    private String pathtobin;

    public ModuleLoader(String pathtobin, ClassLoader parent) {
        super(parent);    
        this.pathtobin = pathtobin;    
    }

    @Override
    public Class findClass(String className) throws ClassNotFoundException {
        try {
            byte b[]= fetchClassFromFS(pathtobin + className + ".class");
            return defineClass(null, b, 0, b.length);
        } catch (FileNotFoundException ex) {
            return super.findClass(className);
        } catch (IOException ex) {
            return super.findClass(className);
        }
    }
}

我在 ModuleEngine 类中运行 main 方法,其中包含类协议子类的路径。这个子类放在不同的包中。因此,当我尝试定义它(defineClass)时,它会尝试加载超类(协议)但未能做到这一点。这是堆栈跟踪:

java.lang.ClassNotFoundException: testPackage.Protocol
    at java.lang.ClassLoader.findClass(Unknown Source)
    at testPackage.ModuleLoader.findClass(ModuleLoader.java:30)
    at java.lang.ClassLoader.loadClass(Unknown Source)
    at java.lang.ClassLoader.loadClass(Unknown Source)
    at java.lang.ClassLoader.defineClass1(Native Method)
    at java.lang.ClassLoader.defineClass(Unknown Source)
    at java.lang.ClassLoader.defineClass(Unknown Source)
    at testPackage.ModuleLoader.findClass(ModuleLoader.java:28)
    at java.lang.ClassLoader.loadClass(Unknown Source)
    at java.lang.ClassLoader.loadClass(Unknown Source)
    at testPackage.ModuleEngine.main(ModuleEngine.java:13)
    at testPackage.HtmlPage.doGet(HtmlPage.java:27)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:621)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:728)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:305)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)
    at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:222)
    at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:123)
    at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:472)
    at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:171)
    at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:99)
    at org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:936)
    at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:118)
    at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:407)
    at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1004)
    at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:589)
    at org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:310)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)
    at java.lang.Thread.run(Unknown Source)
4

1 回答 1

5

而不是ClassLoader.getSystemClassLoader()尝试使用ModuleEngine.class.getClassLoader (). 这将返回知道类附近的ModuleEngine类的类加载器。

于 2013-02-25T12:26:36.597 回答