本篇文章我会根据自己的本机安装GraalVM,并将一个简单的jar打包成一个可执行文件
机器:mac 处理器2.7 GHz 四核Intel Core i7 (非M1)
GraalVM:17 (支持的jdk为8-17),因此不需要找单独的对应版本的jdk,他是从8开始兼容到当前的。
第一步:登陆 GraalVM官网(我选择的17设备环境根据自己实际情况选择,win环境还需要安装 Visual Studio 和 Microsoft Visual C++(MSVC))
下砸预览:从解压后可以看出支持的jdk是8.1-17.07
第二部进入到graalvm-jdk-17.0.7+8.1/Contents/Home目录下
第三步:继续进入bin目录下
现将ant-0.0.1-SNAPSHOT.jar 使用graalv编译为二进制可执行文件(将jar移动到native-image所在目录)
使用命令:./native-image -jar ant-0.0.1-SNAPSHOT.jar ant
houliangdeMacBook-Pro:bin houliang$ ./native-image -jar ant-0.0.1-SNAPSHOT.jar ant
========================================================================================================================
GraalVM Native Image: Generating 'ant' (executable)...
========================================================================================================================
[1/8] Initializing... (8.7s @ 0.17GB)
Java version: 17.0.7+8-LTS, vendor version: Oracle GraalVM 17.0.7+8.1
Graal compiler: optimization level: 2, target machine: x86-64-v3, PGO: off
C compiler: cc (apple, x86_64, 12.0.5)
Garbage collector: Serial GC (max heap size: 80% of RAM)
[2/8] Performing analysis... [***] (13.5s @ 0.42GB)
3,714 (74.24%) of 5,003 types reachable
4,682 (52.83%) of 8,863 fields reachable
18,584 (48.58%) of 38,251 methods reachable
1,230 types, 0 fields, and 565 methods registered for reflection
59 types, 60 fields, and 52 methods registered for JNI access
4 native libraries: -framework Foundation, dl, pthread, z
[3/8] Building universe... (1.6s @ 0.53GB)
Warning: Reflection method java.lang.Class.getMethod invoked at org.springframework.boot.loader.jar.JarFileEntries.<clinit>(JarFileEntries.java:66)
Warning: Reflection method java.lang.Class.getDeclaredMethod invoked at org.springframework.boot.loader.MainMethodRunner.run(MainMethodRunner.java:47)
Warning: Reflection method java.lang.Class.getDeclaredConstructor invoked at org.springframework.boot.loader.jar.Handler.getFallbackHandler(Handler.java:200)
Warning: Aborting stand-alone image build due to reflection use without configuration.
Warning: Use -H:+ReportExceptionStackTraces to print stacktrace of underlying exception
------------------------------------------------------------------------------------------------------------------------
1.4s (5.5% of total time) in 69 GCs | Peak RSS: 0.95GB | CPU load: 4.66
========================================================================================================================
Finished generating 'ant' in 24.0s.
Generating fallback image...
Warning: Image 'ant' is a fallback image that requires a JDK for execution (use --no-fallback to suppress fallback image generation and to print more detailed information why a fallback image was necessary).
可执行文件和jar文件进行比较: 大约只有jar包的十分之一
执行ant (win系统会生成一个exe可执行文件)
houliangdeMacBook-Pro:bin houliang$ ./ant
. ____ _ __ _ _
/\\ / ___'_ __ _ _(_)_ __ __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
\\/ ___)| |_)| | | | | || (_| | ) ) ) )
' |____| .__|_| |_|_| |_\__, | / / / /
=========|_|==============|___/=/_/_/_/
:: Spring Boot :: (v2.7.7-SNAPSHOT)
2023-06-25 14:33:53.089 INFO 22037 --- [ main] com.cn.mini.ant.AntApplication : Starting AntApplication v0.0.1-SNAPSHOT using Java 17.0.7 on houliangdeMacBook-Pro.local with PID 22037 (/Users/houliang/gvm/graalvm-jdk-17.0.7+8.1/Contents/Home/bin/ant-0.0.1-SNAPSHOT.jar started by houliang in /Users/houliang/gvm/graalvm-jdk-17.0.7+8.1/Contents/Home/bin)
2023-06-25 14:33:53.092 INFO 22037 --- [ main] com.cn.mini.ant.AntApplication : No active profile set, falling back to 1 default profile: "default"
2023-06-25 14:33:53.960 INFO 22037 --- [ main] o.s.b.w.embedded.tomcat.TomcatWebServer : Tomcat initialized with port(s): 8082 (http)
2023-06-25 14:33:53.971 INFO 22037 --- [ main] o.apache.catalina.core.StandardService : Starting service [Tomcat]
2023-06-25 14:33:53.971 INFO 22037 --- [ main] org.apache.catalina.core.StandardEngine : Starting Servlet engine: [Apache Tomcat/9.0.70]
2023-06-25 14:33:54.042 INFO 22037 --- [ main] o.a.c.c.C.[Tomcat].[localhost].[/] : Initializing Spring embedded WebApplicationContext
2023-06-25 14:33:54.043 INFO 22037 --- [ main] w.s.c.ServletWebServerApplicationContext : Root WebApplicationContext: initialization completed in 899 ms
2023-06-25 14:33:54.343 INFO 22037 --- [ main] o.s.b.w.embedded.tomcat.TomcatWebServer : Tomcat started on port(s): 8082 (http) with context path ''
2023-06-25 14:33:54.351 INFO 22037 --- [ main] com.cn.mini.ant.AntApplication : Started AntApplication in 1.851 seconds (JVM running for 2.368)
访问接口:
构建运行结束