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

Linux Mint: creating relative Paths using Path objects creates a Path prefixed with working directory

XMLWordPrintable

    • generic
    • generic

      FULL PRODUCT VERSION :
      java version "1.8.0_74"
      Java(TM) SE Runtime Environment (build 1.8.0_74-b02)
      Java HotSpot(TM) 64-Bit Server VM (build 25.74-b02, mixed mode)

      ADDITIONAL OS VERSION INFORMATION :
      Linux Xavier 3.19.0-32-generic #37~14.04.1-Ubuntu SMP Thu Oct 22 09:41:40 UTC 2015 x86_64 x86_64 x86_64 GNU/Linux


      A DESCRIPTION OF THE PROBLEM :
      On a Linux system, one cannot create relative Path objects.

      There are 3 ways to do so:
      - Path#relativize(Path otherPath)
      - Path#subPath(int startIndex, int endIndex)
      - Path#getName(int nameIndex)

      Attempting to use either of these three ways to create a relative path will return a completely unexpected path. Instead of returning a relative path, a path to the present working directory plus the relative path is returned.

      For example, "a/b".relativize("a/b/c/d") should return "c/d". However, it returns "path/to/working/directory/a/b/c/d"

      STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
      1. Create an absolute path (e.g. "/home/user/directory")
      2. Create another absolute path based on the first (e.g. "/home/user/directory/file.txt")
      3. Create a relative path using either of the three methods: #relativize, #subPath, #getName
      4. Print `relativePath.toAbsolutePath().toString()`

      EXPECTED VERSUS ACTUAL BEHAVIOR :
      EXPECTED -
      The included source code should print:

      Printing [relative file]
      Relative path: absoluteFile.txt
      Absolute Path: /home/user/absoluteFile.txt
      Printing [subPath file]
      Relative path: absoluteFile.txt
      Absolute Path: /home/user/absoluteFile.txt
      Printing [namePath]
      Relative path: absoluteFile.txt
      Absolute Path: /home/user/absoluteFile.txt
      ACTUAL -
      The included source code actually prints:

      Printing [relative file]
      Relative path: absoluteFile.txt
      Absolute Path: /home/user/path/to/working/directory/absoluteFile.txt
      Printing [subPath file]
      Relative path: absoluteFile.txt
      Absolute Path: /home/user/path/to/working/directory/absoluteFile.txt
      Printing [namePath]
      Relative path: absoluteFile.txt
      Absolute Path: /home/user/path/to/working/directory/absoluteFile.txt

      REPRODUCIBILITY :
      This bug can be reproduced always.

      ---------- BEGIN SOURCE ----------
          public static void main(String args[]) {
              String userHome = System.getProperty("user.home");
              Path absoluteBase = Paths.get(userHome);
              Path absoluteFile = Paths.get(userHome, "absoluteFile.txt");

              Path relativeFile = absoluteBase.relativize(absoluteFile);
              Path subPathFile = absoluteFile.subpath(absoluteFile.getNameCount() - 1, absoluteFile.getNameCount());
              Path namePathFile = absoluteFile.getName(absoluteBase.getNameCount());

              printRelativeAndAbsoluteStrings(relativeFile, "relative file");
              printRelativeAndAbsoluteStrings(subPathFile, "subPath file");
              printRelativeAndAbsoluteStrings(namePathFile, "namePath file");
          }

          public static void printRelativeAndAbsoluteStrings(Path path, String name) {
              System.out.println("Printing " + name);
              System.out.println("\tRelative path: " + path.toString());
              System.out.println("\tAbsolute Path: " + path.toAbsolutePath().toString());
          }
      ---------- END SOURCE ----------

            Unassigned Unassigned
            webbuggrp Webbug Group
            Votes:
            0 Vote for this issue
            Watchers:
            3 Start watching this issue

              Created:
              Updated:
              Resolved: