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

Inconsistent path resolution on Windows drive root directories

    Details

    • Type: Bug
    • Status: Open
    • Priority: P4
    • Resolution: Unresolved
    • Affects Version/s: 5.0, 7u9
    • Fix Version/s: tbd
    • Component/s: core-libs
    • Labels:
    • Subcomponent:
    • OS:
      windows_7

      Description

      FULL PRODUCT VERSION :
      java version " 1.7.0_09 "
      Java(TM) SE Runtime Environment (build 1.7.0_09-b05)
      Java HotSpot(TM) 64-Bit Server VM (build 23.5-b02, mixed mode)

      ADDITIONAL OS VERSION INFORMATION :
      Microsoft Windows [Version 6.1.7601]

      A DESCRIPTION OF THE PROBLEM :
      A listing of the files of a root directory (on Windows drives) will return an additional backward slash after the colon, but only for the absolute path.

      While this seems somewhat consistent with the JavaDoc of java.io.File, which states:
       " For Microsoft Windows platforms, the prefix of a pathname that contains a drive specifier consists of the drive letter followed by " : " and possibly followed by " \ " if the pathname is absolute. "
      the result is not correct, as it leads to absolute paths like this:
      k:\\subdir1\FileTest.class


      STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
      - Create a new File instance with a drive letter followed by a colon (e.g. new File( " c: " );)
      - call listFiles() on that instance
      - look at the absolute paths of the returned files

      Compare this to the absolute paths returned for a listing of the sub files of new File ( " c:\ " ).

      EXPECTED VERSUS ACTUAL BEHAVIOR :
      EXPECTED -
      The absolute file paths should be identical, o matter whether the root file object is created for path " c: " or path " c:\ " .
      ACTUAL -
      The absolute file paths differ.

      See output of example code below; note the double backslashes in the paths from " Testing new path: k: " , especially the mix of double and single backslashes in " k:\\subdir1\FileTest.class " .

      === Testing new path: k: ===
          Path: k:
          CanonicalPath: K: AbsolutePath: k: AbsoluteFile.getPath: k: = Testing one sub-path: subdir1 =
          Path: k:\subdir1
          CanonicalPath: K:\subdir1
          AbsolutePath: k:\\subdir1
          AbsoluteFile.getPath: k:\\subdir1
        = Testing one sub-path: FileTest.class =
          Path: k:\subdir1\FileTest.class
          CanonicalPath: K:\subdir1\FileTest.class
          AbsolutePath: k:\\subdir1\FileTest.class
          AbsoluteFile.getPath: k:\\subdir1\FileTest.class
      === Done ===

      === Testing new path: k:\ ===
          Path: k: CanonicalPath: K: AbsolutePath: k: AbsoluteFile.getPath: k: = Testing one sub-path: subdir1 =
          Path: k:\subdir1
          CanonicalPath: K:\subdir1
          AbsolutePath: k:\subdir1
          AbsoluteFile.getPath: k:\subdir1
        = Testing one sub-path: FileTest.class =
          Path: k:\subdir1\FileTest.class
          CanonicalPath: K:\subdir1\FileTest.class
          AbsolutePath: k:\subdir1\FileTest.class
          AbsoluteFile.getPath: k:\subdir1\FileTest.class
      === Done ===

      === Testing new path: k:/ ===
          Path: k: CanonicalPath: K: AbsolutePath: k: AbsoluteFile.getPath: k: = Testing one sub-path: subdir1 =
          Path: k:\subdir1
          CanonicalPath: K:\subdir1
          AbsolutePath: k:\subdir1
          AbsoluteFile.getPath: k:\subdir1
        = Testing one sub-path: FileTest.class =
          Path: k:\subdir1\FileTest.class
          CanonicalPath: K:\subdir1\FileTest.class
          AbsolutePath: k:\subdir1\FileTest.class
          AbsoluteFile.getPath: k:\subdir1\FileTest.class
      === Done ===



      REPRODUCIBILITY :
      This bug can be reproduced always.

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


      public class FileTest
      {
          public static void main(String[] args)
          {
              testDir( " k: " );
              testDir( " k:\\ " );
              testDir( " k:/ " );
          }

          private static void testDir(String path)
          {
              System.out.println( " === Testing new path: " + path + " === " );
              File file = new File(path);
              printFileInfo(file);
              File[] list = file.listFiles();
              if (list!=null && list.length>0)
              {
                  System.out.println( " = Testing one sub-path: " + list[0].getName() + " = " );
                  printFileInfo(list[0]);
              }
              for (int i=0; i<list.length; i++)
              {
                  // try to find non-empty directory
                  File[] subList = list[i].listFiles();
                  if (subList!=null && subList.length>0)
                  {
                      System.out.println( " = Testing one sub-path: " + subList[0].getName() + " = " );
                      printFileInfo(subList[0]);
                      break;
                  }
              }
              System.out.println( " === Done === " );
              System.out.println();
              
          }
          
          private static void printFileInfo(File file)
          {
              System.out.println( " Path: " + file.getPath());
              try
              {
                  System.out.println( " CanonicalPath: " + file.getCanonicalPath());
              }
              catch(IOException e)
              {
                  e.printStackTrace();
              }
              System.out.println( " AbsolutePath: " + file.getAbsolutePath());
              System.out.println( " AbsoluteFile.getPath: " + file.getAbsoluteFile().getPath());
          }
          
      }

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

        Attachments

          Issue Links

            Activity

              People

              • Assignee:
                alanb Alan Bateman
                Reporter:
                igerasim Ivan Gerasimov
              • Votes:
                0 Vote for this issue
                Watchers:
                3 Start watching this issue

                Dates

                • Created:
                  Updated: