简体   繁体   English

"URLClassLoader ClassNotFoundException"

[英]URLClassLoader ClassNotFoundException

I'm trying to write a code that loads classes dynamically at run time我正在尝试编写一个在运行时动态加载类的代码

public class URLDynClassLoader {

    public URLDynClassLoader(){
        try{
            loadclasses( new File("C:/Users/Miller/Desktop/test/") , "Shapes." );
        }
        catch(ClassNotFoundException e){
            e.printStackTrace();
            System.out.println(e.getMessage());
        }
        catch( Exception f ){
            System.out.println(f.getMessage());
            System.out.println("error");
        }
    }

    private ArrayList<String> getClassNames( File folder ){
        File[] listOfFiles = folder.listFiles();
        ArrayList<String> fileNames = new ArrayList<String>() ;
        for (File file : listOfFiles) {
            if (file.isFile()) {
                if( accept(file.getName()) ){
                    fileNames.add(file.getName());
                }
            }
        }
        return fileNames ;
    }

    ArrayList<Class> loadclasses( File folder , String packageName ) throws MalformedURLException, ClassNotFoundException{

        URLClassLoader load = URLClassLoader.newInstance( new URL[] { folder.toURL() })  ;
        ArrayList<Class> data = new ArrayList<Class>();
        ArrayList<String> names = getClassNames(folder);

        for(int i=0 ; i<names.size() ; ++i){;
            data.add(load.loadClass( fixName( packageName , names.get(i) ) ));
            System.out.println("i"+i);
        }

        return data ;
    }

    private String fixName(String packageName, String className ) {
        className = className.replaceAll(".class", "");
        return packageName+className;
    }

    public boolean accept(String arg) { 
        return arg.endsWith(".class"); 
    } 

    public static void main(String[] args) {
        new URLDynClassLoader();
    }

}

Problem seems to be with method getClassNames() . 问题似乎出在方法getClassNames() Can you please check fully qualified class name returned from fixName() before loading classes? 您可以在加载类之前检查从fixName()返回的完全限定的类名称吗? It will give you better idea what's wrong with the code. 它将使您更好地了解代码出了什么问题。

I ran your sample code with hard coded values passed to fixName() (without using getClassNames() ) and it was able to load the class file. 我使用传递给fixName()硬编码值(而不使用getClassNames() )运行您的示例代码,它能够加载类文件。

Stupid answer perhaps, but for me the issue was that my project was in a folder which was somehow admin protected.也许是愚蠢的答案,但对我来说,问题是我的项目位于一个受管理员保护的文件夹中。

I tried so many "solutions", but I remembered when I tried to unzip a file to my project folder (some .jar lib I thought could fix it), it kept saying it couldn't extract because of admin rights or something.我尝试了很多“解决方案”,但我记得当我尝试将文件解压缩到我的项目文件夹(我认为可以修复它的一些 .jar lib)时,它一直说由于管理员权限或其他原因无法提取。

Whenever I extracted it somewhere else and manually copy-pasted it, it asked for "admin consent" something, I clicked yes and I could copy the files.每当我将它提取到其他地方并手动复制粘贴时,它都会要求“管理员同意”,我单击“是”,我可以复制文件。

My guess is, it's trying to find classes in a folder to which is does not have access to or something, and returns null.我的猜测是,它试图在无法访问的文件夹中查找类,并返回 null。 Which down the line, returns this error!下线,返回此错误!

So if anyone ever stumbles upon issues like this with "URLClassLoader" and "PolicyException"(s), please also make sure it's in a correct folder!!!因此,如果有人偶然发现“URLClassLoader”和“PolicyException”这样的问题,还请确保它位于正确的文件夹中!!!

"

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

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