Created
November 29, 2017 18:44
-
-
Save ShmuelMofrad/843de01e50d2d2c5aa0c08acb32c097c to your computer and use it in GitHub Desktop.
How to get classpath from classloader?
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
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()); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.