1

I've written a Java SWT application for OS X, exported it as a .jar and bundled it into .app. Everything works just as expected except for a critical part within my app; it requires to open other apps.

I've tried Runtime.exec(); as well as ProcessBuilder to do this, calling the open command on the other apps that I require to launch. This works perfectly fine from Eclipse or if I launch my exported .jar through Terminal. It does, however, not work when started by double-clicking the .app.

I'm wondering if this is an error on my part, or a restriction by either Java or OS X. Because Mac apps are sandboxed (from what I understand), could this be why calling open from within an app does nothing?

I've used two approaches to opening the app I need. It's a Steam game, but also comes as retail, so it can be launched either through the app itself or using the steam:// protocol. In my code I have tried the following:

ProcessBuilder pb = new ProcessBuilder("open", "steam://rungameid/57300//");
pb.start();

ProcessBuilder pb = new ProcessBuilder("open", appDir);
pb.start();

appDir is a string that contains a path to the .app bundle I wish to start.

Any ideas why this fails from a Mac application bundle, but runs fine from Eclipse/Jar? Running the app with console (through the bash script in the bundle) does not return any errors, so it seems to me that Java runs the command fine, but that OS X perhaps ignores it?

I've also tried searching on Google for this, but it seems perhaps highly uncommon or just very specific and I wasn't able to find anything.

Magnus Bull
  • 1,027
  • 1
  • 10
  • 21
  • did you get any message in Console? – Black Apr 13 '16 at 23:22
  • 1
    have you tried using `Runtime.exec()` as detailed in the answer here: http://stackoverflow.com/questions/19456454/how-do-i-launch-another-app-from-java-on-a-mac – Black Apr 13 '16 at 23:25
  • Exactly what are you specifying for 'appDir'? – greg-449 Apr 14 '16 at 06:16
  • @Francis I originally used `Runtime.exec()` with only a single string for the command. It had the same effect, but I moved over to `ProcessBuilder` to see if it acted any differently. I have not tried `Runtime.exec()` with a string array yet (but I can try in some hours). – Magnus Bull Apr 14 '16 at 06:29
  • @greg-449 `appDir` is a dynamic string that contains the absolute path to the .app bundle. For example `/Applications/TextEdit.app` – Magnus Bull Apr 14 '16 at 06:31
  • Have now done some testing and I've updated the post with the info. No real progress I'm afraid. – Magnus Bull Apr 14 '16 at 22:25
  • `ProcessBuilder("open", "/Applications/TextEdit.app")` works fine for me. You might want to specify `/usr/bin/open` to make sure it is not finding some other 'open' – greg-449 Apr 16 '16 at 15:47
  • @greg-449 but does it work fine for you from an exported jar launched through an app package? That's the only time it won't work for me. I tried `/usr/bin/open` but unfortunately it made no difference. I'll try to put up a dummy app. – Magnus Bull Apr 17 '16 at 15:54
  • I run it in my RCP which runs as a Mac app – greg-449 Apr 17 '16 at 16:16
  • Upon creating a dummy app, I found out what seems to be causing it; the **JVM bundle**. I created a very basic app that only runs to open TextEdit. It runs fine, but then I tried adding `JVMRuntime` in `Info.plist` and after copying the bundle into PlugIns within the .app, it no longer opened TextEdit. Does your app have the JVM bundled? Perhaps I should set up another more specific question now that I've narrowed it down to this. – Magnus Bull Apr 17 '16 at 21:02

2 Answers2

1

I'm answering this myself because the problem was my own setup. Specifically it was the bundled runtime I was using. I wrote a more in-depth answer in my other question here:

Java on OS X: “open” command won't run if .app package contains a JVM bundle

Community
  • 1
  • 1
Magnus Bull
  • 1,027
  • 1
  • 10
  • 21
0

If steam: is a registered URL type, you can use org.eclipse.swt.program.Program#launch(String). For example:

org.eclipse.swt.program.Program.launch("steam://rungameid/57300//");

Brian de Alwis
  • 2,814
  • 2
  • 18
  • 32
  • Thank you for this. It solves half the problems. I was able to run this from the app package fine. As for the other half, my bad for not specifying, but I need both methods to work (both steam and normal app launch), but this is progress! – Magnus Bull Apr 15 '16 at 16:41
  • I just tested this for launching apps as well and was able to launch both TextEdit and the app that I want. So basically this does not seem to be _ignored_ like how it is with `open`. Do you know if it's possible to specify arguments this way? I left that out of the question because it seemed unrelated, but I wasn't able to use args the same using this. Thanks. – Magnus Bull Apr 15 '16 at 16:51