简体   繁体   中英

How to bundle a JAR file into a Mac Application Bundle

I have looked around but I cannot find anything that will help or work. I know there is Launch4j which will do what I want with an .exe but not a .app file. I have the runnable JAR but do not know how to bundle it into a .app. I have tried multiple times (with no success) to export it as a .app from eclipse but it will not open after it is exported; no error message it just will not open. So my question is how can I bundle this into an Application Bundle?

If you have Java 6 or lower and an older System, then you have a Program made by Apple (I think) in the Developer directory. This does exactly what you want, otherwise, you could create a "hack" Application bundle (because it's the "I don't want to search through the internet" method).

  1. start Script Editor
  2. klick on "Save As" and under "File" select "Application".
  3. right klick on the Bundle and select "Show Package Contents"
  4. navigate to "Contents/MacOS" and delete the file "applet"
  5. create a new file with the same name and paste the following code into it:

     #!/bin/bash SOURCE="${BASH_SOURCE[0]}" while [ -h "$SOURCE" ]; do DIR="$( cd -P "$( dirname "$SOURCE" )" && pwd )" SOURCE="$(readlink "$SOURCE")" [[ $SOURCE != /* ]] && SOURCE="$DIR/$SOURCE" done DIR="$( cd -P "$( dirname "$SOURCE" )" && pwd )" cd "$DIR" cd .. echo "$(pwd)" for file in * do if [ -f "$file" ] then if [[ "$file" =~ .*.jar ]] then java -jar "$file" fi fi done 
  6. save the file

  7. now open terminal (Spotlight -> Terminal) and type the following chmod 755 note the space behind the 755. Then drag the file you just created into the Terminal window at the end of the text you just typed and hit enter.
  8. the last thing to do is to copy the jar file into the "Contents" folder.

Note: You are creating a Default AppleScript Applet and then switch the executable with your own in order to be able to launch the jar file. To give it execute permission you need to do the chmod 755 .

This is how it should look: --

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM