user3998574
user3998574

Reputation:

how to identified class if including package from another directory

from there, i using class loader to identified class from other directory

File dir=new File ("D:\\dirclass")
fulldir = new File (dir+"\\myclass");
filename = new StringTokenizer(fulldir.getName(), ".").nextToken();
URL[] checkclass = 
{
    dir.toURI().toURL()
};
URLClassLoader urlcl = new URLClassLoader(checkclass);
Class cls = urlcl.loadClass(filename);

this is worked if class without package.

but if class with package, failed to running.

Exception in thread "main" java.lang.NoClassDefFoundError: packclass (wrongname: dirclass\packclass)

is there any other way ?

Upvotes: 0

Views: 53

Answers (1)

meskobalazs
meskobalazs

Reputation: 16041

You have to specify the fully qualified names of classes to the ClassLoader, for example:

Class cls = urlcl.loadClass("com.mypackage.MyClass");

Upvotes: 1

Related Questions