How do I get just the jar URL from a jar: URL cont

2019-02-11 17:37发布

I get a jar file url at runtime as:

jar:file:///C:/proj/parser/jar/parser.jar!/test.xml

How can this be converted to a valid path as:

C:/proj/parser/jar/parser.jar.

I have already tried using File(URI), getPath(), getFile() in vain.

7条回答
我只想做你的唯一
2楼-- · 2019-02-11 18:18

You can do this.This works

    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);
    }
    return filePath[1];
查看更多
登录 后发表回答