Java Class.forName() from distant directory

I am currently loading Java classes using Class.forName() to load it.

clazz = Class.forName("interfaces.MyClass");

But now I want to load classes from different directory, I have tried to set classpath by

clazz = Class.forName("-cp \"C:/dir\" distantinterfaces.DistantClass");

With no success and ClassNotFoundException. Full path to distant class is:

C:/dir/distantinterfaces/DistantClass.class

Answers


Use an URLClassLoader for this. The code might be something along the lines of:

File f = new File("C:/dir");
URL[] cp = {f.toURI().toURL()};
URLClassLoader urlcl = new URLClassLoader(cp);
Class clazz = urlcl.loadClass("distantinterfaces.DistantClass");

Need Your Help

git EOL converted the opposit way around? (despite .gitattributes)

windows git msysgit eol gitattributes

I am working on Windows 7 and to prevent EOL problems I have a .gitattributes file set up the following way (as described in the github help):

Django 1.6 Cant display a manytomany field from models in template

python django model many-to-many

I'm trying to display a manytomany field from doctor models in template. Every doctor has more than one language associated to it. So I'm trying to display languages associated to each doctor. The