简体   繁体   English

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

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

The code I wrote that works fine on Windows is not working on the Mac. 我编写的在Windows上运行良好的代码在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/");

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) 我尝试运行的软件没有与.ema文件建立文件关联的功能(如果您好奇的话,这是一个游戏)

My code looks like this: 我的代码如下所示:

    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. 任何帮助或指导,将不胜感激。

Try this.. 尝试这个..

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

UPDATE: After discussing with Ian.. 更新:与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/")

You're trying to execute "CM Battle for Normandy.app" and that's a folder (yeah I know can be misleading with the .app extension). 您正在尝试执行“ CM Battle for Normandy.app”,这是一个文件夹(是的,我知道扩展名为.app可能会误导您)。 What you want to do is to find a binary in that folder and execute that instead. 您要做的是在该文件夹中找到一个二进制文件,然后执行该文件。 Usually in that folder there's a Contents folder and in it a MacOS folder and in that folder there should be an executable (probably "CM Battle for Normandy"). 通常在该文件夹中有一个Contents文件夹,在其中是一个MacOS文件夹,并且在该文件夹中应该有一个可执行文件(可能是“ CM Battle for Normandy”)。

You can find it with your terminal or Finder. 您可以在终端机或Finder中找到它。

So my quess is to change: 所以我的问题是要改变:

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

To

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

But it might be slightly different. 但这可能略有不同。 It does not have end in an extension like on Windows (ie .exe or smth) but that's because MacOS is a Unix type system. 它没有像Windows那样的扩展名(即.exe或smth)结尾,但这是因为MacOS是Unix类型的系统。

Is this running things from command line? 这是从命令行运行的东西吗? I see a command variable but I don't see any context regarding what it actually is/does. 我看到了一个命令变量,但没有看到有关其实际作用的上下文。 You may need to add a 'sudo' prefix to whatever command that is (assuming it's in terminal). 您可能需要在正在执行的任何命令中添加“ sudo”前缀(假设它在终端中)。 I'm just inferring that from the Permissions error. 我只是从“权限”错误中推断出这一点。

Also, I see a 'getExeFile.' 另外,我看到一个“ getExeFile”。 Maybe I'm misunderstanding this but why would you be looking for a .exe on MacOS? 也许我对此有误解,但是为什么要在MacOS上寻找.exe? Again, I could just be misunderstanding this code. 同样,我可能只是误解了这段代码。 Anyway, I hope this helps or at least helps you jog your brain. 无论如何,我希望这有助于或至少有助于您慢跑。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM