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

File.toURL() returns wrong result for UNC pathname (win)

XMLWordPrintable

    • Icon: Bug Bug
    • Resolution: Duplicate
    • Icon: P3 P3
    • None
    • 9.1pe
    • core-libs
    • None
    • generic
    • generic

      I am using JDK 6 update 1 on Windows. Test case looks like this:
      public class Test {
       public static void main(String[] s) {
          File f = new File(args[0]);
          System.out.println("f = " + f);
          System.out.println("f.toURL() = " + f.toURL());
          System.out.println("f.toURI() = " + f.toURI());
          new File(f.toURL().toURI())
       }
      }

      When I invoke 'java Test \\sm-unwk-01.sfbay.sun.com\ss141213\foo.xml', I get following result:

      f = \\sm-unwk-01.sfbay.sun.com\ss141213\foo.xml
      f.toURL() = file://sm-unwk-01.sfbay.sun.com/ss141213/foo.xml
      f.toURI() = file:////sm-unwk-01.sfbay.sun.com/ss141213/foo.xml
      Exception in thread "main" java.lang.IllegalArgumentException: URI has an author
      ity component
              at java.io.File.<init>(File.java:368)
              at Test.main(Test.java:22)

      Please note, f.toURL() is *not* same as f.toURI() in the above output. f.toURL() has an authority component, where as f.toURI() does not have any authority component. I don't think there should be any authority component in that URL.

      The above code was just to demonstrate the bug. We are facing this issue in a different context [1]. We are trying to get a File object from a URL when the scheme of the URL is file. The URL is obtained by calling ClassLoader.getResource(). ClassLoader.getResource() is returning file://sm-unwk-01.sfbay.sun.com/ss141213/foo.xml, which has an authority component set. So we get exception in our code. Our code looks like this:
      void foobar() {
       URL url = ClassLoader.getSystemResource("foo.xml");
       if("file".equals(url.getProtocol()) {
          File f = new File(url.toURI());
          ...
       }
      }

      [1] https://glassfish.dev.java.net/issues/show_bug.cgi?id=3209

            iris Iris Clark
            sanjsaho Sanjeeb Sahoo
            Votes:
            0 Vote for this issue
            Watchers:
            0 Start watching this issue

              Created:
              Updated:
              Resolved:
              Imported:
              Indexed: