Skip to content

Instantly share code, notes, and snippets.

@tylertreat
Created December 31, 2014 21:32
Show Gist options
  • Select an option

  • Save tylertreat/e0fb625448a387998347 to your computer and use it in GitHub Desktop.

Select an option

Save tylertreat/e0fb625448a387998347 to your computer and use it in GitHub Desktop.
private void addClasspathDependencies() {
String workingDir = System.getProperty("user.dir");
// We need to add these libs to appease the ClassLoader Gods
File lib = new File(workingDir + File.separator + "lib");
for (File file : lib.listFiles())
addToClasspath(file);
// Add bin-test to the class path -- this is where the stubs are
File binTest = new File(workingDir + File.separator + "bin-test");
addToClasspath(binTest);
}
private void addToClasspath(File file) {
if (!file.exists()) {
logger.error("Could not add " + file.getName() + " to classpath (does not exist)");
return;
}
URLClassLoader classLoader = (URLClassLoader) Thread.currentThread().getContextClassLoader();
try {
// Instead of subclassing URLClassLoader, let's just use reflection
URL url = file.toURI().toURL();
Method method = URLClassLoader.class.getDeclaredMethod("addURL", URL.class);
method.setAccessible(true);
method.invoke(classLoader, url);
} catch (Throwable tr) {
logger.error("Could not add " + file.getName() + " to classpath", tr);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment