Skip to main content

Changing the current working directory in Java?



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....


Comments

  1. 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".

    ReplyDelete
  2. 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...

    ReplyDelete
  3. 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)

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

    ReplyDelete
  5. It 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

    You can see an example of its use here (Clojure code, sorry). Look at the function chdirToRoot

    ReplyDelete
  6. 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.

    ReplyDelete
  7. 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.

    ReplyDelete
  8. Try using the "cd" command through a process.

    ReplyDelete

Post a Comment

Popular posts from this blog

[韓日関係] 首相含む大幅な内閣改造の可能性…早ければ来月10日ごろ=韓国

div not scrolling properly with slimScroll plugin

I am using the slimScroll plugin for jQuery by Piotr Rochala Which is a great plugin for nice scrollbars on most browsers but I am stuck because I am using it for a chat box and whenever the user appends new text to the boxit does scroll using the .scrollTop() method however the plugin's scrollbar doesnt scroll with it and when the user wants to look though the chat history it will start scrolling from near the top. I have made a quick demo of my situation http://jsfiddle.net/DY9CT/2/ Does anyone know how to solve this problem?

Why does this javascript based printing cause Safari to refresh the page?

The page I am working on has a javascript function executed to print parts of the page. For some reason, printing in Safari, causes the window to somehow update. I say somehow, because it does not really refresh as in reload the page, but rather it starts the "rendering" of the page from start, i.e. scroll to top, flash animations start from 0, and so forth. The effect is reproduced by this fiddle: http://jsfiddle.net/fYmnB/ Clicking the print button and finishing or cancelling a print in Safari causes the screen to "go white" for a sec, which in my real website manifests itself as something "like" a reload. While running print button with, let's say, Firefox, just opens and closes the print dialogue without affecting the fiddle page in any way. Is there something with my way of calling the browsers print method that causes this, or how can it be explained - and preferably, avoided? P.S.: On my real site the same occurs with Chrome. In the ex