Classloader not 'loading' jar at runtime?

275 views Asked by At

To start of, its was all working, and sundenly it just stopped and i truly have no idea if i done something to it or there is a problem somewhere else.

My classloader should load a jar file at runtime but apparently it doesn't.

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);
        }
    }

I can not disclose the full path of URL but i can confirm 101% the jar is where the String url says, yet i do get DefNotFoundClassException.

0

There are 0 answers