java 一键打包exe (GraalVM方式)

1.GraalVM 版本下载java地址
https://www.graalvm.org/downloads/
这边我选择java17长期支持版本和window系统方式。
本次安装电脑window10专业版本
在这里插入图片描述
2.下载好后,进行解压

tar -zxvf apache-maven-3.9.8-bin.tar.gz

在这里插入图片描述
3.增加变量
名称:GRAALVM_HOME
路径:C:\soft\graalvm-jdk-17.0.11+7.1
在这里插入图片描述
Path 增加 %GRAALVM_HOME%\bin
4. 安装成功后,进行查看

D:\javatest\src\main\java>where java
C:\soft\graalvm-jdk-17.0.11+7.1\bin\java.exe
C:\soft\jdk1.8.0_152\bin\java.exe
D:\javatest\src\main\java>where gu
C:\soft\graalvm-jdk-17.0.11+7.1\bin\gu.cmd

到这边恭喜您成功安装了graalvm-jdk。
5. 安装
Visual Studio 下载网址:https://visualstudio.microsoft.com/zh-hans/downloads/
本次安装的是visual studio community 版本
在这里插入图片描述
6.下载后打开选择,建议用稳定版本。
选择表黄的部分c++桌面开发和右侧标黄的组件。
在这里插入图片描述
6. 安装成功后,执行 helloworld.java文件

public class helloworld {
    public static void main(String[] args) throws InterruptedException {
        Thread.sleep(1000);
        System.out.println("hello world");
        Thread.sleep(1000);
    }
}

javac helloworld.java
native-image helloworld

D:\javatest\src\main\java>javac helloworld.java

D:\javatest\src\main\java>native-image helloworld
------------------------------------------------------------------------------------------------------------------------
[1/8] Initializing...                                                                                   (14.2s @ 0.17GB)
 Java version: 17.0.11+7-LTS, vendor version: Oracle GraalVM 17.0.11+7.1
 Graal compiler: optimization level: 2, target machine: x86-64-v3, PGO: ML-inferred
 C compiler: cl.exe (microsoft, x64, 19.40.33811)
 Garbage collector: Serial GC (max heap size: 80% of RAM)
[2/8] Performing analysis...  [*****]                                                                    (5.7s @ 0.24GB)
   1,816 (59.40%) of  3,057 types reachable
   1,686 (45.56%) of  3,701 fields reachable
   7,656 (35.87%) of 21,344 methods reachable
     621 types,     0 fields, and   284 methods registered for reflection
      53 types,    30 fields, and    48 methods registered for JNI access
       1 native library: version
[3/8] Building universe...                                                                               (0.9s @ 0.26GB)
[4/8] Parsing methods...      [*]                                                                        (1.7s @ 0.27GB)
[5/8] Inlining methods...     [***]                                                                      (0.5s @ 0.26GB)
[6/8] Compiling methods...    [****]                                                                    (15.7s @ 0.30GB)
[7/8] Layouting methods...    [*]                                                                        (0.5s @ 0.29GB)
[8/8] Creating image...       [*]                                                                        (1.1s @ 0.32GB)
   2.81MB (44.22%) for code area:     3,450 compilation units                                                           
   3.46MB (54.39%) for image heap:   49,016 objects and 1 resources
  90.44kB ( 1.39%) for other data
   6.36MB in total
------------------------------------------------------------------------------------------------------------------------
Top 10 origins of code area:                                Top 10 object types in image heap:
   1.46MB java.base                                          540.98kB byte[] for code metadata
   1.15MB svm.jar (Native Image)                             413.68kB byte[] for java.lang.String
  70.93kB com.oracle.svm.svm_enterprise                      324.76kB char[]
  34.21kB org.graalvm.nativeimage.base                       324.21kB java.lang.String
  32.47kB org.graalvm.sdk                                    298.77kB java.lang.Class
  19.34kB jdk.internal.vm.ci                                 252.51kB byte[] for general heap data
  12.82kB jdk.internal.vm.compiler                           150.00kB java.util.HashMap$Node
   1.75kB helloworld                                          79.13kB java.lang.Object[]
   1.17kB jdk.proxy3                                          70.94kB com.oracle.svm.core.hub.DynamicHubCompanion
   1.15kB jdk.proxy1                                          69.75kB byte[] for reflection metadata
  360.00B for 1 more packages                                432.83kB for 496 more object types
------------------------------------------------------------------------------------------------------------------------
Recommendations:
 PGO:  Use Profile-Guided Optimizations ('--pgo') for improved throughput.
 HEAP: Set max heap for improved and more predictable memory usage.
 CPU:  Enable more CPU features with '-march=native' for improved performance.
 QBM:  Use the quick build mode ('-Ob') to speed up builds during development.
 BRPT: Try out the new build reports with '-H:+BuildReport'.
------------------------------------------------------------------------------------------------------------------------
                        1.0s (2.3% of total time) in 258 GCs | Peak RSS: 0.82GB | CPU load: 3.73
------------------------------------------------------------------------------------------------------------------------
Produced artifacts:
 D:\javatest\src\main\java\helloworld.exe (executable)
========================================================================================================================
Finished generating 'helloworld' in 40.9s.

最后单击运行 helloworld.exe可以运行了
在这里插入图片描述
8. 如果看到这边,你跟我一样安装的不是特别顺利

出现了以下报错 Visual Studio 2022 没有安装成功

D:\javatest\src\main\java\hello>native-image hello.hellowrold
Error: Failed to find 'vcvarsall.bat' in a Visual Studio installation.
Please make sure that Visual Studio 2022 version 17.1.0 or later is installed on your system. You can download it at https://visualstudio.microsoft.com/
downloads/. If this error persists, please try and run GraalVM Native Image in an x64 Native Tools Command Prompt or file a ticket.

最关键的一步骤过来了,需要安装手动安装VC_redist.x64.exe和VC_redist.x86.exe
找到这个目录C:\ProgramData\Microsoft\VisualStudio\Packages
![在这里插入图片描述](https://img-blog.csdnimg.cn/direct/3fbce202fce14310a395c1443ca3a23e.png
在这里插入图片描述
搜索VC_redist,两个都需要安装。安装好之后必须重启电脑。在这里插入图片描述

最后更多里面选择修复
在这里插入图片描述
9. 过程很坎坷,到这边恭喜你成功。
10. 参考文档:https://blog.csdn.net/jpkopkop/article/details/127675664 https://blog.csdn.net/tangdou369098655/article/details/135738459

  • 26
    点赞
  • 17
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

金融小白数据分析之路

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值