简体   繁体   中英

Load dynamicly external classes from a extracted war file

This question is so popular but I can't find the way to avoid the "java.lang.ClassNotFoundException".

I had implement a method to load dynamicly some external classes (that I got after extracting a war file)

  Class<?> classGetter() {
    URL url;
    Class<?>  cls = null;
    try {
        String className=new Substringer(entireFilePath.getName(), 5).output;
        String classPath=new 
        Substringer(entireFilePath.getAbsolutePath(),5+className.length()).output;
        File classDirPath = new File(classPath);
        url = classDirPath.toURI().toURL();
        URL[] urls = new URL[]{url};
        URLClassLoader cl = new URLClassLoader(urls); 
        cls = cl.loadClass(packageGetter()+"."+className);
        cl.close();
    } catch (ClassNotFoundException | IOException e) {
        e.printStackTrace();
    }
    return cls;
}

the same exception still show up again and again. here is the stacktrace:

java.lang.ClassNotFoundException: bber.Counter
    at java.net.URLClassLoader.findClass(Unknown Source)
    at java.lang.ClassLoader.loadClass(Unknown Source)
    at java.lang.ClassLoader.loadClass(Unknown Source)
    at earviewer.ClassesLoaser.classGetter(ClassesLoaser.java:48)
    at earviewer.ClassesLoaser.main(ClassesLoaser.java:58)
    Exception in thread "main" java.lang.NullPointerException
    at earviewer.ClassesLoaser.main(ClassesLoaser.java:59)

This error occurs when you give fully qualified name of the class but try to load it inside the package. we should select the path to the class file without enterring to the package folder

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM