United StatesChange Country, Oracle Worldwide Web Sites Communities I am a... I want to...
JDK-4483097 : File returned by getAbsoluteFile() may not refer to the original file

Details
Type:
Bug
Submit Date:
2001-07-23
Status:
Closed
Updated Date:
2001-07-23
Project Name:
JDK
Resolved Date:
2001-07-23
Component:
core-libs
OS:
windows_2000
Sub-Component:
java.io
CPU:
x86
Priority:
P2
Resolution:
Not an Issue
Affected Versions:
1.4.0
Fixed Versions:

Related Reports

Sub Tasks

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
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
                                     
189-09-07 0
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.)
======================================================================
                                     
2004-06-11



Hardware and Software, Engineered to Work Together