Open Source Development License Product Subscriptions provided by JetBrains
Graal VM best practice, use Java to develop CLI, Desktop (Java FX), Web (Spring Boot) projects, and use native-image technology to statically compile Java code into independent executable files (native images).
Graal VM makes Java powerful again, using native-image to compile the program into an executable file of the target platform, and run directly without jvm, the startup speed is very fast, and the memory load is also very low.
To experience the executable file I have compiled (Windows, Linux, Mac), click here to download.
The following performance test results are tested on my native Windows. The test results are related to the machine configuration, especially the time-consuming compilation of
native-image
.
↓Tag \ Module→ | cli-normal | desktop-javafx | web-springboot |
---|---|---|---|
Module desc | Command line app (no framework) | Desktop app (JavaFx) | Web app (SpringBoot) |
JDK | 11 or 17 | 11 or 17 | 11 or 17 |
GraalVM | CE-21.3.0 | CE-21.3.0 | CE-21.3.0 |
Maven Plugin | native-maven-plugin | gluonfx-maven-plugin | spring-native |
Time-consuming to start (jvm) | 0.713s | 2.555s | 1.793s |
Time-consuming to start (native-image) | 0.047s | 0.665s | 0.216s |
Memory load(jvm) | 38.8m | 309.3m | 440.5m |
Memory load(native-image) | 3.1m | 60.4m | 70.2m |
Time-consuming to start (native-image) | 24.786s | 93.455s | 99.434s |
Executable file size (7z compression) | 8.03m (7z : 1.68m) | 62.7m (7z : 13.1m) | 66.5m (7z : 13.9m) |
New module
Brief introduction of new modules and basic test data
- web-jlhttp
- Embedded HTTP Server implemented with only 3000 lines of Java code, sometimes we just want to write one or two simple interfaces for packaging and publishing. Using frameworks such as Spring is really a fuss.
- jarfile:
52k
,Executable file size:12.9m
, 7z:2.9m
- web-nanohttpd
- Another lightweight and well-designed embedded HTTP Server implementation. The library is commonly used for Android application development, but it can be used normally with Graal VM here.
- jarfile:
54k
,Executable file size:12.8m
, 7z:2.9m
- cli-picocli
- Picocli is a modern framework for building powerful, user-friendly, GraalVM-enabled command line apps with ease. It supports colors, autocompletion, subcommands, and more.
- jarfile:
676k
,Executable file size:18.2m
,7z:4.0m
- web-springboot3
- Spring Boot 3.0 has been officially released, supporting Graalvm Native Images, replacing experimental Spring Native projects.
- jarfile:
19.2m
,Executable file size:72.0m
,7z:16.4m
- 2021-03-05 GraalVM-21.0.0.2 (Java 8 or Java 11)
First commit
- org.graalvm.nativeimage/native-image-maven-plugin:21.0.0.2
cli
- com.gluonhq/client-maven-plugin:0.1.38
javafx 15.0.1
- org.springframework.experimental/spring-graalvm-native:0.8.5
springboot 2.4.3
- org.graalvm.nativeimage/native-image-maven-plugin:21.0.0.2
- 2021-12-10 GraalVM-21.3.0 (Java 11 or Java 17)
- org.graalvm.buildtools/native-maven-plugin:0.9.8
cli
- com.gluonhq/gluonfx-maven-plugin:1.0.10
javafx 17.0.1
- org.springframework.experimental/spring-native:0.11.0
springboot 2.6.1
- org.graalvm.buildtools/native-maven-plugin:0.9.8
The following is the development environment of my machine. In theory, windows, linux and mac are all supported (note that the dependencies of GraalVM SDK and native-image of different platforms are different).
- Windows 10 (CPU: i7-7700, RAM: 16G)
- IntelliJ IDEA 2020
- graalvm-ce-java11-21.3.0
- Visual Studio 2019
Graal VM
- Download Graal VM SDK
- Set GraalVM environment variables (note that JAVA_HOME also points to GRAALVM_HOME):
GRAALVM_HOME = C:\path\to\graalvm-ce-java11-21.3.0
JAVA_HOME = %GRAALVM_HOME%
PATH += %GRAALVM_HOME%\bin
- Verify the environment
java -version
C:\Users\Administrator>java -version
openjdk version "11.0.13" 2021-10-19
OpenJDK Runtime Environment GraalVM CE 21.3.0 (build 11.0.13+7-jvmci-21.3-b05)
OpenJDK 64-Bit Server VM GraalVM CE 21.3.0 (build 11.0.13+7-jvmci-21.3-b05, mixed mode, sharing)
- Install native-image components
gu install native-image
- Verification component
gu list
native-image --version
MSVC (Visual Studio 2019)
In addition to GraalVM, Microsoft Visual Studio 2019 is also required. The community version is enough, You can download it from here
During the installation process, make sure to select at least the following individual components:
- Choose the English Language Pack
- C++/CLI support for v142 build tools (14.25 or later)
- MSVC v142 - VS 2019 C++ x64/x86 build tools (v14.25 or later)
- Windows Universal CRT SDK
- Windows 10 SDK (10.0.19041.0 or later)
All build commands, be it with Maven or Gradle, must be executed in a Visual Studio 2019 Command Prompt called x64 Native Tools Command Prompt for VS 2019. A shortcut can be found in the "Start Menu", or you can search the application in the search box. Read the Microsoft documentation for more information.
Alternatively, you can run
cmd.exe /k "<path to VS2019>\VC\Auxiliary\Build\vcvars64.bat
from any other terminal before you can start using the build commands.
Recommend this ultimate solution: Visual Studio 2019 configure MSVC environment variables, use the command line to compile,In this way, MSVC compilation tools can be used directly on any terminal.
Be sure to use this ultimate solution, otherwise it will become very troublesome in the build project. Every time before
mvn package
, you must executecmd.exe k "<path to VS2019>\VC\Auxiliary\ Build\vcvars64.bat
.
Linux
Download Graal VM and configure environment variables. In addition to Graal VM, the following software packages are also required:
- gcc version 6 or higher
- ld version 2.26 or higher
- Ubuntu 18 also needs to install the following libraries (I have tested it in the Linux subsystem):
sudo apt install pkg-config libgl-dev libglib2.0-dev libgtk-3-dev libpango1.0-dev libx11-dev libxtst-dev libasound2-dev libavcodec-dev libavformat-dev libavutil-dev
- Ubuntu 20 also needs to install the following libraries
sudo apt install g++-multilib
- For other Linux distributions, please check which dependent libraries are missing for errors when packaging, and install them yourself.
Mac
- xcode-select --install
For a more detailed description of different platform configurations and dependencies, please refer to:
- install-native-image
Reference when building Java applications
- gluon documentation
Reference when building JavaFx applications
tips:Gluon is a contributor to the
OpenJFX
project and theGraalVM
project. The company providesclient-maven-plugin
to encapsulate the related commands ofnative-image
, which simplifies the packaging operation.
- Through the above steps, you have configured the development environment. Another thing to note is that in IDEA development tools, when you need to set the JDK for the project, you should directly point to the bin directory under GraalVM, not other JDK directories, Otherwise an error may occur during compilation.
- GraalVM should not support cross-compilation, but you can use the Linux subsystem provided by Windows to compile the source code.
For other implementations of these three applications, more modules may be added later.
- cli-<A library that supports parsing args parameters>
For faster development of cli applications
- desktop-<Swing/AWT>
Other GUI implementations
- web-<Lightweight, containerless http-server library>
Other web implementations
- Q Group: 707416319
- About the author: I'm westinyang
- Official information
- Get Started with GraalVM
- native-image documentation
- JavaFX website
Recommended reading
- Spring Native for GraalVM documentation
Recommended reading
- Web articles
- How to evaluate the GraalVM project?
Recommended reading
- Graal VM: Java in the age of microservices
Recommended reading
Very comprehensive explanation
- Generate native image for JavaFX application
- Use GraalVM under Windows to compile JavaFX applications into exe
- Spring Boot as GraalVM Native Images
Recommended reading
- Spring Boot GraalVM Native Image builds with the native-image-maven-plugin
Recommended reading
- How to build native CLI applications written in Java with Graalvm and Picocli
推荐阅读
- How to evaluate the GraalVM project?