出于某种原因,我想获得所有加载的jar文件路径.以前在java8上,我通过使用完成了这个
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();
}
}
Run Code Online (Sandbox Code Playgroud)
但是在Java 9中,SystemClassLoader无法转换为URLClassLoader.
目前我正在使用SomeClass.class.getProtectDomain().getCodeSource().getLocation(),但这花费很多,所以我在这里要求一些更温和的方法来做到这一点.