Skip to content

Instantly share code, notes, and snippets.

@ShmuelMofrad
Created November 29, 2017 18:44
Show Gist options
  • Save ShmuelMofrad/843de01e50d2d2c5aa0c08acb32c097c to your computer and use it in GitHub Desktop.
Save ShmuelMofrad/843de01e50d2d2c5aa0c08acb32c097c to your computer and use it in GitHub Desktop.
How to get classpath from classloader?
import java.net.URL;
import java.net.URLClassLoader;
public class SeeClassPath {
public static void main(String[] args) {
getClasspathFromClassloader();
}
static void getClasspathFromClassloader() {
ClassLoader cl = ClassLoader.getSystemClassLoader();
URL[] urls = ((URLClassLoader) cl).getURLs();
for (URL url : urls) {
System.out.println(url.getFile());
}
}
}
@eli-fin
Copy link

eli-fin commented Mar 28, 2019

Works on java8. However, it will not work on java9+, as getSystemClassLoader is no longer instanceof URLClassLoader.
Instead, you can get the private member ucp (type URLClassPath) the SystemClassLoader and call getURLs on it.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment