Search code examples
javalinuxjavafxdeb

JavaFX DEB Bundle Without JRE Doesn't Work


I'm using the Oracle “Self-Contained Application Packaging” tool to make a .deb file for a JavaFX 8 desktop application. The generated package file can be installed without problems on Ubuntu but then the application fails to run. The file is installed as follows:

$ sudo dpkg -i vocabhunter-1.0.14.deb

However, attempting to run the application generates the following error:

$ /opt/VocabHunter/VocabHunter
VocabHunter Failed to locate JNI_CreateJavaVM
VocabHunter Failed to launch JVM

Importantly, I'm generating a bundle without the JRE included and on investigation it seems that the problem relates to this. The generated file /opt/VocabHunter/app/VocabHunter.cfg contains the following line:

app.runtime=

If I edit this and add the path to Java, the program launches without problems. As a workaround, I've suggested that after installing the .deb bundle the user run the following command:

sudo sed -i "s|app.runtime=.*|app.runtime=$JAVA_HOME|g" /opt/VocabHunter/app/VocabHunter.cfg

However, this makes things hard for the user. Does anyone know how to fix the configuration for the JavaFX packaging tool to avoid this problem?

The build uses Gradle to call an Ant script to generate the bundle. Gradle fills in all of the necessary variables. The Ant script is as follows:

<project name="VocabHunter Packaging" basedir=""
         xmlns:fx="javafx:com.sun.javafx.tools.ant">
    <property environment="env"/>
    <property name="JAVA_HOME" value="${env.JAVA_HOME}"/>
    <target name="jfxbundle" description="Build the application bundle">
        <taskdef resource="com/sun/javafx/tools/ant/antlib.xml"
                 uri="javafx:com.sun.javafx.tools.ant"
                 classpath="${JAVA_HOME}/lib/ant-javafx.jar"/>
        <fx:deploy outdir="${basedir}/build"
                   nativeBundles="${packageType}">
            <fx:platform basedir=""/>
            <fx:application id="VocabHunterId"
                            name="VocabHunter"
                            mainClass="${mainClass}"
                            version="${version}"/>
            <fx:resources>
                <fx:fileset dir="${basedir}/build/libs"/>
            </fx:resources>
            <fx:info title="VocabHunter">
                <fx:association description="VocabHunter session"
                                extension="wordy"
                                mimetype="application/x-vnd.VocabHunterSession"
                                icon="${sessionIcon}"/>
            </fx:info>
            <fx:bundleArgument arg="icon"
                               value="${appIcon}"/>
            <fx:bundleArgument arg="mac.CFBundleVersion"
                               value="${version}"/>
            <fx:bundleArgument arg="launcher-cfg-format"
                               value="prop"/>
        </fx:deploy>
    </target>
</project>

You can see the full script in context here.

I'm testing this using JDK 1.8.0_92 on Ubuntu 14.04.


