URLClassLoader无法加载jar文件

时间:2019-03-04 15:53:56

标签: java

我当前正在使用Web应用程序,该应用程序必须从本地驱动器上的目录加载jar文件。 经过一番研究,URLClassLoaders似乎是最合适的解决方案。

牛肉:

public @Slf4j class ActionLoader {
private static void addFilesToClassPath(File path) {
        URLClassLoader clazzLoader = null;
        try {
            clazzLoader = new URLClassLoader(new URL[] {path.toURI().toURL()},
                                         ActionLoader.class.getClassLoader());
            try {
                Class.forName("de.befoe.xyz.ActionTest", false, clazzLoader);
            } catch (ClassNotFoundException e) {
                log.error("Class not found", e);
            }
            try {
                clazzLoader.loadClass("de.befoe.xyz.ActionTest");
            } catch (ClassNotFoundException e) {
                log.error("Class not found", e);
            }
        } catch (SecurityException e) {
            log.error("Access denied", e);
        } catch (MalformedURLException e) {
            log.error("Malformed URL", e);
        } finally {
            if (clazzLoader != null) {
                try {
                    clazzLoader.close();
                } catch (IOException e) {
                    log.error("Error closing the class loader", e);
                }
            }
        }
    }
}

第一个ClassNotFoundException

java.lang.ClassNotFoundException: de.befoe.xyz.ActionTest
    at java.net.URLClassLoader.findClass(URLClassLoader.java:382)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
    at de.procrm.speedindexer.scr.ActionLoader.addFilesToClassPath(ActionLoader.java:80)
    at de.procrm.speedindexer.scr.ActionLoader.loadActions(ActionLoader.java:31)
    at de.procrm.speedindexer.scr.Init.contextInitialized(Init.java:39)
    at org.apache.catalina.core.StandardContext.listenerStart(StandardContext.java:4842)
    at org.apache.catalina.core.StandardContext.startInternal(StandardContext.java:5303)
    at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:147)
    at org.apache.catalina.core.ContainerBase.addChildInternal(ContainerBase.java:725)
    at org.apache.catalina.core.ContainerBase.addChild(ContainerBase.java:701)
    at org.apache.catalina.core.StandardHost.addChild(StandardHost.java:717)
    at org.apache.catalina.startup.HostConfig.deployWAR(HostConfig.java:940)
    at org.apache.catalina.startup.HostConfig.deployApps(HostConfig.java:467)
    at org.apache.catalina.startup.HostConfig.check(HostConfig.java:1612)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:498)
    at org.apache.tomcat.util.modeler.BaseModelMBean.invoke(BaseModelMBean.java:300)
    at com.sun.jmx.interceptor.DefaultMBeanServerInterceptor.invoke(DefaultMBeanServerInterceptor.java:819)
    at com.sun.jmx.mbeanserver.JmxMBeanServer.invoke(JmxMBeanServer.java:801)
    at org.apache.catalina.manager.ManagerServlet.check(ManagerServlet.java:1451)
    at org.apache.catalina.manager.ManagerServlet.deploy(ManagerServlet.java:724)
    at org.apache.catalina.manager.ManagerServlet.doPut(ManagerServlet.java:423)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:651)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:729)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:292)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:207)
    at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:240)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:207)
    at org.apache.catalina.filters.SetCharacterEncodingFilter.doFilter(SetCharacterEncodingFilter.java:108)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:240)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:207)
    at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:212)
    at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:106)
    at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:614)
    at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:141)
    at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:79)
    at org.apache.catalina.valves.AbstractAccessLogValve.invoke(AbstractAccessLogValve.java:616)
    at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:88)
    at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:528)
    at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1099)
    at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:670)
    at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1520)
    at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.run(NioEndpoint.java:1476)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624)
    at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)
    at java.lang.Thread.run(Thread.java:748)

第二ClassNotFoundException

