JDK-6219961 : URI.resolve() doesn't handle file paths correctly
  • Type: Bug
  • Component: core-libs
  • Sub-Component: java.net
  • Affected Version: 5.0,6
  • Priority: P4
  • Status: Closed
  • Resolution: Won't Fix
  • OS: windows_xp,windows_vista
  • CPU: x86
  • Submitted: 2005-01-21
  • Updated: 2018-12-07
  • Resolved: 2018-11-14
Related Reports
Relates :  
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)

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

A DESCRIPTION OF THE PROBLEM :
An exception is thrown where one should not be.

STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
The following code throws an exception:
import java.net.*;

/**
 *
 * @author kitfox
 */
public class NewMain
{
    
    /** Creates a new instance of NewMain */
    public NewMain()
    {
    }
    
    /**
     * @param args the command line arguments
     */
    public static void main(String[] args)
    {
        try
        {
            URI uri = new URI("file:/C:/tmp/imagefade.svg");
            String frag = "C:\\svg\\test.png";
            URI uri2 = uri.resolve(frag);
            System.err.println("" + uri2);
        }
        catch (Exception e)
        {
            e.printStackTrace();
        }
    }
    
}


EXPECTED VERSUS ACTUAL BEHAVIOR :
EXPECTED -
uri2 should be set to "c:/svg/test.png"

ERROR MESSAGES/STACK TRACES THAT OCCUR :
init:
javacc:
compile:
Compiling 1 source file to C:\dev\cvs.dev.java.net\svgsalamander\classes
run-selected-files:
java.lang.IllegalArgumentException
    at java.net.URI.create(URI.java:842)
    at java.net.URI.resolve(URI.java:1028)
    at com.NewMain.main(NewMain.java:32)
Caused by: java.net.URISyntaxException: Illegal character in opaque part at index 2: C:\svg\test.png
    at java.net.URI$Parser.fail(URI.java:2809)
    at java.net.URI$Parser.checkChars(URI.java:2982)
    at java.net.URI$Parser.parse(URI.java:3019)
    at java.net.URI.<init>(URI.java:578)
    at java.net.URI.create(URI.java:840)
    ... 2 more
BUILD SUCCESSFUL (total time: 0 seconds)


REPRODUCIBILITY :
This bug can be reproduced always.

---------- BEGIN SOURCE ----------
/**
 *
 * @author kitfox
 */
public class NewMain
{
    
    /** Creates a new instance of NewMain */
    public NewMain()
    {
    }
    
    /**
     * @param args the command line arguments
     */
    public static void main(String[] args)
    {
        try
        {
            URI uri = new URI("file:/C:/tmp/imagefade.svg");
            String frag = "C:\\svg\\test.png";
            URI uri2 = uri.resolve(frag);
            System.err.println("" + uri2);
        }
        catch (Exception e)
        {
            e.printStackTrace();
        }
    }
    
}

---------- END SOURCE ----------
###@###.### 2005-1-21 06:33:14 GMT

Comments
This is a user error. The String frag = "C:\\svg\\test.png"; is not a valid URI. The backslash character is not legal in URI. The correct code should look something like: URI uri = new URI("file:/C:/tmp/imagefade.svg"); String frag = "C:\\svg\\test.png"; URI uri2 = Path.of(frag).toURI(); URI uri3 = uri.resolve(uri2); Note that because URI has no knowledge of the file: scheme, and of the peculiarity of windows drives or UNC path, it might be more prudent to first convert the base and relative URIs into Paths or Files, then resolve one path/file against the other and eventually create a new URI from the resulting path/file. Or at least assert that the path when using drive:path syntax is an absolute path.
14-11-2018