JDK-4740945 : File's methods give inconsistent results if "user.dir" changed
  • Type: Bug
  • Component: core-libs
  • Sub-Component: java.io
  • Affected Version: 1.4.0
  • Priority: P3
  • Status: Closed
  • Resolution: Duplicate
  • OS: linux
  • CPU: x86
  • Submitted: 2002-09-03
  • Updated: 2002-09-04
  • Resolved: 2002-09-04
Related Reports
Duplicate :  
Description

Name: nt126004			Date: 09/03/2002


FULL PRODUCT VERSION :
java version "1.4.0_01"
Java(TM) 2 Runtime Environment, Standard Edition (build 1.4.0_01-b03)
Java HotSpot(TM) Client VM (build 1.4.0_01-b03, mixed mode)


FULL OPERATING SYSTEM VERSION :
(Note: same behavior on several different OSs)
Linux -- Mandrake 8.1
glibc-2.2.4-6mdk
Linux bobalex.adc.rsv.ricoh.com 2.4.8-26mdk #1 Sun Sep 23
17:06:39 CEST 2001 i686 unknown

ADDITIONAL OPERATING SYSTEMS :
Windows 2000, Windows ME



A DESCRIPTION OF THE PROBLEM :
Various methods of class File behave inconsistently if the
system property "user.dir" is changed.

For example, getAbsoluteFile and getAbsolutePath reflect the
new specified "user.dir" value, but list and listFiles
produce the sames files as if the "user.dir" still had its
original value.

It is not totally clear from the documentation what the
behavior *should* be if "user.dir" is changed, but the
inconsistencies caused are definitely not a good thing.

In this bug submitter's opinion, a modified "user.dir"
should reflect itself consistently in all File methods.

STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
1. Run the enclosed sample program and observe results.
Instructions are in the source code.

EXPECTED VERSUS ACTUAL BEHAVIOR :
All results should reflect the directory specified in the
sample program's argument, but they don't. See comments in
the program's source code.

REPRODUCIBILITY :
This bug can be reproduced always.

---------- BEGIN SOURCE ----------
//
// Demonstrates an inconsistent behavior in File's method listFiles()
// when property "user.dir" is other than the platform's current
// directory. The files listed are in the platform's current directory,
// although the absolute path reported is relative to "user.dir".
//
// To demonstrate, first run this program with an argument of ".",
// then with some directory other than the current directory.
//

import java.io.*;
import java.util.*;

public class ListFilesBug {
    public static void main(String[] args) {
        if (args.length != 1) {
            System.err.println("Usage: java ListFilesBug <directory name>");
            System.exit(2);
        }
        String dir = args[0];
        File current = new File(".");
        System.setProperty("user.dir", dir);
        System.out.println("user.dir: " + System.getProperty("user.dir"));
        System.out.println("Relative: " + current);
        System.out.println("Absolute: " + current.getAbsolutePath());
        System.out.println();
        System.out.println("Contained files: " +
                Arrays.asList(current.listFiles()));
        System.out.println();
        System.out.println("Contained names: " +
                Arrays.asList(current.list()));
    }
}
---------- END SOURCE ----------
(Review ID: 160371) 
======================================================================