如何将“jar:file” URI转换为Jar文件的路径?

7

我该如何将jar:file:/C:/Program%20Files/test.jar!/foo/bar转换为指向C:/Program Files/test.jarFile对象?

2个回答

13

6
你可以做到这一点。这很有效。
ClassLoader loader = this.getClass().getClassLoader();
URL url = loader.getResource("resource name");
String[] filePath = null;
String protocol = url.getProtocol();
if(protocol.equals("jar")){
    url = new URL(url.getPath());
    protocol = url.getProtocol();
}
if(protocol.equals("file")){
    String[] pathArray = url.getPath().split("!");
    filePath = pathArray[0].split("/",2);
}
File required = new File(FilePath[1]);

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接