简体   繁体   中英

How To Build Java Native Executable Files For Linux

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.

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

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


I think ```jlink``` is useable with older Java versions too

if anyone has experience with jlink on older versions please comment below

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM