JDK-6176415 : REGRESSION: Resources with spaces in their names are not found in JAR files
  • Type: Bug
  • Component: core-libs
  • Sub-Component: java.net
  • Affected Version: 5.0
  • Priority: P3
  • Status: Closed
  • Resolution: Duplicate
  • OS: windows_xp
  • CPU: x86
  • Submitted: 2004-10-08
  • Updated: 2010-04-03
  • Resolved: 2005-06-15
The Version table provides details related to the release that this issue/RFE will be addressed.

Unresolved : Release in which this issue/RFE will be addressed.
Resolved: Release in which this issue/RFE has been resolved.
Fixed : Release in which this issue/RFE has been fixed. The release containing this fix may be available for download as an Early Access Release or a General Availability Release.

To download the current JDK release, click here.
JDK 6
6Resolved
Related Reports
Duplicate :  
Description
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

Comments
EVALUATION I can reproduce this problem with Tiger update 1, but not with Tiger update 2. $ jver 6.0 java -cp problem.jar Bug OK: 'filename with space.txt' found. OK: 'filenamewithnospace.txt' found. $ jver 5.0u2 java -cp problem.jar Bug OK: 'filename with space.txt' found. OK: 'filenamewithnospace.txt' found. $ jver 5.0u1 java -cp problem.jar Bug ERROR: 'filename with space.txt' not found. OK: 'filenamewithnospace.txt' found. Looking at the bug fixes for Tiger update 2, I see a fix for 5103449: REGRESSION: getResourceAsStream is broken in JDK1.5.0-rc Assigning to net team for probable closure as a dup of 5103449 ###@###.### 2005-1-13 04:40:00 GMT
13-01-2005