Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I convert a "jar:file" URI to the path of Jar file?

Tags:

java

uri

How do I go from jar:file:/C:/Program%20Files/test.jar!/foo/bar to a File that points to C:/Program Files/test.jar?

like image 584
Gili Avatar asked Nov 04 '11 18:11

Gili


2 Answers

The following code works for me (based on How do I get just the jar URL from a jar: URL containing a "!" and a specific file in the jar?):

URL url = new URL("jar:file:/C:/Program%20Files/test.jar!/foo/bar");
JarURLConnection connection = (JarURLConnection) url.openConnection();
File file = new File(connection.getJarFileURL().toURI())
like image 185
Gili Avatar answered Oct 12 '22 22:10

Gili


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);
}

File required = new File(FilePath[1]);

like image 20
Hari Chandu Vakacharla Avatar answered Oct 13 '22 00:10

Hari Chandu Vakacharla