JDK-4523159 : getResourceAsStream on jars in path with "!"
  • Type: Bug
  • Component: core-libs
  • Sub-Component: java.net
  • Affected Version: 1.3.0,1.4.0,5.0
  • Priority: P4
  • Status: Open
  • Resolution: Unresolved
  • OS: windows_nt,windows_2000,windows_xp
  • CPU: x86
  • Submitted: 2001-11-06
  • Updated: 2017-07-31
Related Reports
Duplicate :  
Duplicate :  
Relates :  
Relates :  
Relates :  
Name: nt126004			Date: 11/05/2001

java version "1.4.0-beta"
Java(TM) 2 Runtime Environment, Standard Edition (build 1.4.0-beta-b65)
Java HotSpot(TM) Client VM (build 1.4.0-beta-b65, mixed mode)

Issuing a getResourceAsStream() call will throw an exception if
a) classes and resources are inside a JAR file, and
b) the JAR file is located in a directory containing an exclamation mark ("!"),
like "c:\Java!"

Steps to reproduce:

Java source file:

import java.util.*;
import java.io.*;

public class PathTest {

  public static Properties loadProperties( String fileName ) throws IOException
    Properties properties = new Properties();
    InputStream in = PathTest.class.getResourceAsStream( fileName );
    properties.load( new BufferedInputStream( in ) );
    return properties;

  public static void main( String[] args ) throws IOException {
	  Properties myProperties = loadProperties( "props.txt" );
	  System.out.println( myProperties.getProperty( "HelloWorld", "Property not found!" ));

HelloWorld=Hello World!

1. Compile the source file.
2. Create the jar file PathTest.jar containing PathTest.class and props.txt
3. Put the jar file into directory "c:\Test!"
4. Change into that directory "Test!"
5. Execute PathTest using: java -classpath PathTest.jar PathTest
6. That will produce the following exception:

C:\test!>java -classpath PathTest.jar PathTest
Exception in thread "main" java.io.IOException: Stream closed
        at java.io.BufferedInputStream.ensureOpen(BufferedInputStream.java:123)
        at java.io.BufferedInputStream.read(BufferedInputStream.java:273)
        at java.io.InputStreamReader$CharsetFiller.readBytes
        at java.io.InputStreamReader$CharsetFiller.fill
        at java.io.InputStreamReader.read(InputStreamReader.java:511)
        at java.io.BufferedReader.fill(BufferedReader.java:139)
        at java.io.BufferedReader.readLine(BufferedReader.java:299)
        at java.io.BufferedReader.readLine(BufferedReader.java:362)
        at java.util.Properties.load(Properties.java:195)
        at PathTest.loadProperties(PathTest.java:9)
        at PathTest.main(PathTest.java:15)

7. If executed in a directory named "c:\test" the same jar will work like as

C:\test>java -classpath PathTest.jar PathTest
Hello World!

There are some bugs in the bugbase which describe related problems with jars in
paths containing special characters and they are all marked as fixed. The
exclamation mark problem is still there though. Some quick tests brought up
that the url generated for the stream uses an exclamation mark itself to
specify which file in the jar ist to be loaded, like:


And that seems to trigger the loading of the wrong jar (c:\test.jar) which then
gives the exception.

That bug is really nasty because during development everything works fine (the
error does not occur if the class is not inside a jar), tests work fine
(because the program really does what it should) and when some user installs it
inside a directory containing an exclamation mark: it crashes.

I guess that the bug could easily be fixed by looking for the LAST occurance
of "!/" while parsing the url.

I have not tried it but I guess that it also will give problems to install a
JDK or JRE in directory containig exclamation marks....
(Review ID: 134891) 

Unfortunately, we have to backout the fix for this bug in mustang. Actually, it remains partially fixed. "!" chars anywhere except at the end of a pathname component will work. But as suggested sometime ago, a proper solution to this requires an encoding scheme. We need to change the jar spec. to specify (for example) that "!" characters that are not to be interpeted as part of the jar: URL separator must be encoded as %21 which is the standard percent encoded sequence for "!" as used in the URI RFCs. This will have to wait until dolphin now.

EVALUATION The problem can be reproduced with the latest hopper build java version "1.4.1-beta" Java(TM) 2 Runtime Environment, Standard Edition (build 1.4.1-beta-b10) Java HotSpot(TM) Client VM (build 1.4.1-beta-b10, mixed mode) ###@###.### 2002-05-02 ==================================================================== Actually, the bug can not be trivially fixed by looking for the last occurrance of "!/" as suggested in the description. Consider the case where props.txt is jar'd within a directory with name ending in "!": jar:file:/c:/test!/PathTest.jar!/foo!/props.txt There is a general problem with the use of "!" in file names. See bug 4730642. What we really need is to define quoting syntax. As this would entail a new feature, it is not suitable for a maintenance release (i.e. a "dot dot" release). Will consider for tiger. -- iag@sfbay