Created
May 29, 2013 01:36
-
-
Save zwang/5667392 to your computer and use it in GitHub Desktop.
Load a class dynamically in Java
This file contains hidden or 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
private Class<?> clazz = null; | |
public MonitorResinThread(Timer timer) { | |
super(timer, 1); | |
//Make sure the class is only load once | |
try { | |
String homePath = "/Home/folder/to/jar"; | |
File file = new File(homePath + "/lib/resin.jar"); | |
URL url = new URL("jar", "", "file:" + file.getAbsolutePath() + "!/"); | |
URL[] urls = new URL[] { url }; | |
ClassLoader loader = URLClassLoader.newInstance(urls, getClass() | |
.getClassLoader()); | |
this.clazz = Class.forName("com.class.name", | |
true, loader); | |
} catch (Exception e) { | |
this.clazz = null; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment