Search code examples
javaexecutablejpackage

How to use jpackage with preview features in Java


I am trying to create a Microsoft Windows .exe version of my application using jpackage. However, my application uses features that are currently in preview. Here is my build script.

#compiles with preview features enabled
javac --enable-preview --source 21 -d classes src/Helltaker/Main.java src/module-info.java

#creates the jar file in the run folder
jar --create --file run/HelltakerPathFinder_run_me.jar --main-class Helltaker.Main -C classes .

#deletes the jlink created runtime from previous runs of this script
rm -rf ../_CUSTOM_JAVA_RUNTIME

#creates a minimized version of the Java Runtime and stores it into the _CUSTOM_JAVA_RUNTIME folder
jlink --output ../_CUSTOM_JAVA_RUNTIME --add-modules davidalayachew.HelltakerPathFinder --module-path run

#use jpackage to package it up for installation and running
jpackage \
         --input run \
         --name HelltakerPathFinder \
         --description "A path finding algorithm for the video game Helltaker -- written in Java 20" \
         --main-jar HelltakerPathFinder_run_me.jar \
         --win-console \
         --runtime-image ../_CUSTOM_JAVA_RUNTIME

So, I tried it, and it finished without error. A .exe file was created. I moved the executable to a different folder, double clicked it, and then an install started. Once the install finished, I navigated to the newly installed application's location (C:\Program Files\HelltakerPathFinder), opened up a windows terminal there, started the application, and then it failed with the following message.

PS C:\Program Files\HelltakerPathFinder> .\HelltakerPathFinder.exe
Error: LinkageError occurred while loading main class Helltaker.Main
        java.lang.UnsupportedClassVersionError: Preview features are not enabled for Helltaker/Main (class file version 65.65535). Try running with '--enable-preview'

So, I tried running with --enable-preview and got this.

PS C:\Program Files\HelltakerPathFinder> .\HelltakerPathFinder.exe --enable-preview
Error: LinkageError occurred while loading main class Helltaker.Main
        java.lang.UnsupportedClassVersionError: Preview features are not enabled for Helltaker/Main (class file version 65.65535). Try running with '--enable-preview'

How do I get this .exe file to run with --enable-preview?


Solution

  • There's the --java-options flag for that:

    --java-options <java options>
          Options to pass to the Java runtime
          This option can be used multiple times.
    

    So, for you that would be:

    --java-options '--enable-preview'