我在Windows上編寫的代碼工作正常無法在Mac上運行。 縮寫形式,我呼籲:如何在Mac上從Java啓動另一個應用程序
Runtime.getRuntime().exec (String ["/Applications/CM Battle for Normandy/CM Battle for Normandy.app" "2vs2 White Manor 072.ema"], null, "/Applications/CM Battle for Normandy/");
沒有文件關聯設置與我嘗試運行的軟件.ema文件(這是一個遊戲,如果你是好奇)
我的代碼看起來像這樣的:
private void launchGameProgram (PBEMGame selectedGame) {
if (selectedGame == null)
return; // no work to do
InstalledProgram program = selectedGame.playedWith();
if (program == null)
return; // no work to do
try {
Vector<String> command = new Vector<String>();
command.add (program.getExeFile().getAbsolutePath());
if (selectedGame.getLastTurn() != null && selectedGame.getLastTurn().getTurnFile() != null) {
// Add the turn file name to the command
command.add (selectedGame.getLastTurn().getTurnFile().getName());
}
GUIApplicationPolicy.getLog().log ("WTII testing: About to launch: " + command.toString() + " from: " + program.getExeFile().getParentFile());
Runtime.getRuntime().exec (command.toArray (new String[command.size()]), null, program.getExeFile().getParentFile());
} catch (IOException exception) {
GUIApplicationPolicy.getLog().log (exception);
exception.printStackTrace();
}
}
這將產生日誌中的以下內容:
!Entry: 2013/10/02 23:08:33.017
!Message: WTII testing: About to launch: [/Applications/CM Battle for Normandy/CM Battle for Normandy.app, 2vs2 White Manor 072.ema] from: /Applications/CM Battle for Normandy
!Entry: 2013/10/02 23:08:33.023
!Exception: Cannot run program "/Applications/CM Battle for Normandy/CM Battle for Normandy.app" (in directory "/Applications/CM Battle for Normandy"): error=13, Permission denied
!Stack: java.io.IOException: Cannot run program "/Applications/CM Battle for Normandy/CM Battle for Normandy.app" (in directory "/Applications/CM Battle for Normandy"): error=13, Permission denied
at java.lang.ProcessBuilder.start(ProcessBuilder.java:1041)
at java.lang.Runtime.exec(Runtime.java:617)
at com.lesliesoftware.whoseturnisit.WhoseTurnIsIt.launchGameProgram(Unknown Source)
at com.lesliesoftware.whoseturnisit.WhoseTurnIsIt.access$3100(Unknown Source)
at com.lesliesoftware.whoseturnisit.WhoseTurnIsIt$LaunchSelectedGameProgram.widgetSelected(Unknown Source)
at org.eclipse.swt.widgets.TypedListener.handleEvent(TypedListener.java:234)
at org.eclipse.swt.widgets.EventTable.sendEvent(EventTable.java:84)
at org.eclipse.swt.widgets.Display.sendEvent(Display.java:3776)
at org.eclipse.swt.widgets.Widget.sendEvent(Widget.java:1367)
at org.eclipse.swt.widgets.Widget.sendEvent(Widget.java:1390)
at org.eclipse.swt.widgets.Widget.sendEvent(Widget.java:1375)
at org.eclipse.swt.widgets.Widget.notifyListeners(Widget.java:1187)
at org.eclipse.swt.widgets.Display.runDeferredEvents(Display.java:3622)
at org.eclipse.swt.widgets.Display.readAndDispatch(Display.java:3277)
at com.lesliesoftware.whoseturnisit.WhoseTurnIsIt.main(Unknown Source)
Caused by: java.io.IOException: error=13, Permission denied
at java.lang.UNIXProcess.forkAndExec(Native Method)
at java.lang.UNIXProcess.<init>(UNIXProcess.java:135)
at java.lang.ProcessImpl.start(ProcessImpl.java:130)
at java.lang.ProcessBuilder.start(ProcessBuilder.java:1022)
... 14 more
任何幫助或指導,將不勝感激。
有趣的是,在這種情況下open有什麼作用? –
@IanLeslie - 'open'是執行應用程序的Mac命令。嘗試'打開/應用程序/ CM戰鬥諾曼底/ CM爭奪諾曼底.app --args 2vs2'白色莊園072.ema'從'終端'確認。 – vidit
此外,我編輯了我的答案,在其中添加'--args',這是將參數傳遞給應用程序所必需的。 – vidit