JDK-6351751 : File.toURL() [deprecated] generates non conforming file url
  • Type: Bug
  • Component: core-libs
  • Sub-Component: java.io
  • Affected Version: 1.4.2,6
  • Priority: P3
  • Status: Closed
  • Resolution: Not an Issue
  • OS: generic,windows_xp
  • CPU: generic,x86
  • Submitted: 2005-11-17
  • Updated: 2012-01-11
  • Resolved: 2006-08-25
Related Reports
Relates :  
Relates :  
Description
FULL PRODUCT VERSION :
java version "1.5.0_04"
Java(TM) 2 Runtime Environment, Standard Edition (build 1.5.0_04-b05)
Java HotSpot(TM) Client VM (build 1.5.0_04-b05, mixed mode, sharing)

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

A DESCRIPTION OF THE PROBLEM :
The java.io.File toURL() method generates file URL's that seem to not conform to the governing RFC 1738 (section 3.10), which specifies. "A file URL takes the form: file://<host>/<path>", where <host> may be left empty for local file systems, example (windows):
file:///C:/temp/text.txt

The toURL() method of java.io.File generates a file URL of the form:
"file:<host>/<path>" (with host being left empty), example:
file:/C:/temp/text.txt

Refer to http://www.faqs.org/rfcs/rfc1738.html

This issue is not in any way related to Bug ID: 4723726 (except that both may be generating non-conforming file URL's).

After looking at the source for java.net.URL and java.net.URI, I have
identified the cause of the invalid (non-conformant) file URL's being
generated.
The errors occur in the URL.toString()/URI.toString() underlying
methods, i.e. java.net.URLStreamHandler.toExternalForm(URL u) and
java.net.URI.defineString() respectively.


STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
Run the minimal application listed below and observe its console output.




EXPECTED VERSUS ACTUAL BEHAVIOR :
EXPECTED -
Output of the above application expected to be:

File C:\temp\text.txt has url file:///C:/temp/text.txt

(note the triple slash after file:)
ACTUAL -
Output of the above application is:

File C:\temp\text.txt has url file:/C:/temp/text.txt

(note the single slash after file:)

REPRODUCIBILITY :
This bug can be reproduced always.

---------- BEGIN SOURCE ----------
import java.io.File;
import java.net.MalformedURLException;
import java.net.URL;
public class FileUrl {
    public static void main(String[] args) throws MalformedURLException {
        File file = new File("C:/temp/text.txt");
        URL url = file.toURL();
        System.out.println("File " + file + " has url " + url);
    }
}

---------- END SOURCE ----------

CUSTOMER SUBMITTED WORKAROUND :
This bug generally has no implications, although some protocol handlers may treat the file URL's generated by java.io.toURL() as having an illegal syntax.

Comments
EVALUATION The proposed changes to File.toURI() will change UNC handling as the server name will be in the authority component rather than the path component. However, the spec for File.toURI is very clear that for a file URL, the authority component is null: @return An absolute, hierarchical URI with a scheme equal to <tt>"file"</tt>, a path representing this abstract pathname, and undefined authority, query, and fragment components Closing this issue as not a bug.
25-08-2006

EVALUATION Contribution-Forum:https://jdk-collaboration.dev.java.net/servlets/ProjectForumMessageView?messageID=11489&forumID=1463
16-02-2006

EVALUATION If to fix this bug, make sure that: File.toURL() File.toURI.toURL() all yield the same result. It'll be nice if new URL("file:/c:/temp/text.txt").toString() also outputs file:///c:/temp/text.txt This is desired behavior, like a decent browser does.
05-12-2005

EVALUATION See comment.
30-11-2005