Running Java from an OSX “.app” bundle causes “Err

2019-02-11 08:54发布

问题:

This was tested on OSX Mavericks through virtual box, and on Yosemite on a macbook.

I have a simple executable jar named "HelloWorld.jar".

I am trying to create a .app bundle for this java application. (Obviously, my actual program is more complex, but I have whittled it down to the barest problems).


CASE 1 - SIMPLE BUNDLE WITHOUT JAVA - WORKS COMPLETELY

Step 1 - Test at Console - Works

At the console I type

echo "Hello World (no java)" > /Users/josh/Desktop/test-output.txt

I view test-output.txt and see the expected output.

Step 2 - Test with Script - Works

I make a simple bash script named test:

 #!/bin/bash
 echo "Hello World (no java)" > /Users/josh/Desktop/test-output.txt

I chmod +x test and then type ./test, I view test-output.txt and see the expected output.

Step 3 - Create Rudimentary App Bundle - Works

mkdir -p test.app/Contents/MacOS
cp test test.app/Contents/MacOS
open test.app

I view test-output.txt and see the expected output.


CASE 2 - SIMPLE BUNDLE WITH JAVA - DOES NOT WORK

Setup

File HelloWorld.java:

public class HelloWorld {
    public static void main ( String[] args ) {
        System.out.println ( "Hello World" );
    }
}

File myManifest

Main-Class: HelloWorld

Did the following at console:

javac HelloWorld.java
jar -cfm HelloWorld.jar myManifest HelloWorld.class

Step 1 - Test at Console - Works

At the console I type:

java -jar HelloWorld.jar > /Users/josh/Desktop/java-output.txt

I get the expected output: Hello World

Step 2 - Test with Script - Works

I make a simple bash script named "helloworld"

 #!/bin/bash
 java -jar HelloWorld.jar > /Users/josh/Desktop/java-output.txt 

I chmod +x helloworld and then type ./helloworld, I get the expected output: Hello World

Step 3 (With Java) - Does not Work

mkdir -p helloworld.app/Contents/MacOS
cp helloworld helloworld.app/Contents/MacOS
cp HelloWorld.jar helloworld.app/Contents/MacOS
open helloworld.app

I get the following error:

LSOpenURLsWithRole() failed with error -10810 for the file /Users/josh/Desktop/helloworld/helloworld.app

/user/Josh/desktop/java-output.txt appears, but has no text inside.


As you can see, it appears that something is happening where running java inside an .app bundle is giving me that -10810 error.

Note: I also tried a variation of the first example, where I had the bash script launch /Applications/TextEdit.app, and that worked successfully. It makes me suspect the problem is with java specifically.

Does anyone have any idea what's causing this problem and how I can fix it?

回答1:

I do not currently have an OS X machine handy to test this, but hints on the web from another question seem to imply that you need to set JAVA_HOME and possibly PATH in order to make java work inside the app bundle.

Specifically, at the top of your shell script, before attempting to run your program, put the following lines, with appropriate changes for your system.

export JAVA_HOME=/path/to/my/java/install
export PATH=$PATH:/path/to/directory/containing/java

More generally, to diagnose the root cause of the problem, change the existing line in your script to capture stderr and see if that gives you any useful output that might have otherwise been swallowed by the app's environment.

 java -jar HelloWorld.jar > /Users/josh/Desktop/java-output.txt  2>  /Users/josh/Desktop/java-error.txt

If you are able to capture the printed error, it may suffice to show you root cause.