Last active
September 22, 2017 00:45
-
-
Save tom-code/adb9e846e4bc87152453dfc2b8e91651 to your computer and use it in GitHub Desktop.
classloader - two jars with different versions of same classes
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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