Search code examples
javaantjavafx-2

How to compile an .app bundle for OSX


I need to create a .app file in order to use my javafx application on osx.

I already read this, but it doesn't help much...

The tutorial above has a topic "DMG Package" but when I compile my app with the fxbuild there is no .app folder/conainer anywhere(also no dmg). What am I doing wrong?


Solution

  • Update 2022

    Parts of this answer are obsolete.

    The javafxpackager tool has been removed from the jdk and replaced with the jpackage tool.

    If you wish to accomplish a similar task today, I encourage you to use the joackage tool either by:

    1. Executing jpackage directly from the command line or a shell script similarly to, but different from, this answer, OR

    2. Executing joackage from a maven or grade build tool plugin, for example:

    Sample Build Script

    Here is a script which creates and installs a .app bundle on OS X.

    #! /bin/bash
     
    ################
    # package.sh
    #
    # Packages java code as a native OS X application
    #
    # - Fetches source for a Java Swing HelloWorld application from the web
    # - Compiles the source
    # - Packages the source into a native Mac application
    # - Installs the native Mac application
    # - Runs the native Mac Application
    #
    # Requires:
    #   OS X 10.8+
    #   Java 8b77+ (http://jdk8.java.net/download.html) 
    #   super user rights on the execution account
    #
    # To Use:
    #   chmod +x package.sh
    #   ./package.sh
    # As the script executes sudo commands, enter your 
    # superuser password as needed.
    ################
     
     
    # select java version
    set JAVA_HOME=`/usr/libexec/java_home -v 1.8`
    $JAVA_HOME/bin/java -version
     
    # cleanup work directory and any existing application install
    if [ -d work ]; then
      rm -rf work
    fi
     
    if [ -d /Applications/HelloWorld.app ]; then
      sudo rm -rf /Applications/HelloWorld.app
    fi
     
    # create work directory and use it
    mkdir work
    cd work
     
    # fetch a simple HelloWorldSwing sample java source from the web
    mkdir start
    cd start
    curl -O http://docs.oracle.com/javase/tutorial/uiswing/examples/start/HelloWorldSwingProject/src/start/HelloWorldSwing.java
    cd ..
     
    # compile the HelloWorldSwing java source
    $JAVA_HOME/bin/javac start/HelloWorldSwing.java
     
    # create a jar file 
    $JAVA_HOME/bin/jar cvf HelloWorldSwing.jar start/*.class
     
    # make an executable jar file
    $JAVA_HOME/bin/javafxpackager -createjar -srcdir . -appclass start.HelloWorldSwing -srcfiles HelloWorldSwing.jar -noembedlauncher -outdir . -outfile HelloWorld.jar
     
    # package the jar and java runtime as a native application with installer
    $JAVA_HOME/bin/javafxpackager -deploy -srcdir . -srcfiles HelloWorld.jar -outdir . -outfile HelloWorld -appclass start.HelloWorldSwing -native -name HelloWorld
     
    # codesign the application using your apple developer ID (if you have one)
    # allows the app to be accepted by the Apple GateKeeper - https://developer.apple.com/resources/developer-id/
    # codesign -s "Developer ID Application" bundles/HelloWorld.app
     
    # mount the installer disk image
    hdiutil attach bundles/HelloWorld.dmg
     
    # install the HelloWorld application from the disk image
    sudo cp -R /Volumes/HelloWorld/HelloWorld.app /Applications
     
    # unmount the installer disk image
    umount `mount | grep HelloWorld | cut -d " " -f1`
     
    # leave the work directory
    cd ..
     
    # run our newly installed application
    open -a HelloWorld
    

    Answers to additional questions

    is there no way to create one on windows?

    If you want to create a Mac App, you need a Mac (at least for JavaFX 2.2 or 8 apps).

    WAIT- is this only Java 8?? "Requires: Java 8b77+"

    The script might also work with later versions of Java 7 (e.g. after build 6), but I have never tried it with those versions.

    It definitely won't work with early versions of Java 7 (e.g. before build 6) nor any version of Java previous to that.

    "Requires: OS X 10.8+"

    The script might also work on OS X 10.7, but I haven't tested with that version.

    It definitely won't work on 10.6 or earlier (and neither would the app produced).

    tried a java class called app-bundler.jar but that didnt work

    I've never tried app-bundler.jar and cannot comment on what it may or may not do. This solution does not relate to app-bundler.jar.