-->

Why ContextClassLoader returns path with exclamati

2020-08-18 05:44发布

问题:

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.

回答1:

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.



回答2:

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.