Skip to content

Instantly share code, notes, and snippets.

@zwang
Created May 29, 2013 01:36
Show Gist options
  • Save zwang/5667392 to your computer and use it in GitHub Desktop.
Save zwang/5667392 to your computer and use it in GitHub Desktop.
Load a class dynamically in Java
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