0

当我尝试在运行时动态加载一个类,并且该类是在一个不是默认包的包中编译时,我有一个 ClassNotFound 异常。

示例:包名称:“型号”。类名:“Cup.class”。这是我的代码:

File file = new File(fileName);
            URL url;
            URL[] urls;
            String name = file.getName().substring(0, file.getName().length()-6);
            file = new File(file.getAbsolutePath().replaceAll(file.getName(), ""));
            System.out.println(name);
            // Convert File to a URL
            url = file.toURL();
            urls = new URL[]{url};
            ClassLoader cl = new URLClassLoader(urls);          
            Class cls = cl.loadClass(name);     
            Shape temp = (Shape) cls.newInstance();
            ShapeFactory.getInstance().registerShape(temp);
4

0 回答 0