Template project to build a JavaFX application as a JAR with dependencies with Maven

Recently, I was asked what the best way is to build a Fat JAR (a JAR with all dependencies) using Maven. Therefore, I created a GitHub project javafx-jar-template that you can use as a starting point. It contains a small JavaFX demo application with the TilesFX dependency and the necessary plugins in the pom.xml file.

You can use Visual Studio Code or any other IDE for this project. I used IntelliJ IDEA with the following approach.

Screenshot of a JavaFX demo application displaying the GUI

Install JDK and Maven

Make sure you have the requirement tools installed on your machine.

Install Java JDK with JavaFX

This template requires Java 21 or newer. You can use any JDK and install the JavaFX runtime separately from the Gluon website. But the easiest way is to install a JDK which has JavaFX included:

# Install SDKMAN 
$ curl -s "https://get.sdkman.io" | bash
$ source "$HOME/.sdkman/bin/sdkman-init.sh"

# Install the Zulu JDK 21 with JavaFX 
# Answer Y to set as default
$ sdk install java 21.0.4.fx-zulu

Install Maven

If you didn’t install Maven before:

$ sdk install maven

Check Java and Maven Version

$ java -version
openjdk version "21.0.4" 2024-07-16 LTS
OpenJDK Runtime Environment Zulu21.36+17-CA (build 21.0.4+7-LTS)
OpenJDK 64-Bit Server VM Zulu21.36+17-CA (build 21.0.4+7-LTS, mixed mode, sharing)

$ mvn -version
Apache Maven 3.9.6 (bc0240f3c744dd6b6ec2920b3cd08dcc295161ae)
Maven home: /Users/frank/.sdkman/candidates/maven/current
Java version: 21.0.4, vendor: Azul Systems, Inc., runtime: /Users/frank/.sdkman/candidates/java/21.0.4.fx-zulu/zulu-21.jdk/Contents/Home
Default locale: en_US, platform encoding: UTF-8
OS name: "mac os x", version: "14.6.1", arch: "aarch64", family: "mac"

Run in IntelliJ IDEA

You can find the sources on GitHub: github.com/FDelporte/javafx-jar-template.

  1. Check out the sources, directly in your IDE or with git clone https://github.com/FDelporte/javafx-jar-template.git
  2. Open in IntelliJ IDEA as a new Maven project
  3. Configure a JDK with JavaFX in “Project Structure > SDK” Configuring the SDK in IntelliJIDEA
  4. Go to the file src/main/java/be/webtechie/app/AppLauncher.java
  5. Click on the green button “Run AppLauncher.main()” Running the application in IntelliJIDEA

Build as executable JAR with all dependencies

  1. Fill-in the name you want to use for your jar-file in pom.xml > build > finalName.
  2. Make sure the mainClass is correct in pom.xml > build > plugin > maven-assembly-plugin.
  3. Run the Maven package command $ mvn package.
  4. You can now find the jar with all dependencies packages in /target/{YOUR_FINALNAME}.jar.
  5. Run it with java -jar target/{YOUR_FINALNAME}.jar.

Conclusion

I hope this template helps you get started quickly with a new JavaFX project. Feel free to share your feedback or show me what you’ve built using this template!