JDK-4483097 : File returned by getAbsoluteFile() may not refer to the original file
  • Type: Bug
  • Component: core-libs
  • Sub-Component: java.io
  • Affected Version: 1.4.0
  • Priority: P2
  • Status: Closed
  • Resolution: Not an Issue
  • OS: windows_2000
  • CPU: x86
  • Submitted: 2001-07-23
  • Updated: 2001-07-23
  • Resolved: 2001-07-23
Description

Name: bsC130419			Date: 07/23/2001


java version "1.4.0-beta"
Java(TM) 2 Runtime Environment, Standard Edition (build 1.4.0-beta-b65)
Java HotSpot(TM) Client VM (build 1.4.0-beta-b65, mixed mode)

When I manipulate System property "user.dir", File's getAbsoluteFile() may no
longer refer to the same file as original. Here is an example:
----------
import java.io.File;
import java.io.IOException;

public class FileAbsPathBug {
  public static void main( String[] args) throws IOException {
   // Create test file in c:/temp
    File test= new File( "c:/temp/test.txt");
    if( !test.exists()) {
      test.createNewFile();
    }
    // Set c:/temp as my base directory for resolving relative paths
    System.setProperty( "user.dir", "c:/temp/");
    // Make a relative path for the c:/temp/test.txt file we've just creared
    File f= new File( "test.txt");
    System.err.println(
      f.getAbsolutePath() +
      (f.exists() ? " exists" : " does not exist" )
    );
    System.err.println(
      f.getAbsolutePath() +
      (f.getAbsoluteFile().exists() ? " exists" : " does not exist" )
    );
  }
}
----------
Running this results in the following printout:
c:\temp\test.txt does not exist
c:\temp\test.txt exists

I suspect that this problem happens because File internally resolves existence
using FileSystem's getBooleanAttributes(this) & FileSystem.BA_EXISTS.
FileSystem.getBooleanAttribute is a native method, which is likely to not know
about "user.dir".

One fix to this would be always passing an absolute path to native methods of
FileSystem.
(Review ID: 128603) 
======================================================================

Comments
WORK AROUND Name: bsC130419 Date: 07/23/2001 An ugly work-around is using getAbsoluteFile() on all File objects that came from "unknown" sources, and may therefore contain a relative path (e.g. user- supplied strings, as opposed to JFileChooser.) ======================================================================
11-06-2004

EVALUATION Not a bug. If you need to resolve a filename against the value of the "user.dir" system property then you must use getAbsolutePath (or getAbsoluteFile, or getCanonicalPath, or getCanonicalFile). Simpler operations such as exists() always resolve against the directory in which the Java virtual machine was originally invoked (and no, there is no way to change that). -- mr@eng 2001/7/23
07-09-0189