JDK-8130462 : File.isAbsolute() does not cover all cases
  • Type: Bug
  • Component: core-libs
  • Sub-Component: java.io
  • Affected Version: 8u45
  • Priority: P4
  • Status: Resolved
  • Resolution: Not an Issue
  • OS: windows_7
  • CPU: x86_64
  • Submitted: 2015-07-03
  • Updated: 2017-11-02
  • Resolved: 2015-08-18
Related Reports
Relates :  
Description
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!");
	    }
    }

}


Comments
As per Alan's comment https://msdn.microsoft.com/en-us/library/aa365247(VS.85).aspx#fully_qualified_vs._relative_paths explicitly lists what an absolute path consists of. "/xxx.yyy" is not in that list.
18-08-2015

/foo.bar is a relative path on Windows, as is C:foo.bar.
06-07-2015

Checked this with JDK 8u45 in Windows and Linux (64-bit) The result differs in Windows and Linux as reported by the submitter. In Windows: > java test /foo.bar is not an absolute path! In Linux: > java test /foo.bar IS an absolute path! Note sure, if this is to treated as an enhancement or bug. Moving this up for further evaluation.
06-07-2015