I'm trying to test that a class can be loaded to the jvm more then once but using different ClassLoader
so my code tries to load a class (class name "Tzvika") twice
first using the default ClassLoader
and in the second try using the URLClassLoader
the problem is that i get the same reference for the URLClassLoader and the default ClassLoader
what i'm doing wrong?
here code:
public static void main(String[] args) {
Tzvika t1 = new Tzvika();
System.out.println("t1 class loader: " + t1.getClass().getClassLoader());
Tzvika t2 = null;
try {
URLClassLoader clsLoader = URLClassLoader.newInstance(new URL[] {new URL("file:/C://data/workspace/ashrait/tests/SampleClassLoader/bin/com/tzvika/sample/")});
// same problem when i do this
//URLClassLoader clsLoader = new URLClassLoader(new URL[] {new URL("file:/C://data/workspace/ashrait/tests/SampleClassLoader/bin/com/tzvika/sample/")});
Class cls = clsLoader.loadClass("com.tzvika.sample.Tzvika");
t2 = (Tzvika)cls.newInstance();
System.out.println("t2 class loader: " + t2.getClass().getClassLoader());
} catch (Exception e) {
e.printStackTrace();
}
}
here my console output:
t1 class loader: sun.misc.Launcher$AppClassLoader@1a52fdf
t2 class loader: sun.misc.Launcher$AppClassLoader@1a52fdf