For some reason, I want to get all loaded jars' file path. Previously on java8, I have done this by 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 connot cast to URLClassLoader.
Currently I am using SomeClass.class.getProtectDomain().getCodeSource().getLocation(), but this costs much, so I am here asking for some more gentle way to do this.