java.lang.ClassNotFoundException: de.befoe.xyz.ActionTest
    at java.net.URLClassLoader.findClass(URLClassLoader.java:382)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
    at java.lang.Class.forName0(Native Method)
    at java.lang.Class.forName(Class.java:348)
    at de.procrm.speedindexer.scr.ActionLoader.addFilesToClassPath(ActionLoader.java:81)
    at de.procrm.speedindexer.scr.ActionLoader.loadActions(ActionLoader.java:31)
    at de.procrm.speedindexer.scr.Init.contextInitialized(Init.java:39)
    at org.apache.catalina.core.StandardContext.listenerStart(StandardContext.java:4842)
    at org.apache.catalina.core.StandardContext.startInternal(StandardContext.java:5303)
    at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:147)
    at org.apache.catalina.core.ContainerBase.addChildInternal(ContainerBase.java:725)
    at org.apache.catalina.core.ContainerBase.addChild(ContainerBase.java:701)
    at org.apache.catalina.core.StandardHost.addChild(StandardHost.java:717)
    at org.apache.catalina.startup.HostConfig.deployWAR(HostConfig.java:940)
    at org.apache.catalina.startup.HostConfig.deployApps(HostConfig.java:467)
    at org.apache.catalina.startup.HostConfig.check(HostConfig.java:1612)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:498)
    at org.apache.tomcat.util.modeler.BaseModelMBean.invoke(BaseModelMBean.java:300)
    at com.sun.jmx.interceptor.DefaultMBeanServerInterceptor.invoke(DefaultMBeanServerInterceptor.java:819)
    at com.sun.jmx.mbeanserver.JmxMBeanServer.invoke(JmxMBeanServer.java:801)
    at org.apache.catalina.manager.ManagerServlet.check(ManagerServlet.java:1451)
    at org.apache.catalina.manager.ManagerServlet.deploy(ManagerServlet.java:724)
    at org.apache.catalina.manager.ManagerServlet.doPut(ManagerServlet.java:423)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:651)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:729)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:292)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:207)
    at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:240)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:207)
    at org.apache.catalina.filters.SetCharacterEncodingFilter.doFilter(SetCharacterEncodingFilter.java:108)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:240)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:207)
    at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:212)
    at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:106)
    at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:614)
    at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:141)
    at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:79)
    at org.apache.catalina.valves.AbstractAccessLogValve.invoke(AbstractAccessLogValve.java:616)
    at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:88)
    at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:528)
    at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1099)
    at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:670)
    at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1520)
    at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.run(NioEndpoint.java:1476)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624)
    at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)
    at java.lang.Thread.run(Thread.java:748)

路径变量指向包含jar文件的目录(我也尝试使用直接指向jar文件的URL初始化类加载器)。

当我现在尝试从一个jar文件访问一个类时,我得到一个ClassNotFoundException。在调试器中检查类加载器时,我还可以看到,packages和classes-fields均为空,因此看起来好像类加载器没有加载任何东西。

首先,我认为这与缺少依赖项有关,因此我创建了第二个没有依赖项的jar文件,并将类的父类加载器添加到构造函数中,但是它什么都没有改变。

想法?

E:经过一番研究,看来我的第一个想法(依赖性)是正确的。我有另一个供Web应用程序和加载的jar文件使用的库。如果我将此库添加到要添加到URLClassLoader的jar列表中,则可以很好地加载jar。但是可以肯定的是,这不是我想要做的,因为从技术上讲,我的Web应用程序使用的不是jar文件,而是其他类,这会导致其他问题。

现在的问题是为什么父类加载器类使用不正确。经过进一步研究,似乎我通过调用ActionLoader.class.getClassLoader()使用的父类加载器不是正确的加载器。因此,在eclipse调试器中,我看到了一个称为委托的字段,该字段设置为false。因此,我改为使用此类的父级加载器,并且文件加载也很好。

1 个答案:

答案 0 :(得分:0)

我没有您的JAR文件,因此我已经在自定义路径上对我的一个JAR文件进行了测试,并且已使用systemClassLoader正确加载了该类。

这是示例代码:

addFilesToClassPath(new File("C:/Users/Domenico/.m2/repository/com/doctorchat/core/0.1.ALPHA/core-0.1.ALPHA.jar"));

private static void addFilesToClassPath(File path) {

   try(URLClassLoader clazzLoader = new URLClassLoader(new URL[]{path.toURI().toURL()}, ClassLoader.getSystemClassLoader())) {
            Class c = clazzLoader.loadClass("com.doctorchat.core.exceptions.NotImplementedException");
            System.out.println(c);
        } catch (MalformedURLException e) {
            e.printStackTrace();
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        //  clazzLoader.getResource()

    }