United StatesChange Country, Oracle Worldwide Web Sites Communities I am a... I want to...
JDK-4523159 : getResourceAsStream on jars in path with "!"

Details
Type:
Bug
Submit Date:
2001-11-06
Status:
Open
Updated Date:
2013-01-03
Project Name:
JDK
Resolved Date:
Component:
core-libs
OS:
windows_nt,windows_xp,windows_2000
Sub-Component:
java.net
CPU:
x86
Priority:
P4
Resolution:
Unresolved
Affected Versions:
1.3.0,1.4.0,5.0
Targeted Versions:

Related Reports
Duplicate:
Duplicate:
Relates:
Relates:
Relates:

Sub Tasks

Description
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 ) );
    in.close();
    return properties;
  }

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

props.txt:
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
(InputStreamReader.java:339)
        at java.io.InputStreamReader$CharsetFiller.fill
(InputStreamReader.java:374)
        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
advertised:

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:

jar:file:/C:/test!/PathTest.jar!/props.txt

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) 
======================================================================

                                    

Comments
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
                                     
2004-06-11



Hardware and Software, Engineered to Work Together