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

File(File, String) produces unexpected absolute path if File is a drive letter (win)

    Details

    • Type: Bug
    • Status: Closed
    • Priority: P4
    • Resolution: Won't Fix
    • Affects Version/s: 1.4.2
    • Fix Version/s: None
    • Component/s: core-libs
    • Labels:
    • Subcomponent:
    • CPU:
      x86
    • OS:
      windows_xp

      Description

      Name: js151677 Date: 06/22/2004


      FULL PRODUCT VERSION :
      1.2.2, 1.3.1, 1.4.2

      ADDITIONAL OS VERSION INFORMATION :
      Windows XP (5.1), and probably all windows plateforms

      A DESCRIPTION OF THE PROBLEM :
      Setting a File to a relative path and building a new File relative to the first using File(parent,child) constructor is not consistent if the first File was built with a *drive* relative path.
      It seems that building a relative path with File(parent,child) constructor incorrectly inserts a name-separator caracter after the drive letter, resulting in an absolute path instead of a drive relative path


      STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
      If you run the following code from the c:\windows directory :
         File f1 = new File(".");
         File f2 = new File(f1, "system32");
         System.out.println(f1.getPath()+", "+f1.isAbsolute()+", "+f1.getAbsolutePath());
         System.out.println(f2.getPath()+", "+f2.isAbsolute()+", "+f2.getAbsolutePath());

      it correctly produces a relative path:
         ., false, C:\WINDOWS\.
         .\system32, false, C:\WINDOWS\.\system32

      but the following code:
         File f1 = new File("c:");
         File f2 = new File(f1, "system32");
         System.out.println(f1.getPath()+", "+f1.isAbsolute()+", "+f1.getAbsolutePath());
         System.out.println(f2.getPath()+", "+f2.isAbsolute()+", "+f2.getAbsolutePath());

      does not produce the expected result and builds an incorrect absolute path

      EXPECTED VERSUS ACTUAL BEHAVIOR :
      EXPECTED -
      the preceding code should produce:
         c:, false, c:\WINDOWS
         c:system32, false, c:\WINDOWS\system32

      ACTUAL -
      it incorrectly produces:
         c:, false, c:\WINDOWS
         c:\system32, true, c:\system32


      REPRODUCIBILITY :
      This bug can be reproduced always.

      ---------- BEGIN SOURCE ----------
      import java.io.*;
      public class test
      {
      public static void main(String[] args)
      {
      // This code works has expected
      File f1 = new File(".");
      File f2 = new File(f1, "system32");
      System.out.println(f1.getPath()+", "+f1.isAbsolute()+", "+f1.getAbsolutePath());
      System.out.println(f2.getPath()+", "+f2.isAbsolute()+", "+f2.getAbsolutePath());
      // This does not ! The resulting path is incorrect...
      f1 = new File("c:");
      f2 = new File(f1, "system32");
      System.out.println(f1.getPath()+", "+f1.isAbsolute()+", "+f1.getAbsolutePath());
      System.out.println(f2.getPath()+", "+f2.isAbsolute()+", "+f2.getAbsolutePath());
      // ...it should have been equivalent to this:
      f1 = new File("c:system32");
      System.out.println(f1.getPath()+", "+f1.isAbsolute()+", "+f1.getAbsolutePath());
      // A work around that resolves to the right path, but it is now absolute :(
      f1 = new File("c:");
      f2 = new File(f1.getAbsolutePath(), "system32");
      System.out.println(f1.getPath()+", "+f1.isAbsolute()+", "+f1.getAbsolutePath());
      System.out.println(f2.getPath()+", "+f2.isAbsolute()+", "+f2.getAbsolutePath());
      }
      }
      ---------- END SOURCE ----------

      CUSTOMER SUBMITTED WORKAROUND :
      A first solution is to always resolve the relative path to an absolute path before path concatenation:
         f1 = new File("c:");
         f2 = new File(f1.getAbsolutePath(), "system32"); // the path is absolute

      But if you want your path to stay relative, you need to write your own code to do the path concatenation with Strings.
         String path1=f1.getPath();
         if (path1.endsWith(":")) // by the way, there is no File static field for the drive-separator character, so this code may be not portable
             f2 = new File(path1+path2);
         else // use the usual File concatenation
             f2 = new File(f1, path2);
      (Incident Review ID: 280839)
      ======================================================================
      ###@###.### 10/15/04 01:13 GMT

        Attachments

          Activity

            People

            • Assignee:
              Unassigned
              Reporter:
              jssunw Jitender S (Inactive)
            • Votes:
              0 Vote for this issue
              Watchers:
              1 Start watching this issue

              Dates

              • Created:
                Updated:
                Resolved:
                Imported:
                Indexed: