3

I need to execute an external program from within my java application. I'm programming on a mac and have a .app application I'd like to run when the user selects it. It runs successfully on windows using:

String cmd = "path_to_executable\program.exe\"";

Process p = Runtime.getRuntime().exec(cmd); ...

But this does not work with an .app file. I've opened the contents of the .app file and have found a Unix executable file along with a other supporting files for the appliation. Is the unix executable file equivalent to an .exe file ?

animuson
  • 53,861
  • 28
  • 137
  • 147
mdl11
  • 57
  • 1
  • 5

3 Answers3

7

Launching an Mac application from Java on OS X is just a matter of invoking the built-in open command with the Application as a parameter. The open command line tool in OS X knows how to correctly open many different file types, including application bundles. If you were trying to launch the TextEdit.app application, you would invoke:

open /Applications/TextEdit.app

In Java, you would use:

String cmd = "open /Application/TextEdit.app";
Process p = Runtime.getRuntime().exec(cmd);
John Haager
  • 2,107
  • 1
  • 17
  • 22
  • A much cleaner solution, +1. – Vineet Kosaraju Nov 14 '12 at 00:07
  • The executable is located with in the .app folder 2 folders down. i.e. Open .app folder I find folders: contents/MacOS/linux executable. Does the command line tool you mentioned know to look down that many folders to find the linux executable or should I be accounting for those extra file layers in the path? Nothing opens with the change you suggested. No error caught either. – mdl11 Nov 14 '12 at 17:03
  • The "open" command passes the application bundle info off to Finder. Finder then launches the application as if you had double clicked on it. It is Finder that knows how application bundles are arranged and knows how to launch them. – John Haager Nov 15 '12 at 00:02
1

Yes, the executable that you should be running is the unix executable located in the .app bundle. It might not work properly though, you might need to cd into the directory or something.

Most application binaries are located in Contents/MacOS/App Name. You could make a method which would take in a string name and execute that binary based on the standard directory.

Vineet Kosaraju
  • 5,572
  • 2
  • 19
  • 21
0

A .app file is really just a folder. It is a way to self contain applications on a Mac. The actual binary file is, most likely, the Unix executable you found. It would help to know some details about the app.

The binary files for a .app are located in Application.app/Contents/MacOS

spex
  • 1,110
  • 10
  • 21
  • The app is called Mr. Tides. Our java application needs to run this outside application when the user selects it. I'm a little confused by the path name. I have the correct path to the Unix executable but I get an error that says "No Such File or Directory". The name of the executable I found in the .app folder is called Mr. Tides, with a space between Mr. and Tides. Would I use this in the path? and would I have to use a .bin extension? – mdl11 Nov 13 '12 at 23:23
  • When there are spaces in a filename, you may need to escape them. For example, if the app file is in the root of your hard drive, then the path to that executable is `/Mr.\ Tides.app/Contents/MacOS/Mr.\ Tides` – spex Nov 13 '12 at 23:27
  • Should the Mr. Tides executable have an extension at the end of the file in the path name i.e. something like a .exe? For example /Mr.\ Tides.app/Contents/MacOS/Mr.\ Tides.SOMETHING – mdl11 Nov 13 '12 at 23:38
  • mld11: No, the raw executables on OS X have no extension, just like on Linux. – John Haager Nov 16 '12 at 20:02