How do I get just the jar URL from a jar: URL containing a! and a specific file in the jar?

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()).

I am going with exactly this solution, although I wish there was a way to do this without opening the jar (or parsing directly, of course); seems like URL needs to understand this weird! Syntax, or else a subclass needs to exist which understands it. – skiphoppy Jul 15 '09 at 17:23 (In Java7) JarURLConnection doesn't open the JAR file when you execute the above code.It just parses the URL on your behalf.

– Gili Nov 4 at 18:36.

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", parts0); } } Hope this helps.

Very close, but the usage of split() is too much low-level parsing for me. Sun's URL implementation doesn't seem to provide methods to deal with this weird jarfile. Jar!

Path/to/specific/file syntax for Sun's weird jar URLs; the whole shebang (pun intended) is returned by getPath(). But JarURLConnection, as starblue mentioned, seems to work, although only through opening up the jar. – skiphoppy Jul 15 '09 at 17:22.

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('! ')).

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('!')); ...

1 Because it is generally better to use existing libraries. – starblue Dec 31 '08 at 11:19 Because I know diddly-squat about the format of this bizarre jar:file:// URL I am dealing with, which Sun invented and doesn't appear to have documented, and so therefore I can't possibly know if my parsing will work in all circumstances. At least if I use Sun's Java URL libraries, I have a slight chance that it'll work for more cases I might encounter.Maybe.

If the libraries are good. They'll be more widely used and tested than anything I write, that's for sure. – skiphoppy Jul 15 '09 at 16:03 I voted you up because this is an important question, even though it is definitely a bad suggestion.

– skiphoppy Jul 15 '09 at 17:24 1 There are other ways to raise important questions, aside from telling one to "continue with his life" – Leonel Jul 15 '09 at 17:53.

I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.

Related Questions