简体   繁体   English

从提取的 war 文件中动态加载外部类

[英]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".这个问题很受欢迎,但我找不到避免“java.lang.ClassNotFoundException”的方法。

I had implement a method to load dynamicly some external classes (that I got after extracting a war file)我已经实现了一个动态加载一些外部类的方法(我在提取一个war文件后得到)

  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.当您提供 class 的完全限定名称但尝试将其加载到 package 中时,会发生此错误。 we should select the path to the class file without enterring to the package folder我们应该 select class 文件的路径而不进入 package 文件夹

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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