我有一个打开硒浏览器的项目。结构看起来像这样:
myProyect
src
com.open
OpenFirefox.java
lib
geckodriver
geckodriver.exe
如果我为此创建一个jar文件,则仅当该jar与lib/geckodriver/geckodriver.exe
位于同一位置时,才能执行该jar。控制台中的double click
和java -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();
}
什么也没发生,我什至没有收到错误消息。
发生了什么事?
答案 0 :(得分:0)
您应该使用Runtime.exec()
Process process = Runtime.exec("java -jar " + filepath);
if (process.waitFor() == 0) {
System.out.println("process ran without errors");
}