views:

4209

answers:

7

How can I change the current working directory from within a Java program? Everything I've been able to find about the issue claims that you simply can't do it, but I can't believe that that's really the case.

I have a piece of code that opens a file using a hard-coded relative file path from the directory it's normally started in, and I just want to be able to use that code from within a different Java program without having to start it from within a particular directory. It seems like you should just be able to call System.setProperty( "user.dir", "/path/to/dir" ), but as far as I can figure out, calling that line just silently fails and does nothing.

I would understand if Java didn't allow you to do this, if it weren't for the fact that it allows you to get the current working directory, and even allows you to open files using relative file paths....

+3  A: 

If I understand correctly, a Java program starts with a copy of the current environment variables. Any changes via System.setProperty(String, String) are modifying the copy, not the original environment variables. Not that this provides a thorough reason as to why Sun chose this behavior, but perhaps it sheds a little light...

Adam Paynter
+3  A: 

The working directory is a operating system feature (set when the process starts). Why don't you just pass your own System property (-Dsomeprop=/my/path) and use that in your code as the parent of your File:

File f = new File ( System.getProperty("someprop"), myFilename)
rparree
+11  A: 

There is no way to do this in Java. You could instead use the new File(parent, path) constructor, so then only the parent part would need to change between programs; or you could set up a .bat file to run Java from a different directory.

Every reference I can find says basically the same thing, and the relevant bug was closed as "will not fix".

Michael Myers
mmyers, we meet again! Again, answers within 10 seconds. lol, this time I really am going on break...
Adam Paynter
It's YOU!! I'm going to be looking behind me all day now.
Michael Myers
Thanks. I had been able to find some other sources saying you couldn't do it, but I wasn't convinced until I saw that bug you linked to. Unfortunately it's not code I can easily modify, so I'll probably end up using something along the lines of the ".bat" solution you described.
Nick
A: 

The smarter/easier thing to do here is to just change your code so that instead of opening the file assuming that it exists in the current working directory (I assume you are doing something like new File("blah.txt"), just build the path to the file yourself.

Let the user pass in the base directory, read it from a config file, fall back to user.dir if the other properties can't be found, etc. But it's a whole lot easier to improve the logic in your program than it is to change how environment variables work.

matt b
+1  A: 

If you run your legacy program with ProcessBuilder, you will be able to specify its working directory.

PhiLho
A: 

The other possible answer to this question may depend on the reason you are opening the file. Is this a property file or a file that has some configuration related to your application?

If this is the case you may consider trying to load the file through the classpath loader, this way you can load any file Java has access to.

Nathan Feger
A: 

Try using the "cd" command through a process.

Martijn