Bundle your JavaFX App with a JRE - Mac Edition

613 views Asked by At

I ran into a substantial problem with the way Netbeans packages up Java programs into native Mac Apps (For those who aren't familiar, Netbeans can bundle your Jar file with a JRE so you have a completely self-contained App. Clients don't even have to have Java on their computers to run it. The whole process is explained well here).

The problem I found was that the apps Netbeans produces completely ignore JavaFX Preloaders. I think it's because of the slimmed down JRE that gets added in. I couldn't find any solutions online and wanted to post one here for anyone who runs into the same issue.

1

There are 1 answers

0
corpico On BEST ANSWER

Here's what you have to do:

  1. Package the app as you would normally, using the process linked above.
  2. Right-click the produced app and select Show Package Contents. Then enter the Contents folder.
  3. Enter the Plugins folder. This contains the JRE. Delete it.
  4. Replace the deleted JRE with the copy stored on your computer. It can be found in Your Hard Drive < Library < Java < Java Virtual Machines. Just copy the version you need if there are multiple folders in there.
  5. Go back to the Contents folder and enter the MacOS folder. This contains the shell that runs your Jar file. Open the executable file that's in there with a text editor and delete everything. Replace it with this:

#!/bin/sh

# Constants APP_JAR="YOURAPP.jar" APP_NAME="YOURAPP" VM_ARGS=""

# Set the working directory DIR=$(cd "$(dirname "$0")"; pwd)

# Set java location _java="../PlugIns/jdk1.8.0_05.jdk/Contents/Home/bin/java"

# Move to correct directory cd $DIR

# Run the application exec $_java $VM_ARGS -Dapple.laf.useScreenMenuBar=true -Dcom.apple.macos.use-file-dialog-packages=true -Xdock:name="$APP_NAME" -Xdock:icon="$DIR/../Resources/ICON.icns" -cp ".;$DIR;" -jar "$DIR/../Java/$APP_JAR"

In the above, substitute YOURAPP for the name of your jar file, jdk1.8.0_05 for whichever version of Java you added in step 4, and ICON for the name of your app's icon, if it has one.

  1. If you want to change the app's icon from the standard Java logo, go back to Contents and add your own .icns file to the Resources folder.

There you have it! The app should run as intended, preloader and all.