开发者

How to run a Mac application From Java?

开发者 https://www.devze.com 2023-01-28 19:35 出处:网络
I tried the code below to run a stand-alone utility app I creat开发者_开发知识库ed from Apple script but, I get a No File or Directory Exists error.

I tried the code below to run a stand-alone utility app I creat开发者_开发知识库ed from Apple script but, I get a No File or Directory Exists error.

I put identical copies (for testing) in the project, dist, parent directories but, it didn't help.

So, my questions are: Is my call to run the app bad (perhaps because it's not a Windows exe)? How to run a mac app from java?

Thanks

private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
    // TODO add your handling code here:
    Runtime r=Runtime.getRuntime();
    Process p=null;
    String s="MyLineInInput.app";
    try {
        p = r.exec(s);
    } catch (IOException ex) {
        Logger.getLogger(AudioSwitcherView.class.getName()).log(Level.SEVERE, null, ex);
    }


}


A Mac App Bunde is not an executable file, it's a folder with a special structure. It can be opened using the open command, passing the App Bundle path as an argument: open MyLineInInput.app.

EDIT: Even better would be using Desktop.getDesktop().open(new File("MyLineInInput.app"));


I used the Runtime.getRuntime().exec() method with the open command mentioned in the selected answer. I didn't use Desktop.getDesktop().open() since it unwantedly opened a terminal in my case and I didn't want to create an extra File object.

Process process = Runtime.getRuntime().exec("open /System/Applications/Books.app");

Reason for adding '/System':

It seems we need to use the /System prefix for System apps. For user-installed apps, that's not required, and it can be like /Applications/Appium.app.


To answer @Pantelis Sopasakis' issue that I also faced initially -

I get the error message: java.lang.IllegalArgumentException: The file: >/Applications/Microsoft Office 2011/Microsoft\ Excel.app doesn't exist.

In this case, it could be simply due to not escaping the space characters in the path.

Environment: JDK 11 Zulu - macOS Monterey 12.2.1 - M1 Silicon

0

精彩评论

暂无评论...
验证码 换一张
取 消