Run jar file

Hi everybody,
I have a problem, I googled and tried some way but now I don’t find an answer. I hope you can help me!
I write a java file which has content follow:

class TestFrame {
        public static void main(String] args){
                JFrame jFrame = new JFrame();
                JTextField jTextField = new JTextField();
                jFrame.setSize(400, 200);

(This Frame only show current path)
Then, I build jar from this file (I attached it in my post).
If I run jar file by typing in terminal:

java -jar TestFrame.jar

the Frame show:
It’s done, no problem.
But if I run jar file by one click, the Frame show:
It’s my problem, it’s always display My Document, not current path

I tried this jar file on windows xp, it runs correctly.

Thanks for reading

PS: I use openSUSE 11.2, KDE 4.4, Dolphin for explorer file.

PS again: I see these icons of Frame are different, and I don’t know why, it’s not dependent on my code

When you execute a Java main class from a jar file, the jar file location has nothing to do with your “current working directory” (“user.dir”). Try running it from another directory in the terminal and you will see that it will display that particular directory. It uses Documents directory as the ‘current working directory’ for all desktop applications unless their are further settings from within the application. (I think this setting can be changed but I don’t remember where exactly it is.)

Thanks syampillai,
I tried to run this file from another directory:

cd $HOME
java -jar /mnt/STORE/Test/test.jar

And Frame show: /home/banhbaochay (it’s directory which I stand, not directory which jar file stands).
That’s strange, I don’t understand. In windows xp, I always use “user.dir” to determine directory which jar file stands.

As per the JVM specifications, this is not guaranteed. Theoretically, the class byte-codes may not come from a file system.

banhbaochay3 wrote:
> That’s strange, I don’t understand. In windows xp, I always use
> “user.dir” to determine directory which jar file stands.

to understand you first need to remember that Microsoft does NOT use
the original SUN Java (aka: “real java”) instead, like so many other
things they do, they adapt standards to comply with the way they want
them to work…and produce their own software…

and, remember that SUN was a company which produced a certain flavor
of *nix, which MS never embraced in any way…

so, don’t be surprised when things originated by a *nix company, and
running on a *nix-like operating system such as Linux doesn’t mimic
the later arriving and changed software produced by MS (so that it
is NOT compatible with the older)…

you might be ready to read this also: Linux is Not Windows at

When it comes to chocolate, resistance is futile.
CAVEAT: [posted via NNTP w/openSUSE 10.3]

I’ve checked sun, System properties “user.dir” in java always display current working directory when the properties were initialized, it’s not depend on OS.
Therefore, I suspect Frame display Documents directory when I click jar file because in fact, current directory working is Documents directory, it’s not the directory which jar file stands.
Is problem in set up open with to jar file? I set up open with command: “java -jar”.

Hi DenverD,
I don’t think so about Java on Windows and on Linux are different. I know Linux and Windows are not the same, I always agree it.
As I wrote in the previous post, System properties always behave exactly on Windows and Linux (because Java is cross-platform language).
I found some information from here, there is a paragraph which I relize it’s resonable:

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)

I create small script

cd `dirname $1` && java -jar $1

Then I run jar file with script and the Frame displays exactly as I want.

I have another idea without using script. Open Configure Desktop → Advanced → File Association, then choose application–>x-java-archive. In the right panel, choose add (add any command) then edit as my picture below:
In line Command, I type “java -jar %U”. And in workpath, it’s current working directory which I said.
Now my job is only show current working directory in there and it’s done. Is it right?
But I don’t know need to type what in there :frowning: (maybe whatever as %U, %c… which show directory located jar file)

Generally, it is not a good practice to depend on the working directory to take any action from within a Java program. We can easily see that someone who invokes the program can easily manipulate it:

java -Duser.dir=BlahBlah ...

Oh, thanks syampillai,
Your recommended help me to solve this problem without using script :slight_smile:
In command, I only type:

java -Duser.dir=%d -jar %u

Thank you again :).
This topic should be closed :slight_smile:

You are welcome.