如何在Mac上从Java启动另一个应用程序 [英] How do I launch another app from Java on a Mac

查看:353
本文介绍了如何在Mac上从Java启动另一个应用程序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我编写的在Windows上可以正常运行的代码在Mac上无法正常工作. 我正在呼叫的简短格式:

The code I wrote that works fine on Windows is not working on the Mac. The short form I am calling:

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文件建立文件关联的设置(如果您好奇的话,这是一个游戏)

There is no file association setup with .ema files by the software I am trying to run (it's a game if you are curious)

我的代码如下:

    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();
    }
}

这会在日志中生成以下内容:

This produces the following in the log:

!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

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

Any help or guidance would be appreciated.

推荐答案

尝试一下.

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

这篇关于如何在Mac上从Java启动另一个应用程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