14

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.

user710818
  • 23,228
  • 58
  • 149
  • 207

2 Answers2

8

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.

adarshr
  • 61,315
  • 23
  • 138
  • 167
  • 1
    Yes, because there is no physical file in that location. You'll have to use JAR API if you want to read the contents of a JAR file. Or, set your JAR file in classpath and you will be directly be able to read `conf/configuration.xml`. – adarshr Mar 02 '12 at 09:12
6

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.

Jerome
  • 4,472
  • 4
  • 18
  • 18