ant-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Russell Gold <>
Subject Re: Working Directory Woes ... or Not ?
Date Wed, 05 Jul 2000 21:22:50 GMT
At 5:00 PM -0400 7/5/00, Louis Tribble wrote:
>Russell Gold wrote:
>> At 10:33 PM -0400 7/4/00, Peter Donald wrote:
>> >Hi,
>> >
>> >Can someone point out where my stupidity lies here :P. I was under the
>> >impression that working directory was impossible to set in java. However
>> >recently I have been playing with
>> >System.setProperty("user.dir","someotherdirectory") and everything seems to
>> >work !!! hmmm The only problems occured when I used relative paths. So what
>> >I did at the begining of my program was resolve any relative paths to
>> >absolute paths. This means you have to resolve some system properties (ie
>> >"java.class.path") and also some internal paths inside the program.
>Some comments: 
>1. Doing this with user.dir is effectively using system properties
>as a global constant pool (three whacks with a ruler).

But isn't that what system properties are???

>2. A lot of code, including JDK code, puts the value of the property in a 
>static initializer, so you which value you get may depend on which class 
>loads first.
>3. Given 1 and 2, if current directory redirection is really required, 
>why  not create a CurrentDirectory singleton to make the magic explicit, 
>documented, and predictable?

Because the current directory is used by the io package, which won't know about your CurrentDirectory
class.  As a result, certain programs need to run in specific directories to work properly.

>4. exec is unaffected (this is where the current directory has usually
>mattered to me). I understand JDK 1.3 adds a way to set the current
>directory when exec-ing.
>5. Never having seen JDK source, I also can't rule out some bits of
>native library code (either JDK or extensions) getting the value from a 
>source unaffected by setting the user.dir property.

I think it is likely that changing the property on the fly is not guaranteed to be safe; however,
it should always be safe to establish it on the java command line:

    java -Duser.dir="/my/proper/directory" ProgramWhichCares

as this setting will happen before any code copies it. The chief benefit of this would be
to eliminate the need for the platform-specific batch files when starting a forked java task.
 This should work in any version of the JDK.

Russell Gold                     | "... society is tradition and order    (preferred)  | and reverence, not a series of cheap              | bargains between selfish interests."       |   - Poul Anderson, "Iron"

View raw message