Monday, February 22, 2010

Loading Class which is not in Classpath

Java reflections gives us the ability to load a class, but using simple class loaders you can load a class which is there in classpath. But how to load the class which is not there in classpath?
Well, its not that hard.
URLClassLoader is there for us. Look at following code, which shows how to use URLClassLoader.
File file=new File("path to you directory in which your class is");
URL urls[]={
 file.toURI().toURL()
};

URLClassLoader loader=new URLClassLoader(urls);
Class object=loader.loadClass("classname");

No comments:

Post a Comment

Ads Inside Post