java.io.File bug in Linux?

I made a very simple runnable jar with the following code:

package stuff;

import java.io.File;
import javax.swing.JOptionPane;

public class Testing 
{
	public static void main(String[] args)
	{	
		File file = new File("");
		JOptionPane.showMessageDialog(null, file.getAbsoluteFile());
	}
}

Placing this jar in example C:/Test/Hello and running it will cause the program to print C:/Test/Hello, which is absolutely correct.
Doing the same, but in Linux, and the program will print /home/pj1 no matter where the jar is located. This is wrong! It should print the path to its location.
Is there a way to fix this?

On Linux, when you launch a jar file (or any file for that matter) from a file manager, it actually launches from “~” (the user’s home directory), so you are getting the right output.

So to get the jar’s folder, you have to use this workaround:


String path = (new File(SomeClass.class.getProtectionDomain().getCodeSource().getLocation().toURI()).getParent());

Well, that sucks. I have to rewrite a lot of strings now. But thanks! :slight_smile:

Is there a way to resolve this other than manually modifying every single string?
I actually did it, by creating a variable calldc HOME and altered every string to start with HOME +.
It wasnt a good solution. Matter fact, its a piss solution. External libraries still crashes, throwing FileNotFoundException.

I tried an other solution:

			String ourPath = (new File(Engine.class.getProtectionDomain().getCodeSource().getLocation().toURI()).getParent());
			System.setProperty("user.dir", ourPath);
			System.setProperty("user.home", ourPath);

Did not work.

I don’t know, but on my “linux” your code just works fine.

Well, instead of spending lots of time fixing this, I will just force Linux users to run the project from the terminal, which solves this(it behaves like windows).

Why? File has a constructor which takes another file and a relative path: you should use that rather than manipulating strings yourself.

It doesnt matter anyway. Launching the game from the shell script(terminal) solves this. Matter fact, launching the game from is a must because I have to change the Xms and Xmx.

But I have a new problem. My shell script wont run the game when I click on it.
First, I made shell scripts executable by following this tutorial:


Then, I created a shell script, named Run.sh:
java -jar “home/pj1/Desktop/crap/Game/data.jar”
I also tried inserting #!/bin/bash at the top, still did not work.

Whats wrong?

I haven’t read the thread as I need to run right now, but you may want to tell your users that they can run it using Alt-F2 if they need to run it via terminal.

Two things. Firstly, you’ve missed the leading / so that’s a relative path. Secondly, you’re still not guaranteeing that you get the working directory you want. Your shell script should look something like this:

#!/bin/sh
cd /home/pj1/Desktop/crap/Game
java -jar ./data.jar

or even better (assuming that the shell script will be in the same directory as the jar, and allowing them to be moved around provided they stay together):

#!/bin/sh
cd `dirname $0`
java -jar ./data.jar

doesnt new File(".") do exactly what you want ?
on windows it should be equivalent
IIRC

Making assumptions about where PWD is not a good idea. On all platforms its something that should not be assumed.

For example if i have a launch icon on the desktop, what is the pwd? in the windows start menu? What about simlinks?

That is why many applications will have a “installed in” directory. I use a launch script to ensure that all appropriate directories are set. In unix this means using things like readlink so you can use simlinks properly.