18

Is there anyway in Java to find out if the given path is absolute or not regardless of the platform the program is currently running. So, what I want is probably something like the following example:

On Linux:

new File("/home/").isAbsolute() // Should return true.
new File("C:/My Documents").isAbsolute() // Should *also* return true.

On Windows:

new File("C:/Documents").isAbsolute() // Should return true.
new File("/home/").isAbsolute() // Should *also* return true.

I can probably code something to get around with this, but I just wanted to find out if anyone knew a built-in class provided in Java to solve this problem. Or has anyone ever come this problem? And how did you solve it?

Thanks!

His
  • 5,891
  • 15
  • 61
  • 82
  • 1
    on linux, a path like "c:/my documents" is probably invalid, and it is certainly not absolute. In systems implementing the posix standard, all paths exist below a single 'root', and thus an absolute path starts at that root. Thus an absolute path always begins with "/". anything else is taken to be relative to the process's current working directory. On linux, the "C:/..." is taken to mean the directory inside the current working directory that is named "C:" which is most likely nonsense. – SingleNegationElimination Jun 22 '09 at 03:07
  • 1
    Your question makes little sense. Imagine a hypothetical operating system where all paths are absolute. Let's say there is java on this operating system. So, to meet your requirement, you need a isFileAbsolute() method that always returns true, regardless of the platform the program is currently running. Easy to implement, but meaningless. – Erich Kitzmueller Jun 22 '09 at 09:41
  • I have a situation where user configures a working directory on a remote computer. The remote computer could be running Unix or Windows (though the most common scenario is local machine is windows and remote machine is unix). The user-specified path has to be validated, and it is required to determine if a path is absolute. – Santosh Tiwari Jan 13 '12 at 18:06
  • 1
    see [http://stackoverflow.com/questions/7627049/how-to-check-whether-the-path-is-relative-or-absolute-in-java][1] file.isAbsolute() [1]: http://stackoverflow.com/questions/7627049/how-to-check-whether-the-path-is-relative-or-absolute-in-java – Vlad Patryshev Sep 07 '12 at 04:38

4 Answers4

10

Nope.

There are some underlying FileSystem classes (that's Java 7, but they exist prior to it as well) that expose isAbsolute(), but they're not public - so you shouldn't use them, and even if you did your code would be full of reflection junk - and only the "correct" OS ones are included in the JRE, so you'd have to code around them anyway.

Here are the Java 7 implementations of isAbsolute(...) to get you started. Note that File.getPrefixLength() is package-private.

Win32FileSystem:

public boolean isAbsolute(File f) 
{
        int pl = f.getPrefixLength();
        return (((pl == 2) && (f.getPath().charAt(0) == slash))
                || (pl == 3));
}

UnixFileSystem:

public boolean isAbsolute(File f) 
{
        return (f.getPrefixLength() != 0);
}
Kevin Montrose
  • 22,191
  • 9
  • 88
  • 137
  • 12
    Note that after six years, this answer is outdated. Nowadays, the File class contains an "[isAbsolute()](http://docs.oracle.com/javase/7/docs/api/java/io/File.html#isAbsolute)" method that does exactly what you'd expect: _"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 "\\\\"_ – Guus Aug 24 '15 at 10:15
  • 1
    No this answer is not outdated and even with a jre 11 the File#isAbsolute method is still os dependent. On Unix os new File("C:\\Temp").isAbsolute() will say false and say true on windows. – mlapeyre May 25 '21 at 15:23
7

In Java 7:

new File(path).isAbsolute()
Val Blant
  • 1,664
  • 2
  • 24
  • 34
  • This is OS dependent and is not what OP needs. https://docs.oracle.com/javase/7/docs/api/java/io/File.html#isAbsolute() – marcioggs Mar 14 '22 at 09:30
4

My crack at this using Apache FilenameUtil -

   public static boolean isAbsolute(final String path) {
      return FilenameUtils.getPrefixLength(path) != 0;
   }

Technically this is returning !relative. Which is fine for my purposes.

remery
  • 250
  • 1
  • 4
  • 8
-1

I ended up using this (in Java 6):

private static boolean testPath(String path) {
    int prefixLen = FilenameUtils.getPrefixLength(path);
    if (testPathWin(path, prefixLen) || testPathLinux(prefixLen))
        return true;
    else
        return false;
}

private static boolean testPathWin(String path, int prefixLen) {
    if (prefixLen == 3)
        return true;
    File f = new File(path);
    if ((prefixLen == 2) && (f.getPath().charAt(0) == '/'))
        return true;
    return false;
}

private static boolean testPathLinux(int prefixLen) {
    return (prefixLen != 0);
}
stuhpa
  • 306
  • 3
  • 13
  • You copied Java 7 implementation public boolean isAbsolute(File f) for windows but you are using apache commons FilenameUtils.getPrefixLength() method. Java 7 implementation and Apache implementation of getPrefixLength() are not the same. For UNC paths apache will return "\\foo\poo" -> "\\foo\" as prefix and length will be 6 – acheron55 Mar 14 '14 at 19:46
  • @Acheron55: I don't understand which "isAbsolute()" do you refer to? Where is it exactly in my source snippet? – stuhpa Mar 16 '14 at 22:55
  • check the source code of FilenameUtils.getPrefixLength(path). It may return any number of length for UNC paths. But you only check for length 2 or 3 in your testPathWin method. So it is wrong. It would be true if you could use java implementation of it. Because it only returns 0,1,2,3 values. Also this line: `if (testPathWin(path, prefixLen) || testPathLinux(prefixLen))` linux check will render your windows method useless since it has a much broader range. Let's say your length is 1 for a windows path, but your testPath method will still return true, which is false – acheron55 Mar 17 '14 at 16:06