Skip to content

Instantly share code, notes, and snippets.

@tom-code
Last active September 22, 2017 00:45
Show Gist options
  • Save tom-code/adb9e846e4bc87152453dfc2b8e91651 to your computer and use it in GitHub Desktop.
Save tom-code/adb9e846e4bc87152453dfc2b8e91651 to your computer and use it in GitHub Desktop.
classloader - two jars with different versions of same classes
public class Main {
public static void main(String[] args) {
Main m = new Main();
TestClassIface t1 = m.load("test/1.jar");
TestClassIface t2 = m.load("test/2.jar");
t1.execute();
t2.execute();
}
TestClassIface load(String path) {
try {
System.out.println(new File(path).canRead());
ClassLoader cl1 = new URLClassLoader(new URL[] { new File(path).toURL()});
Class<?> cls = cl1.loadClass("Test");
return (TestClassIface) cls.newInstance();
} catch (MalformedURLException | IllegalAccessException | ClassNotFoundException | InstantiationException e) {
e.printStackTrace();
}
return null;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment