Skip to main content

current user directory question

8 replies [Last post]
navinkjha
Offline
Joined: 2004-12-28
Points: 0

If the user.dir system property is changed using set property it seems to take effect. But if a file created in current working directory, the file is still cerated in the current working directory that originally existed. Thought ?

Reply viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.
soupdragon
Offline
Joined: 2006-01-07
Points: 0

The whole "current" directory concept isn't really suited to a multi-threaded environment, as Java programs very often are.

And it's not like you can't produce the same effect with a File for the current directory for the most part. About the only time it might be significant is when using Runtime.exec().

In fact, how about that? An optional working directory parameter for Runtime.exec() would probably eliminate the only valid argument for being able to change current directory in the JVM.

alanb
Offline
Joined: 2005-08-08
Points: 0

http://java.sun.com/j2se/1.5.0/docs/api/java/lang/Runtime.html#exec(java.lang.String[],%20java.lang.String[],%20java.io.File) has been in the platform since 1.3.

soupdragon
Offline
Joined: 2006-01-07
Points: 0

Oops! Well, you learn something new every day.

leouser
Offline
Joined: 2005-12-12
Points: 0

when I needed a 'current directory' that worked in the JythonShell I wrote, I just set up the machinery in the application. It was internal and anything that needed the 'current directory' would reference that File instance.

leouser

alanb
Offline
Joined: 2005-08-08
Points: 0

The user.dir property is set at VM startup to be the working directory. You should not change this property or set it on the command-line. If you do, then you will see some inconsistent behaviour as there places in the implementation that assumes that the user.dir is the working directory and that it doesn't change during the lifetime of the VM. So out of curiousity, what problem are you looking at that requires you change user.dir? Are you looking to change the working directory (like chdir)?

navinkjha
Offline
Joined: 2004-12-28
Points: 0

Alan,

Someone came and asked me about this. What the person was looking for was changing the current working directory.

-Navin

alanb
Offline
Joined: 2005-08-08
Points: 0

I assume you know that changing the working directory of a multi-threaded application can be hairy. It would be interesting to know why this person needs this feature. Is it a case where they want to resolve relative paths against some directory other than the value of user.dir?

navinkjha
Offline
Joined: 2004-12-28
Points: 0

Alan,

He said he was dealing with some code that was reading/writing files in default working directory. On windows this is desktop, so all the files get written on the desktop. Which is why he wants to change the working directory. I told him to get hold of the source code and make required changes. Personally I think it is a bad idea to write to some default folder.

-Navin