簡體   English   中英

如何在Mac上從Java啟動另一個應用程序

[英]How do I launch another app from Java on a Mac

我編寫的在Windows上運行良好的代碼在Mac上無法運行。 我正在呼叫的簡短形式:

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

任何幫助或指導,將不勝感激。

嘗試這個..

Runtime.getRuntime().exec (String ["open /Applications/CM Battle for Normandy/CM Battle for Normandy.app" "--args" "2vs2 White Manor 072.ema"], null, "/Applications/CM Battle for Normandy/")

更新:與Ian討論之后。

Runtime.getRuntime().exec (String ["open" "/Applications/CM Battle for Normandy/CM Battle for Normandy.app" "--args" "2vs2 White Manor 072.ema"], null, "/Applications/CM Battle for Normandy/")

您正在嘗試執行“ CM Battle for Normandy.app”,這是一個文件夾(是的,我知道擴展名為.app可能會誤導您)。 您要做的是在該文件夾中找到一個二進制文件,然后執行該文件。 通常在該文件夾中有一個Contents文件夾,在其中是一個MacOS文件夾,並且在該文件夾中應該有一個可執行文件(可能是“ CM Battle for Normandy”)。

您可以在終端機或Finder中找到它。

所以我的問題是要改變:

"/Applications/CM Battle for Normandy/CM Battle for Normandy.app"

"/Applications/CM Battle for Normandy/CM Battle for Normandy.app/Contents/MacOS/CM Battle for Normandy"

但這可能略有不同。 它沒有像Windows那樣的擴展名(即.exe或smth)結尾,但這是因為MacOS是Unix類型的系統。

這是從命令行運行的東西嗎? 我看到了一個命令變量,但沒有看到有關其實際作用的上下文。 您可能需要在正在執行的任何命令中添加“ sudo”前綴(假設它在終端中)。 我只是從“權限”錯誤中推斷出這一點。

另外,我看到一個“ getExeFile”。 也許我對此有誤解,但是為什么要在MacOS上尋找.exe? 同樣,我可能只是誤解了這段代碼。 無論如何,我希望這有助於或至少有助於您慢跑。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM