Question

A lot of people have the problem that when they start from nautilus a JAR-application, that the CWD (Current Working Directory) is set to their home folder (and not the folder in which the JAR is in). This is a bug in Nautilus, not especially Linux.

If you know a trick to make it easier to start a Jar correct instead of opening a terminal and manually type there java -jar myJar.jar, please post it below.

Thanks


A short code example of the problem:

File f = new File("ASimpleTextFile.txt");
System.out.println(f.getAbsolutePath());

In Windows, this prints: [The Path To The Jar]/ASimpleTextFile.txt
But in Linux, it prints: /home/[yourName]/ASimpleTextFile.txt

This is a very bad Idea because of we often use relative path's to configuration files. This way, they will not be found, because of the file is pointing to the homefolder.

Was it helpful?

Solution 2

I wrote a tiny C++ application which acts a wrapper that first sets the working directory. The code of the application can be found here on pastebin. Compile this and move to /usr/bin/java-jar-launch.

Then, in Nautilus, browse to one of your JAR files, select and click right on it and then choose for "open with". Then select custom command and type java-jar-launch. Don't forget to check the check box to remember it for the Java JAR type. Press OK and close.

If you double click a JAR file in Nautilus, it will be opened with my application, which invokes java with the correct working directory.

OTHER TIPS

Basically there is no guarantee what so ever about the current working directory.

What you CAN do, however, is to ask the JVM about the URL where it found the byte code for a given class (which you know where is put) and use that to locate other resources "next" to it.

See Determine location of a java class loaded by Matlab

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top