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....
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.
ReplyDeleteEvery reference I can find says basically the same thing, and the relevant bug was closed as "will not fix".
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...
ReplyDeleteThe working directory is a operating system feature (set when the process starts).
ReplyDeleteWhy 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)
If you run your legacy program with ProcessBuilder, you will be able to specify its working directory.
ReplyDeleteIt is possible to change the PWD, using JNA/JNI to make calls to libc. The JRuby guys have a handy java library for making POSIX calls called jna-posix Here's the maven info
ReplyDeleteYou can see an example of its use here (Clojure code, sorry). Look at the function chdirToRoot
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.
ReplyDeleteLet 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.
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?
ReplyDeleteIf 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.
Try using the "cd" command through a process.
ReplyDelete