使用 Java 代码打开下载的 JAR 文件 Mac

Open a downloaded JAR file Mac with Java code

我正在创建一个简单的下载程序,它可以使用 Web 浏览器打开文件进行下载。在 Mac,当您打开下载的可执行 JAR 文件时会出现一个对话框,提示您 can't open it because it is from an unidentified developer。 有没有办法使用 Java 代码在没有对话框的情况下打开 JAR 文件?这是我的代码:

            File newFile = new File(System.getProperty("user.home")+"/Library/AppTest/Application.jar");
            File file = new File(System.getProperty("user.home")+"/Downloads/AppTest.jar");
            try {
                Files.move(file.toPath(), newFile.toPath());
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            try {
                Desktop.getDesktop().open(file);
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }

代码的问题在于它在执行时只是 shows this dialog,并且不会让您打开文件。我想在不进入设置的情况下解决这个问题,因为其他用户可能有同样的问题。

要在 mac 上打开 JAR 文件,请使用 Runtime:

String command = "java -jar path/to/jar/file";
Process p = Runtime.getRuntime().exec(command);

以上代码来自this link.