7

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)
4

1 回答 1

5

根据用于调用程序的方法,当前工作目录可能会改变。通过java -jar工作目录运行时由终端设置,通过提示容易识别。双击时,目录默认为用户的主目录。演示的快速测试:

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

假设 JAR 位于 中/home/test/Applications,从终端运行时它将显示此路径。双击时,它只是/home/test. 因此,使用这两种方法的路径可能相同(当存档位于 中时~),但不一定 - 尝试调试/重现异常行为时遇到困难。

于 2013-03-30T22:44:53.930 回答