如何在tomcat上动态加载类?

时间:2013-02-25 12:24:43

标签: java tomcat classloader

我已经学会了如何动态地将“.class”文件加载到我的程序中,但如果我在tomcat上运行它,则无效。这是代码:

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方法,其中包含类Protocol的子类的路径。此子类放在不同的包中。因此,当我尝试定义它(defineClass)时,它会尝试加载超类(Protocol),但却无法做到。这是堆栈跟踪:

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)

1 个答案:

答案 0 :(得分:5)

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