问题
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.
回答1:
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//");
回答2:
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
来源:https://stackoverflow.com/questions/36610422/os-x-java-app-bundle-cannot-open-other-apps