Results 1 to 10 of 10

Thread: Run jar file

  1. #1

    Default 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:
    Code:
    class TestFrame {
            public static void main(String[] args){
                    JFrame jFrame = new JFrame();
                    JTextField jTextField = new JTextField();
                    jTextField.setText(System.getProperty("user.dir"));
                    jFrame.add(jTextField);
                    jFrame.setSize(400, 200);
                    jFrame.setVisible(true);
            }
    }
    (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:
    Code:
    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

  2. #2
    Join Date
    Sep 2008
    Location
    Dubai
    Posts
    1,770

    Default Re: Run jar file

    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.)
    openSUSE 12.3 (x86_64) with Kernel 3.7.10-1.16-desktop and KDE 4.11.2 on MacBook Pro
    Latest MS Windows version used: Win95

  3. #3

    Default Re: Run jar file

    Thanks syampillai,
    I tried to run this file from another directory:
    Code:
    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.

  4. #4
    Join Date
    Sep 2008
    Location
    Dubai
    Posts
    1,770

    Default Re: Run jar file

    Quote Originally Posted by banhbaochay3 View Post
    ... 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.
    openSUSE 12.3 (x86_64) with Kernel 3.7.10-1.16-desktop and KDE 4.11.2 on MacBook Pro
    Latest MS Windows version used: Win95

  5. #5
    Join Date
    Jun 2008
    Location
    Earth - Denmark
    Posts
    10,730

    Default Re: Run jar file

    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
    http://tinyurl.com/8b9s6

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

  6. #6

    Default Re: Run jar file

    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".

  7. #7

    Default Re: Run jar file

    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
    Code:
    #!/bin/bash
    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 (maybe whatever as %U, %c... which show directory located jar file)

  8. #8
    Join Date
    Sep 2008
    Location
    Dubai
    Posts
    1,770

    Default Re: Run 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:
    Code:
    java -Duser.dir=BlahBlah ...
    openSUSE 12.3 (x86_64) with Kernel 3.7.10-1.16-desktop and KDE 4.11.2 on MacBook Pro
    Latest MS Windows version used: Win95

  9. #9

    Default Re: Run jar file

    Oh, thanks syampillai,
    Your recommended help me to solve this problem without using script
    In command, I only type:
    Code:
    java -Duser.dir=%d -jar %u
    Thank you again .
    This topic should be closed

  10. #10
    Join Date
    Sep 2008
    Location
    Dubai
    Posts
    1,770

    Default Re: Run jar file

    You are welcome.
    openSUSE 12.3 (x86_64) with Kernel 3.7.10-1.16-desktop and KDE 4.11.2 on MacBook Pro
    Latest MS Windows version used: Win95

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •