Generate Native Image
Prerequisites
- Install VS2017+ in your Windows laptop. (Too easy in Linux and Mac)
If you are not installing it properly (E.G. Just copiedWindows Kits
and theMicrosoft Visual Studio
folder), you might lost some environment variable. Please remember to runMicrosoft Visual Studio\2022\Community\VC\Auxiliary\Build\vcvars64.bat
as thex64 Native Tools Command Prompt
You might also want to change the take care about these variables:- INCLUDE (Check it from some other machine with the
x64 Native Tools Command Prompt
byecho %INCLUDE%
) - LIB (Ditto)
- INCLUDE (Check it from some other machine with the
set INCLUDE=<Copy from another good machine>
set LIB=<Copy from another good machine>
Sometimes the Anti-Virus tool will block you from creating files at C:\Users\<user>\AppData\Local\Temp
. To make your life easier, you might change the TEMP and TMP variable by:
set TMP=c:\Temp
set TEMP=c:\Temp
- Install the GraalVM and Native-image tool for Windows.
For Swing / Awt related application. You might encounter below errors. You can avoid them by the Step-by-steps section.
# Error 1:
Exception in thread "AWT-EventQueue-0" java.lang.Error: java.home property not set
# Error 2:
xception in thread "main" java.lang.UnsatisfiedLinkError: no awt in java.library.path
at org.graalvm.nativeimage.builder/com.oracle.svm.core.jdk.NativeLibrarySupport.loadLibraryRelative(NativeLibrarySupport.java:120)
Step-by-steps
- Firstly, you need to generate the agent trace files
- Use
$GRAAL_HOME\bin\java
to run your jar (do not do it with IDE run directly because will add a lot of IDE related stuff and fail your build later on), add below JVM options-agentlib:native-image-agent=config-output-dir=<project-base>\src\main\resources\META-INF\native-image\<your group>\<your artifact>
- Run all the app with all use cases.
- Close the app.
- Use mvn to repackage the app. (Ensure the generated META-INF files are included to your package)
- Use
- Start the
x64 Native Tools Command Prompt for VS 2022
- Run native-image command to build the jar created by maven.
# -O0 for testing purpose, it will not optimize the exe
> native-image -O0 -o my-exe-file -jar <project base>\target\my-jar.jar --initialize-at-build-time=org.slf4j.LoggerFactory -H:+ReportExceptionStackTraces
- Each time failed, you can adjust the
--initialize-at-build-time
and--initialize-at-run-time
to remove some errors. For example, you might encounter a lot errors like below. (You should read the error message carefully, with the- --trace-class-initialization=<track the class>
, you will know which Class to be added to the initialize at run/build time.) (Example https://stackoverflow.com/questions/63328298/)
SEVERE: Unable to initialize BouncyCastleAlpnSslUtils.
java.lang.ClassNotFoundException: org.bouncycastle.jsse.BCSSLEngine
Error: No instances of java.net.Inet4Address are allowed in the image heap as this class should be initialized at image runtime. Object has been initialized without the native-image initialization instrumentation and the stack trace can't be tracked.
Trace: Object was reached by ...
Error: com.oracle.graal.pointsto.constraints.UnsupportedFeatureException: No instances of java.net.Inet4Address are allowed in the image heap as this class should be initialized at image runtime. Object has been initialized without the native-image initialization instrumentation and the stack trace can't be tracked.
- After tested okay, use the optimized parameter to re-compile it.
# Now optimize it
> native-image -H:Name=my-exe-file -jar <project base>\target\my-jar.jar --initialize-at-build-time=org.slf4j.LoggerFactory -H:+ReportExceptionStackTraces -o my-exe-file -O3
- You need to include some lib files in JDK to avoid awt issue.
> dir lib
2022/11/24 17:58 <DIR> .
2022/11/24 17:58 <DIR> ..
2022/10/20 12:43 4,630 fontconfig.bfc
2022/10/20 12:43 12,345 fontconfig.properties.src
2022/10/20 12:43 3,793 psfont.properties.ja
2022/10/20 12:43 11,390 psfontj2d.properties
- Then run command:
bin/my-exe.exe -Djava.home=<the parent folder path>
(The java home is very simple to contain thelib
folder and the 4 font related files in it)
-- parent folder
|-- bin (The files inside is generated by the graal-vm automatically)
|-- my-exe.exe
|-- jvm.dll
|-- awt.dll
|-- ... (some other dll files)
|-- lib (this one nees to be manually prepared)
|-- fontconfig.bfc
|-- fontconfig.properties.src
|-- psfont.properties.ja
|__ psfontj2d.properties
- To remove the console, run below command in the
x64 Native Tools Command Prompt
EDITBIN /SUBSYSTEM:WINDOWS my-exe.exe
网友评论