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

File.isAbsolute() does not cover all cases

XMLWordPrintable

    • Icon: Bug Bug
    • Resolution: Not an Issue
    • Icon: P4 P4
    • None
    • 8u45
    • core-libs
    • x86_64
    • windows_7

      A DESCRIPTION OF THE REQUEST :
      As documented in http://docs.oracle.com/javase/8/docs/api/java/io/File.html#isAbsolute-- , the isAbsolute function:
      Tests whether this abstract pathname is absolute. The definition of absolute pathname is system dependent. On UNIX systems, a pathname is absolute if its prefix is "/". On Microsoft Windows systems, a pathname is absolute if its prefix is a drive specifier followed by "\\", or if its prefix is "\\\\".
      However, Windows accepts both forward and backward slash as prefix, as documented in:
      https://msdn.microsoft.com/en-us/library/aa365247(VS.85).aspx#fully_qualified_vs._relative_paths



      JUSTIFICATION :
      Using / as the root directory makes Java applications even more cross-platform, given that the code would not need to cares about the drive letter.

      EXPECTED VERSUS ACTUAL BEHAVIOR :
      EXPECTED -
      After using:
      File source=new File("/foo.bar")
      The instruction source.isAbsolute() would return true also on Windows.
      ACTUAL -
      After using:
      File source=new File("/foo.bar")
      The instruction source.isAbsolute() returns false.

      ---------- BEGIN SOURCE ----------
      import java.io.File;

      public class test {
          
           public static void main(String[] args) {
                      final String path = "/foo.bar";
      File source = new File( path );

                  if (source.isAbsolute())
                  {
      System.out.println(path+" IS an absolute path!");
                  } else
      {
      System.out.println(path+" is not an absolute path!");
      }
          }

      }
      ---------- END SOURCE ----------

      CUSTOMER SUBMITTED WORKAROUND :
      import java.io.File;

      public class test {
          
           public static void main(String[] args) {
                      final String path = "/foo.bar";
      File source = new File( path );

                  if (source.isAbsolute() || source.getPath().charAt(0) == "/" || source.getPath().charAt(0) == "\\")
                  {
      System.out.println(path+" IS an absolute path!");
                  } else
      {
      System.out.println(path+" is not an absolute path!");
      }
          }

      }

            robm Robert Mckenna
            webbuggrp Webbug Group
            Votes:
            0 Vote for this issue
            Watchers:
            4 Start watching this issue

              Created:
              Updated:
              Resolved: