2

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.

Martijn Courteaux
  • 67,591
  • 47
  • 198
  • 287
  • 2
    In what way does your Java program use the current directory? You can't even do `chdir` in Java, so, the idea is that your program should be cwd-agnostic. – C. K. Young Nov 11 '10 at 17:41
  • 1
    @Chris: Not at all true; I often use current directory to write data, requiring that the user set up the app to have current dir be where they want to write the data. – Lawrence Dol Nov 11 '10 at 17:52
  • 1
    @Software Monkey: that's a sensible approach when you app is a command-line app. But for a UI app, there should be no dependency on the current directory (except maybe as the default location of file-open dialogs or similar things). – Joachim Sauer Nov 11 '10 at 18:02

1 Answers1

5

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

Community
  • 1
  • 1
Thorbjørn Ravn Andersen
  • 73,784
  • 33
  • 194
  • 347