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 17:56

This might do it, if MS-Windows is not offended by a leading slash:

    final URL jarUrl =
        new URL("jar:file:/C:/proj/parser/jar/parser.jar!/test.xml");
    final JarURLConnection connection =
        (JarURLConnection) jarUrl.openConnection();
    final URL url = connection.getJarFileURL();

    System.out.println(url.getFile());
查看更多
男人必须洒脱
3楼-- · 2019-02-11 17:57

Some might consider this to be a bit 'hacky', but it'll do the job in that instance and i'm sure it'd perform a damn sight better than creating all those objects in the other suggestions.

String jarUrl = "jar:file:/C:/proj/parser/jar/parser.jar!/test.xml";

jarUrl = jarUrl.substring(jarUrl.indexOf('/')+1, jarUrl.indexOf('!'));
查看更多
爷、活的狠高调
4楼-- · 2019-02-11 18:02

Not sure of any exact method that will give you what you want, but this should get you close:

import static org.junit.Assert.assertEquals;

import java.net.URL;

import org.junit.Test;

public class UrlTest {

    @Test
    public void testUrl() throws Exception {
        URL jarUrl = new URL("jar:file:/C:/proj/parser/jar/parser.jar!/test.xml");
        assertEquals("jar", jarUrl.getProtocol());
        assertEquals("file:/C:/proj/parser/jar/parser.jar!/test.xml", jarUrl.getFile());
        URL fileUrl = new URL(jarUrl.getFile());
        assertEquals("file", fileUrl.getProtocol());
        assertEquals("/C:/proj/parser/jar/parser.jar!/test.xml", fileUrl.getFile());
        String[] parts = fileUrl.getFile().split("!");
        assertEquals("/C:/proj/parser/jar/parser.jar", parts[0]);
    }
}

Hope this helps.

查看更多
混吃等死
5楼-- · 2019-02-11 18:09
  //This code will work on both Windows and Linux
  public String path()
  {
  URL url1 = getClass().getResource("");
  String urs=url1.toString();
  urs=urs.substring(9);
  String truepath[]=urs.split("parser.jar!");
  truepath[0]=truepath[0]+"parser.jar";
  truepath[0]=truepath[0].replaceAll("%20"," ");
  return truepath[0];
  }
查看更多
对你真心纯属浪费
6楼-- · 2019-02-11 18:11

I just had to do this.

        URL url = clazz.getResource(clazz.getSimpleName() + ".class");
        String proto = url.getProtocol();
        boolean isJar = proto.equals("jar"); // see if it's in a jar file URL

        if(isJar)
        {
            url = new URL(url.getPath()); // this nicely strips off the leading jar: 
            proto = url.getProtocol();
        }

        if(proto.equals("file"))
        {
             if(isJar) 
                // you can truncate it at the last '!' here
        } 
        else if(proto == "http") {
            ...
查看更多
混吃等死
7楼-- · 2019-02-11 18:18

This solution will handle spaces in the path.

String url = "jar:file:/C:/dir%20with%20spaces/myjar.jar!/resource";
String fileUrl = url.substring(4, url.indexOf('!'));
File file = new File(new URL(fileUrl).toURI());
String fileSystemPath = file.getPath();

or with a URL object to begin with:

...
String fileUrl = url.getPath().substring(0, url.indexOf('!'));
...
查看更多
登录 后发表回答