Uploaded image for project: 'JDK'
  1. JDK
  2. JDK-4523159

getResourceAsStream on jars in path with "!"

XMLWordPrintable

    • Icon: Bug Bug
    • Resolution: Unresolved
    • Icon: P4 P4
    • None
    • 1.3.0, 1.4.0, 5.0
    • core-libs
    • Cause Known
    • x86
    • windows_nt, windows_2000, windows_xp

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

            michaelm Michael McMahon
            nthompsosunw Nathanael Thompson (Inactive)
            Votes:
            0 Vote for this issue
            Watchers:
            2 Start watching this issue

              Created:
              Updated:
              Imported:
              Indexed: