For some reason I want to get the file path of the downloaded jars. Earlier on java8 I did this using
for (java.net.URL url : ((java.net.URLClassLoader) A.class.getClassLoader()).getURLs()) {
try {
String path = url.toString();
if (path.startsWith("file:/"))
path = path.substring(6);
path = java.net.URLDecoder.decode(path, "UTF-8");
} catch (Exception e) {
e.printStackTrace();
}
}
But in Java 9, the SystemClassLoader compiler is added to the URLClassLoader.
I am currently using SomeClass.class.getProtectDomain().getCodeSource().getLocation()
, but it is expensive, so here I am asking for a milder way to do this.
source
share