Loading Multiple Version of the Same Class

There comes a time we are forced to use multiple version of a library in the project. The Java system classloader will load a specific class only once. To load different implementation of a class reside in different versions of JAR file, we can make use of java.net.URLClassLoader.

Following is an example code of using URLClassLoader to load the class named HelloPrint from two different versions of Hello library.

The ClassloaderMultipleVersion.java

public class ClassloaderMultipleVersion {

	public static void main(String[] args) throws Exception {

		ClassLoader loaderA = new URLClassLoader(new URL[] { new File("Hellov1.jar")
																.toURI().toURL() });
		ClassLoader loaderB = new URLClassLoader(new URL[] { new File("Hellov2.jar")
																.toURI().toURL() });

		Class<?> classA = loaderA.loadClass("com.prnv.hello.HelloPrint");
		Class<?> classB = loaderB.loadClass("com.prnv.hello.HelloPrint");

		Method methodA = classA.getMethod("hello");
		Method methodB = classB.getMethod("hello");



This example make use of Java Reflection to invoke the method.