从非默认包加载类

时间:2013-12-14 16:37:55

标签: java dynamic packages urlclassloader dynamic-class-loaders

当我尝试在运行时动态加载类,并且该类是在非默认包的包中编译时,我有一个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);

0 个答案:

没有答案
相关问题