Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can I get an URLConnection for a file within a JAR within a non-exploded WAR?

I have some resource files that are in the classpath of my web application (inside a JAR, if that matters). I know I can access the contents of those resources by calling e.g. SomeClassLoader.getResourceAsStream( "/samples/myscript.txt" ). I have tested this in the past in both exploded and non-exploded WAR files with success.

However, to integrate with some other existing classes in my app, I need to provide an URLConnection object to that file. I tested and confirmed that calling getResource("/samples/myscript.txt").openConnection() works in exploded WARs (additionaly, debugging revealed that the result is a file:/// URL to the exploded file).

Question: will that method also work on non-exploded ("packaged?") WARs?

(I currently do not have easy access to a server that deploys wars without exploding them, hence why I'm asking instead of outright trying it. Also, some servers (e.g. Jetty, Tomcat -- even with unpackWARs="false") allows non-exploded deployments, but behind the scenes they unpack the war, effectively behaving like an exploded deployment -- and, evidently, working correctly. I think the servers that gave me trouble in the past were Websphere and Weblogic).

like image 469
st.never Avatar asked Dec 09 '25 03:12

st.never


1 Answers

I don't believe so. To do that, you'd have to use a JarUrlConnection for a JAR URL whose underlying URL was another JAR URL. If i try that, i get:

java.net.MalformedURLException: no !/ in spec

'spec' is what JarUrlConnection calls the path that refers to the file inside the JAR. It seems that for a URL like jar:jar:file:///outer.jar!/inner.jar!/myscript.txt, it chops off the spec at the first exclamation mark, and then rejects inner.jar!/myscript.txt as a spec. When really, we'd like it to chop off the spec at the last exclamation path, unwrapping the inner URL (which refers to the outer JAR!) to use as a base. Unfortunate. I can't think of any way round this.

like image 148
Tom Anderson Avatar answered Dec 10 '25 17:12

Tom Anderson



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!