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

REGRESSION: Resources with spaces in their names are not found in JAR files

XMLWordPrintable

    • Icon: Bug Bug
    • Resolution: Duplicate
    • Icon: P3 P3
    • 6
    • 5.0
    • core-libs
    • x86
    • windows_xp

      FULL PRODUCT VERSION :
      java version "1.5.0"
      Java(TM) 2 Runtime Environment, Standard Edition (build 1.5.0-b64)
      Java HotSpot(TM) Client VM (build 1.5.0-b64, mixed mode, sharing)

      ADDITIONAL OS VERSION INFORMATION :
      Microsoft Windows XP [Version 5.1.2600]

      A DESCRIPTION OF THE PROBLEM :
      If a class uses getResourceAsStream (...) and passes a path that includes spaces in the name, the resource will not be found if loaded from a JAR file, but WILL be found if loaded from the filesystem.

      STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
      Create a class that loads two files using .class.getResourceAsStream. One file should have no spaces in its name, the other should. Run the program and verify it finds both resources. Bundle the class and the two files into a JAR file and run it from there, verify it can find the file with no spaces in its name but not the one with spaces.

      The sample Java file I sent should be placed in a folder named "problem".

        problem\LoadResource.java

      Also create two files (they can be empty):

        problem\filename with space.txt
        problem\filenamewithnospace.txt

      Compile the .java file. If using 1.5 be sure to use "-source 1.4", so you'll
      be able to run it under 1.4:

        javac -source 1.4 problem\*.java

      Then create the JAR file:

       jar cvfm problem.jar problem\manifest.mf problem

      Now run it under Java 1.4 and verify it works, whether using the JAR file or
      the filesystem to load the resources:

      a) Using file system:

        c:\j2sdk1.4.2_05\bin\java.exe -cp . problem.LoadResource
        OK: 'filename with space.txt' found.
        OK: 'filenamewithnospace.txt' found.

      b) Using JAR file:

        c:\j2sdk1.4.2_05\bin\java.exe -cp problem.jar problem.LoadResource
        OK: 'filename with space.txt' found.
        OK: 'filenamewithnospace.txt' found.

      Now run it under Java 1.5RC0 and verify it works using the filesystem, but not
      from the JAR file (note: on my system JDK1.5RC0 is in the PATH):

      a) Using file system:

        java -cp . problem.LoadResource
        OK: 'filename with space.txt' found.
        OK: 'filenamewithnospace.txt' found.

      b) Using JAR file:

        java -cp problem.jar problem.LoadResource
        ERROR: 'filename with space.txt' not found.
        OK: 'filenamewithnospace.txt' found.

      That should reproduce it.


      EXPECTED VERSUS ACTUAL BEHAVIOR :
      EXPECTED -
      getResourceAsStream should work consistently whether your application is run from a directory (eg, unpacked JAR) or a JAR.
      ACTUAL -
      Any resources with spaces in their names are not found under JDK1.5 RC0, using existing JAR's that work fine under JDK 1.4.2. Resources without spaces are found by 1.5.

      REPRODUCIBILITY :
      This bug can be reproduced always.

      ---------- BEGIN SOURCE ----------
      manifest.mf
      -----------
      Manifest-version: 1.0
      Main-Class: problem.LoadResource


      LoadResource.java
      -----------------
      package problem;
      import java.io.InputStream;

      /**
       * Demonstrates a problem in JDK1.5 RC0, that resources with spaces in their
       * names will not be found if loaded from a JAR file, but are found just fine
       * when not in a JAR file.
       */
      public class LoadResource {

      public static void main (String args[]) throws Exception {
      loadResource ("filename with space.txt");
      loadResource ("filenamewithnospace.txt");
      }

      public static void loadResource (String name) throws Exception {
      InputStream stream =
      LoadResource.class.getResourceAsStream (name);
      if (stream == null) {
      System.out.println ("ERROR: '" + name + "' not found.");
      } else {
      System.out.println ("OK: '" + name + "' found.");
      stream.close ();
      }
      }
      }
      ---------- END SOURCE ----------

      CUSTOMER SUBMITTED WORKAROUND :
      Convert all resources bundled in all Java programs so that the path names do not include a space (!).

      Release Regression From : 1.4.2
      The above release value was the last known release where this
      bug was known to work. Since then there has been a regression.

      Release Regression From : 1.4.2
      The above release value was the last known release where this
      bug was known to work. Since then there has been a regression.
      ###@###.### 10/8/04 20:34 GMT

            michaelm Michael McMahon
            jleesunw Jon Lee (Inactive)
            Votes:
            0 Vote for this issue
            Watchers:
            1 Start watching this issue

              Created:
              Updated:
              Resolved:
              Imported:
              Indexed: