views:

298

answers:

2

Hello,

In Linux, the JVM sets the working direcory equal to the homefolder from the user. In Windows is this the folder where the jar is located. How can I find where the jar (from my app) is located to change the working dir?

Martijn

A: 

In both cases it is the actual directory. Under windows automatically the actual directory is set to the base-dir of the jar, if you execute it from explorer. Under linux the home-directory is the actual directory for your GUI. To set the directory write a short shell-script, which sets the actual directory (with the cd-command) and then executes the jar (with javaw -jar xyz.jar).

Mnementh
+1  A: 

Try obtaining the path to the jar with this code:

String path = YourClass.class.getProtectionDomain().getCodeSource().getLocation().toURI().getPath();

Then you can remove the "*.jar" part with:

path=path.substring(0, path.lastIndexOf('/')+1);

I had the same problem with a java game when I needed to open a file in the same directory the jar was.

Double clicking the jar in linux didn't open the file. Of course you can open a command line and change dir to the jar dir and then run the jar from there but I needed to double click the jar.

Eduramiba