Hi,
here is an example:
static void urlcl() throws Exception {
URL[] urls = new URL[] {
new File("C:\\global_libs\\somejar.jar").toURL(),
};
URLClassLoader urlCL = new URLClassLoader(urls);
Class clazz = Class.forName("com.somepackage.CustomStringBuffer",
true, urlCL);
Object instance = clazz.newInstance();
System.out.println("toString : " + instance.toString());
System.out.println("hashCode : " + instance.hashCode());
Method meth = clazz.getMethod("append", new Class[]
{String.class});
meth.invoke(instance, new String[] {"test"});
meth = clazz.getMethod("sendBuffer", new Class[] {Writer.class});
Writer pw = new PrintWriter(System.out);
meth.invoke(instance, new Writer[] {pw});
}
The best strategy however is to cast the returned instance to some
interface. This allows for re-loading of classes as soon as there is a
new JAR version (you can monitor for JAR file change).
Have a look at the Javadoc of java.net.URLClassLoader.
HTH,
chris