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.
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:
- Use for instance Azul Zulu with JavaFX included.
- Check installation instructions on the Azul Documentation website:
- Or use SDKMAN on Linux or macOS to easily install a JDK or switch to another one:
# 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:
- Follow the instructions on the website of the Apache Maven Project.
- Or use SDKMAN on Linux or macOS:
$ 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.
- Check out the sources, directly in your IDE or with
git clone https://github.com/FDelporte/javafx-jar-template.git
- Open in IntelliJ IDEA as a new Maven project
- Configure a JDK with JavaFX in “Project Structure > SDK”
- Go to the file
src/main/java/be/webtechie/app/AppLauncher.java
- Click on the green button “Run AppLauncher.main()”
Build as executable JAR with all dependencies
- Fill-in the name you want to use for your jar-file in
pom.xml
> build > finalName. - Make sure the
mainClass
is correct inpom.xml
> build > plugin > maven-assembly-plugin. - Run the Maven
package
command$ mvn package
. - You can now find the jar with all dependencies packages in
/target/{YOUR_FINALNAME}.jar
. - 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!