Search code examples
javajava-web-startjnlp

Change working dir in Java Webstart


Is there a way to change working dir for JVM when running Java Webstart? When i use system.setProperties("user.dir", newDir) it sets it(system.getProperties() shows it does) but ignores it.

Is this a limitation in Java Webstart to always use the working dir where i started the jnlp file? I am using all permissions in the jnlp file.

Please help!

EDIT: Whatever i do now, my webstart always uses user.dir to save files. Frustrating!


Solution

  • I've had this question in the past myself, but I've always found that, in the end, I didn't need it. Why do I say this?

    Because your java web start app is not like an executable run from Program Files. It doesn't even exist on your computer like most programs (it is broken up into a bunch of different files and reassembled by the JVM). Therefore, you cannot say that the program has its own directory to do what it needs.

    But it doesn't need to. Here's why:

    1. Java has the Preferences API to help when you need to store data. The under-workings of the Preferences API is as mysterious as JWS, thus they are really a perfect fit. Either way, if you need to write things to a file, you should check this API to see if it can meet your needs.
    2. If you need to write files for the user, then prompting them and allowing them to choose the location obviously means you won't use your current working directory to read/write files.
    3. If you need to serialize objects, you should just create a program directory using the user.home resource as @AndrewThompson suggested. This will be "your" directory and is as good (in fact, better) than a directory in Program Files (if you're running on Windows, as an example).

    In conclusion, in all cases (that I've come across), there's no need to change your current working directory. If you need your own folder, create one in user.home (because you won't run into file permissions issues there).