Class loading from a non-default Package

279 views Asked by At

When I try to load a class Dynamically at run-time, and that class was compiled in a package that is not the default package, I have a ClassNotFound Exception.

Example: Package name: "Model". Class name: "Cup.class". This is my code:

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

There are 0 answers