2013-10-18 56 views
0

我在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 

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

回答

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

UPDATE:与伊恩商量后..

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

有趣的是,在这种情况下open有什么作用? –

+0

@IanLeslie - 'open'是执行应用程序的Mac命令。尝试'打开/应用程序/ CM战斗诺曼底/ CM争夺诺曼底.app --args 2vs2'白色庄园072.ema'从'终端'确认。 – vidit

+0

此外,我编辑了我的答案,在其中添加'--args',这是将参数传递给应用程序所必需的。 – vidit

0

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

您可以通过您的终端或Finder找到它。

所以我quess是改变:

"/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类型的系统。

+0

确定了一些进展。然而,该应用程序没有正确启动 - 它尝试但失败 - 我认为它找不到它所依赖的一些库。我不确定,因为我没有从这个日志中得到日志。在Windows上,如果我尝试使用不正确的“目录开始”启动游戏,我看到类似的结果。我会试着告诉它从.app目录开始,看看我能得到什么。正如你可能会告诉我没有Mac,所以我通过一个有用的用户来做这件事。但这是非常缓慢的。 –

+0

我不认为你可以做到这一点。你需要使用osx的“open”命令 - 它知道如何正确查看.app /文件夹。我不认为osx希望任何人在里面寻找并寻找二进制文件来运行... – GreenAsJade

0

是在命令行中运行这个东西?我看到一个命令变量,但是我没有看到任何关于它究竟是什么的情况。你可能需要为任何命令添加一个'sudo'前缀(假设它在终端中)。我只是从权限错误推断出来的。

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

+0

不知道如何回答“是从命令行运行”。我没有命令行,但我试图启动另一个应用程序,在Windows上,这些参数将与命令行窗口中使用的相同。 –

+0

getExeFile是我为最初写在Windows上的私人类编写的一种方法。我从来没有想到,在Mac上,可执行文件(缺少更好的单词)将是目录而不是文件。所以这个名字只是一个工件,实际返回的File对象是windows上的xxx.exe和mac上的xxx.app。 –

+0

@IanLeslie“可执行的东西”是一个二进制文件,并且有Mac上的应用程序的二进制文件,因为我在回答中发布了 –

相关问题