United StatesChange Country, Oracle Worldwide Web Sites Communities I am a... I want to...
Bug ID: JDK-4466485 getClass().getResource().getFile() returns file name with %20
JDK-4466485 : getClass().getResource().getFile() returns file name with %20

Details
Type:
Bug
Submit Date:
2001-06-05
Status:
Closed
Updated Date:
2002-11-27
Project Name:
JDK
Resolved Date:
2002-11-27
Component:
core-libs
OS:
windows_xp,windows_2000
Sub-Component:
java.lang
CPU:
x86
Priority:
P4
Resolution:
Won't Fix
Affected Versions:
1.4.0
Fixed Versions:

Related Reports
Duplicate:

Sub Tasks

Description

Name: bsC130419			Date: 06/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)


Prior to jdk1.4 beta (build 1.4.0-beta-b65), getClass().getResource
("myfile").getFile() would return the location of "myfile" which then could be
used to read "myfile".  For e.g. it would return /C:/Documents and
Settings/myfile


But in jdk1.4 beta (build 1.4.0-beta-b65) getClass().getResource
("myfile").getFile() returns the location of myfile that has spaces translated
into %20 characters.  So, now it returns something like /C:/Documents%20and%
20Settings/myfile    This cannot be used as a valid file name in java.io.File,
or java.io.FileInputStream classes

Existing code that uses getClass().getResource("myfile").getFile() does not
work in jdk1.4 beta (build 1.4.0-beta-b65).




------------------------------------------------------------------

import java.net.*;
import java.io.*;


public class GetClassGetResourceGetFileNameBug {

	public void testReadingFileWithCustomBuffering() {
		try {
			URL url = getClass().getResource("webster-
dictionary.txt");
			String fileName = url.getFile();
			System.out.println("File name of the resource "+
fileName);

			FileInputStream fis = new FileInputStream(fileName);
			byte buf[] = new byte[2048];
			int cnt = 0;	// number of lines
			int n;
			while((n = fis.read(buf)) != -1) {
				for(int i = 0; i < n; i++) {
					if(buf[i] == '\n') {
						cnt++;
					}
				}
			}
			fis.close();
			System.out.println("\t\t# lines read : " + cnt);
		} catch(IOException e) {
			System.err.println(e);
		}
	}



	public static void main(String args[]) {
		(new GetClassGetResourceGetFileNameBug
()).testReadingFileWithCustomBuffering();
	}	 // end main


}  // end class


----------------------------------------------------------

C:\Documents and Settings\akjain\DESKTOP\performance>c:\jdk1.4\bin\java -cp . Ge
tClassGetResourceGetFileNameBug

File name of the resource /C:/Documents%20and%20Settings/akjain/DESKTOP/performa
nce/webster-dictionary.txt

java.io.FileNotFoundException: /C:/Documents%20and%20Settings/akjain/DESKTOP/per
formance/webster-dictionary.txt (The system cannot find the path specified)


---------------------------------------------------

  To run this program you will have to put (any text file/fake) "webster-
dictionary.txt" file in the directory where your java class is located.


-----------------------------------------------------------
(Review ID: 125856) 
======================================================================

                                    

Comments
WORK AROUND



Name: bsC130419			Date: 06/05/2001


None.  You will have to manually parse the file location string returned by
getClass().getResource("myfile").getFile() to translate %20 to space characters.
======================================================================

No, there is a workaround.  Instead of doing this:

    FileInputStream fis = new FileInputStream(url.getFile());

you can force any %-escaped characters to be decoded by first converting the
URL to a URI, and then using the path component of the URI as the filename:

    URI uri = new URI(url.toString());
    FileInputStream fis = new FileInputStream(uri.getPath());

-- ###@###.### 2001/2/13
                                     
179-09-02 0
EVALUATION

The current behavior is correct.  Spaces in URLs must be %-escaped.  It was a
bug (4359123) that the URLs returned by the Class.getResource method were not
being escaped in previous releases.

-- ###@###.### 2002/2/13

The current behavior will not be changed.

The root cause of this bug is the fix for 4359123 (NoClassDefFoundError if '#'
anywhere in path), a fix for which was strongly desired by several parts of the
Java Community.

After extensive research the fix that we came up with for that bug was the
least incompatible change we could find that still fixed the problem in a
sensible way.

We apologize for any difficulties this may have caused, but we are not going
to change the behavior of URL objects returned by the Class.getResource()
method yet again.  We strongly recommend that new code use the java.net.URI
class whenever it's necessary to deal with URL-like strings.

-- ###@###.### 2002/11/26
                                     
192-10-11 0



Hardware and Software, Engineered to Work Together