无法使用Desktop.getDesktop()。open(new File(“ C:/path/firefox.jar”);)执行jar文件。当jar需要外部文件时

时间:2018-08-13 13:23:54

标签: java jar executable-jar

我有一个打开硒浏览器的项目。结构看起来像这样:

myProyect
   src
     com.open
        OpenFirefox.java
   lib
     geckodriver
        geckodriver.exe

如果我为此创建一个jar文件,则仅当该jar与lib/geckodriver/geckodriver.exe位于同一位置时,才能执行该jar。控制台中的double clickjava -jar firefox.jar都可以正常工作。

现在,我需要从另一个程序执行此jar。我一直在打开其他与Desktop.getDesktop().open(file);没有依赖关系的jar,但是没有问题,但是当我尝试

try {
    File file = new File("C:/Users/user/Desktop/firefox.jar");                  
    Desktop.getDesktop().open(file);
} catch (Exception e) {
    e.printStackTrace();
}

什么也没发生,我什至没有收到错误消息。

发生了什么事?

1 个答案:

答案 0 :(得分:0)

您应该使用Runtime.exec()

Process process = Runtime.exec("java -jar " + filepath);
if (process.waitFor() == 0) {
    System.out.println("process ran without errors");
}