Java - What's the difference of double-clicking and running from Terminal for opening a .jar file?

jar, java, terminal

Solution

Depending on the method used to invoke the program, the current working directory might change. When running via `java -jar` the working directory is set by the terminal, easily identifiable by the prompt. When double-clicking, the directory defaults to the user's home directory. A quick test for demonstration:

public class TestWorkingDirectory { 
    public static void main(String[] args) {
        javax.swing.JOptionPane.showMessageDialog(
                null, System.getProperty("user.dir"));
    }
}

Assuming the JAR is located in `/home/test/Applications`, it will show the this path when running from terminal. When double-clicking, it's just `/home/test`. Therefore the path could be identical using both methods (when archive is located in `~`), but not necessarily - a difficulty when trying to debug / reproduce abnormal behavior.

Problem

I am using a Java-based application (a .jar file) to access a website for online discussion. And recently I've experienced some weird difference between running the application by double-clicking (on both Mac and Ubuntu) and running `java -jar client.jar` from Terminal. When I open the client by double-clicking, it will not allow me to log in, while everything works fine if I run it from Terminal. I know the description might be too vague, but I just wonder whether there is any general difference between these two ways of running .jar file. Thanks! My Java environment is shown below: ``` $ java -version java version "1.7.0_17" Java(TM) SE Runtime Environment (build 1.7.0_17-b02) Java HotSpot(TM) Server VM (build 23.7-b01, mixed mode) ```

Original source