双击vs java -jar MyJar.jar [英] Double-click vs java -jar MyJar.jar

查看:117
本文介绍了双击vs java -jar MyJar.jar的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个.jar文件,当我从命令提示符通过java -jar MyJar.jar运行它时,它运行正常。但是双击它不会。双击正确启动程序,但内部的东西不起作用。

I have a .jar file and when I run it from the command prompt via java -jar MyJar.jar, it works fine. However double-clicking on it doesn't. Double-clicking starts the program correctly, but something on the inside doesn't work.

为了试图找出我自己的错误:什么是双击runnable .jar与从命令行运行它的区别?

For the purposes of trying to figure out what is wrong on my own: what is the difference between double-clicking on a runnable .jar vs running it from the command line?

推荐答案

执行程序的位置是重要的,可以根据它的执行方式而改变。

Where the program is executed is important and can change depending on how it was executed.

您可以使用类似的东西来测试...

You can test this by using something like...

import java.awt.EventQueue;
import java.awt.HeadlessException;
import java.io.File;
import java.io.IOException;
import javax.swing.JOptionPane;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;

public class WhereAmI {

    public static void main(String[] args) {
        new WhereAmI();
    }

    public WhereAmI() {
        EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                try {
                    UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
                } catch (ClassNotFoundException ex) {
                } catch (InstantiationException ex) {
                } catch (IllegalAccessException ex) {
                } catch (UnsupportedLookAndFeelException ex) {
                }

                try {
                    String path = new File(".").getCanonicalPath();
                    JOptionPane.showMessageDialog(null, "I was started in " + path);
                } catch (IOException exp) {
                    exp.printStackTrace();
                }

            }
        });
    }

}

例如。编译时,Jar位于 / Volumes / Disk02 / DevWork / personal / java / projects / wip / StackOverflow / WhereAmI / dist

For example. When compiled, the Jar resides in /Volumes/Disk02/DevWork/personal/java/projects/wip/StackOverflow/WhereAmI/dist

如果我将目录更改为此位置并运行 java -jar WhereAmI.jar 则输出

If I change directories to this location and run java -jar WhereAmI.jar it outputs

如果我将目录更改为 / Volumes / Disk02 / DevWork / personal / java / projects / wip / StackOverflow / WhereAmI 并运行 java -jar dist / WhereAmI.jar 输出

If I change directories to /Volumes/Disk02/DevWork/personal/java/projects/wip/StackOverflow/WhereAmI and run java -jar dist/WhereAmI.jar it outputs

执行上下文已更改。当您双击Jar并且它取决于系统时,会发生同样的事情。如果它是一个捷径或实际的Jar也会很重要。

The execution context has changed. The same thing will happen when you double click the Jar and it is system dependent. It will also matter if it's a short cut or the actual Jar.

这意味着如果你依赖任何相关资源,你必须确保Jar被执行在相对于您的资源的正确位置。

This will mean that if you rely on any relative resources, you must make sure that the Jar is executed within the correct location, relative to your resources.

如何实现这取决于操作系统

How to achieve this is dependent on the OS

这篇关于双击vs java -jar MyJar.jar的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