Reputation: 632
I'm looking for a way to build my Java or JavaFx apps so that I have a single executable file that runs without installing a Java runtime on the target system that is running Linux (like a .exe on Windows)
Usually, Java apps are packaged into jar files that can be run like this
java -jar <file-name>.jar
This requires us to install a Java JRE beforehand.
I'm looking for a solution to package my Java app so that it can execute without needing a Java JRE on the target machine.
Upvotes: 0
Views: 3767
Reputation: 632
as @Slaw mentioned in a comment for java 14+ this is the best solution
it uses the jlink
command internally
and supports Mac, Windows, and Linux
The official Java-based solution is mentioned above but its highly recommended to use GraalVM in case you want extra performance and a better approach for microservices
installation steps are mentioned below in @Kevin Hooke's answer
if anyone has experience with jlink
on older versions please comment below
Upvotes: 0
Reputation: 2621
You can use GraalVM to compile Java apps to native platform executables.
First install GraalVM following steps here: https://www.graalvm.org/docs/getting-started/#install-graalvm
Install the native-image plugin:
gu install native-image
Compile your Java app with javac as normal:
javac YourApp.java
Compile to a native executable:
native-image YourApp
Execute your native executable:
./YourApp
Upvotes: 1