Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why ContextClassLoader returns path with exclamation character?

I try to open file in jar in WEB-INF/lib with

Thread.currentThread().getContextClassLoader();
 URL url=classLoader.getResource(myconfig);

In debugger I can see:

jar:file:/C:/apache-tomcat/webapps/mywebapp/WEB-INF/lib/myjarresource.jar! 
 /conf/configuration.xml

Why in file path is "!" ? I think for this reason application cannot open this file. How to receive correct path? Thanks.

like image 822
user710818 Avatar asked Mar 02 '12 09:03

user710818


2 Answers

It means whatever comes after the ! is inside the JAR file.

In case of myjarresource.jar!/conf/configuration.xml, open up myjarresource.jar using a compression utility such as 7-zip and you will see that it contains conf/configuration.xml.

like image 149
adarshr Avatar answered Nov 15 '22 13:11

adarshr


In the JarURLConnection javadoc , the syntax of a JAR URL is described:

The syntax of a JAR URL is: jar:!/{entry}

So '!' indicates that you 'enter' the java archive.

Edit: I believe that you cannot do File file=new File(url.toURI()) because of the ":" which appears twice in the generated URI and is not compliant with the URI specifications(chapter 2.2 and 3) therefore this is rejected in the File ctor.

like image 20
Jerome Avatar answered Nov 15 '22 12:11

Jerome