Making and Running a Jar, for a Modular JavaFX Project

Written 8/10/21

Let’s say you have a working Maven project using JavaFX, and you wish to make and share a .jar file. In this article I will show a way to craft a CLI command to run this jar. The days when a simple java -jar myProject.jar sufficed are long gone!

For generating the jar, we can use the standard “mvn clean install”. (In Eclipse we enter: clean install into the Goals field of the Run configuration file.) The generated jar file will be located in the project’s /target directory.

To run the resulting jar file, I’ve not found a way to use the -jar option. There may be a way to do so, involving editing the MANIFEST as well as adding the needed module options. However, the following example command has proved successful. I will explain all the parts in the text that follows. Note, this line has to be executed in the same file location as the .jar file.

WINDOWS:
java -cp myProject-0.0.1.jar –module-path "%PATH_TO_FX%" --add-modules javafx.graphics,javafx.controls my.package.path.LaunchMyProject

MAC/LINUX:
java -cp myProject-0.0.1.jar --module-path $PATH_TO_FX --add-modules javafx.graphics,javafx.controls my.package.path.LaunchMyProject

java -cp myProject-0.0.1.jar
With the -cp parameter, we list the jar file itself to be included on the class path.

–module-path “%PATH_TO_FX%” (Windows)
–module-path $PATH_TO_FX (Mac/Linux)
The path to the JavaFX modules must be included. Locations for any other modules employed by the project would be “;” separated on Windows, and “:” separated on Mac/Linux. The location here can be spelled out as an absolute address, but I am showing the use of an environment variable PATH_TO_FX (following the example in Gluon’s documentation). This variable should be configured to address the /lib folder within a JavaFX SDK, which can be downloaded from Gluon. The /lib folder holds .jar files for each JavaFX module. The reason for placing the environment variable in quotes in the Windows example is that one of the parent folders of my chosen location happens to have a space in the name.

If the person you wish to have run the jar does not have the JavaFX SDK downloaded, they will have to do so, and they will also have to know how to address the /lib folder and to edit the command line accordingly. You might consider, as an alternative, shipping the needed JavaFX SDK (about 80MB as of this writing) along with your project jar. If you do so, you can also provide a relative address to the files.

For example, with the following structure (and assuming the user doesn’t move anything):

projectFolder/myProject.jar
projectFolder/javafx-sdk-16

we would run the command from the projectFolder/ location, and specify the module-path parameter as follows:

--module-path javafx-sdk-16\lib

Unfortunately, copying over just the required jars from the lib file does not appear to be sufficient. AFAIK, the entire SDK is required.

–add-modules javafx.graphics,javafx.controls
A comma-separated list (both Windows and Mac/Linux) of the names of required modules must be provided. The names can be found in the module-info.java file requires lines. Unnamed defaults do not need to be included in this list, they will be automatically derived. The *module-info" file may also have a requires line for various Java language modules, e.g., java.desktop, but the local Java (assuming it’s a modular version) should have these covered.

my.package.path.LaunchMyProject
This example text is a stand-in for the class that holds the main() method. If you are unsure of the address, it can be determined by converting the .jar to a .zip and inspecting the file folder structure. Note that there are no “/” or “\” (forward or back slashes) in the address, just “.” delimiters.

-END-

1 Like