Solution

  • To answer this here too, you are required to have JRE_HOME being set for running some native JavaFX launcher without having bundled JRE. On Windows it looks inside the registry and searches for HKLM\Software\JavaSoft\Java Runtime Environment\[CurrentVersion]\JavaHome. I could NOT find any documentation about this.

    To workaround this, you are required to "update" the app.runtime-value as part of the postinst-script being executed while installing. Something like this:

    #!/bin/sh
    # postinst script for APPLICATION_NAME
    #
    # see: dh_installdeb(1)
    
    set -e
    
    # summary of how this script can be called:
    #        * <postinst> `configure' <most-recently-configured-version>
    #        * <old-postinst> `abort-upgrade' <new version>
    #        * <conflictor's-postinst> `abort-remove' `in-favour' <package>
    #          <new-version>
    #        * <postinst> `abort-remove'
    #        * <deconfigured's-postinst> `abort-deconfigure' `in-favour'
    #          <failed-install-package> <version> `removing'
    #          <conflicting-package> <version>
    # for details, see http://www.debian.org/doc/debian-policy/ or
    # the debian-policy package
    
    case "$1" in
        configure)
            echo Adding shortcut to the menu
    SECONDARY_LAUNCHERS_INSTALL
    APP_CDS_CACHE
            xdg-desktop-menu install --novendor /opt/APPLICATION_FS_NAME/APPLICATION_LAUNCHER_FILENAME.desktop
    FILE_ASSOCIATION_INSTALL
    
            if [ "SERVICE_HINT" = "true" ]; then
                echo Installing daemon
                cp /opt/APPLICATION_FS_NAME/APPLICATION_PACKAGE.init /etc/init.d/APPLICATION_PACKAGE
    
                if [ -x "/etc/init.d/APPLICATION_PACKAGE" ]; then
                    update-rc.d APPLICATION_PACKAGE defaults
    
                    if [ "START_ON_INSTALL" = "true" ]; then
                        if which invoke-rc.d >/dev/null 2>&1; then
                            invoke-rc.d APPLICATION_PACKAGE start
                        else
                            /etc/init.d/APPLICATION_PACKAGE start
                        fi
                    fi
                fi
    
            fi
    
            if [ -f /etc/profile ]; then
                # load special environment variables
                . /etc/profile
    
                # remove stored value in case of dpkg-reconfigure
                RUNTIME_PATH_TO_SET=""
    
                if [ -z "$JRE_HOME" ]; then
                    echo JRE_HOME is not set, checking for JAVA_HOME being set
                    if [ -z "$JAVA_HOME" ]; then
                        echo JAVA_HOME is not set, checking for known locations
    
                        # look for known locations
                        KNOWN_JDK_DIRS="/usr/lib/jvm/java-8-oracle /usr/lib/jvm/java-8-openjdk-amd64 /usr/lib/jvm/java-8-openjdk-i386"
                        FOUND_JAVA_HOME=""
    
                        # Look for the right JVM to use (use the first one)
                        for potentialjdkdir in $KNOWN_JDK_DIRS; do
                            if [ -r "$potentialjdkdir/bin/java" -a -z "$FOUND_JAVA_HOME" ]; then
                                FOUND_JAVA_HOME="$potentialjdkdir"
                            fi
                        done
    
                        if [ -z "$FOUND_JAVA_HOME" ]; then
                            # still nothing found :(
                            echo Please make sure to have Java installed and JRE_HOME variable set before running APPLICATION_LAUNCHER_FILENAME
                        else
                            echo Updating runtime-settings using known location
                            RUNTIME_PATH_TO_SET="$FOUND_JAVA_HOME"
                        fi
                    else
                        echo Updating runtime-settings using JAVA_HOME
                        # JAVA_HOME is set, use that value
                        RUNTIME_PATH_TO_SET="$JAVA_HOME"
                    fi
                fi
    
                # always write runtime-location, as it might get removed again when user calls dpkg-reconfigure
                sed -i "s|app.runtime=.*|app.runtime=$RUNTIME_PATH_TO_SET|g" /opt/APPLICATION_FS_NAME/app/APPLICATION_LAUNCHER_FILENAME.cfg
            fi
        ;;
    
        abort-upgrade|abort-remove|abort-deconfigure)
        ;;
    
        *)
            echo "postinst called with unknown argument \`$1'" >&2
            exit 1
        ;;
    esac
    
    # dh_installdeb will replace this with shell code automatically
    # generated by other debhelper scripts.
    
    #DEBHELPER#
    
    exit 0
    

    When using the javafx-maven-plugin or javafx-gradle-plugin, put this script inside src/main/deploy/package/linux with the filename postinst to get picked up by the javapackager/bundler.

    Disclaimer: I'm the maintainer of the javafx-maven-plugin and creator of the javafx-gradle-plugin

    EDIT: updated script for working on dpkg-reconfigure too