Classloader在运行时没有“加载”jar?

时间:2014-11-28 01:53:39

标签: java tomcat classloader dynamic-class-loaders

首先,它一切正常,太阳光刚刚停止,我真的不知道我是否做了什么,或者其他地方有问题。

我的类加载器应该在运行时加载一个jar文件,但显然不是。

String url   = "https://" + serverHost + "/" + getJarPath() + "se.jar";
log.info("http url is: " +url);
String https = url.replaceAll("\\s","");

URL httpsURL = new URL(https);  
log.info("https url is: " + httpsURL);

URLConnection con = httpsURL.openConnection();              
addURL(con.getURL());



private void addURL(URL https) throws IOException { 
        Class<?>[] parameters = new Class[]{URL.class}; 
        // IMPORTANT: MUST use the webapp classloader - so derived extension classes can resolve their base classes
        ClassLoader contextClassLoader = Thread.currentThread().getContextClassLoader();        
        // cast to a URL class loader so we can add additional JAR(s) to the search path
        URLClassLoader webappClassLoader = (URLClassLoader)contextClassLoader;  
        Class<?> sysclass = URLClassLoader.class;

        Method method;
        try {
            method = sysclass.getDeclaredMethod("addURL", parameters);
            method.setAccessible(true);
            method.invoke(webappClassLoader, new Object[]{ https });
        } catch (NoSuchMethodException | SecurityException e) {
            log.log(Level.WARNING, "FATAL Exception", e);
        } catch (IllegalAccessException | IllegalArgumentException | InvocationTargetException e) {
            log.log(Level.WARNING, "FATAL Exception", e);
        }
    }

我无法透露URL的完整路径,但我可以确认101%的jar是String url所说的,但我确实得到了DefNotFoundClassException。

0 个答案:

没有答案