如何在Mac上从Java启动另一个应用程序

时间:2013-10-18 18:17:37

标签: java macos swt exec

我写的代码在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

任何帮助或指导都将不胜感激。

3 个答案:

答案 0 :(得分:1)

试试这个..

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/")

更新:与伊恩讨论后。

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/")

答案 1 :(得分:0)

你正在尝试执行“CM Battle for Normandy.app”,这是一个文件夹(是的,我知道可能会误导.app扩展名)。你想要做的是在该文件夹中找到一个二进制文件并执行它。通常在该文件夹中有一个Contents文件夹,其中有一个MacOS文件夹,在该文件夹中应该有一个可执行文件(可能是“诺曼底的CM战斗”)。

您可以使用终端或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类型的系统。

答案 2 :(得分:0)

这是从命令行运行吗?我看到一个命令变量,但我没有看到任何关于它实际是什么/做什么的背景。您可能需要在任何命令中添加“sudo”前缀(假设它在终端中)。我只是从权限错误中推断出来。

另外,我看到'getExeFile'。也许我误解了这个,但你为什么要在MacOS上寻找.exe?再一次,我可能只是误解了这段代码。无论如何,我希望这有助于或至少帮助你慢跑你的大脑。