使用 exe4j 将 jar 包生成 .exe 文件(敲详细)

3,953 阅读3分钟

这是我参与11月更文挑战的第11天,活动详情查看:2021最后一次更文挑战

这里为你提供了如何使用 IDEA 将普通的 Java 项目打包成 Jar 包:IDEA 将普通 Java 项目打包成 Jar 包并运行

下载 exe4j

  1. 进入 exe4j 官网:exe4j.apponic.com/

    image-20211118100609313

  2. 点击【Download】。

    image-20211118100609313

  3. 选择32位或者64位,这里以(64位为例)。

    image-20211118100947958

  4. 点击 【Download】。

    image-20211118101140078

  5. 等待下载完成。

    image-20211118101314158

  6. 下载完成。

    image-20211118101456993

安装 exe4j

  1. 下载好的安装包。

    image-20211118101456993

  2. 双击安装包运行。

    image-20211118101526106

  3. 点击【Next】。

    image-20211118101544636

  4. 勾选【I accept the agreement】,点击【Next】。

    image-20211118101635532

  5. 设置安装路径,点击【Next】。

    image-20211118101653845

  6. 等待安装完成。

    image-20211118101750758

  7. 安装完成。

    image-20211118101801334

将 jar 包转为 .exe 可执行文件

  1. 准备好 ico 文件,和 jar 包。

    image-20211118100024715

  2. 双击运行 exe4j 的快捷方式。(桌面上没有,可以在开始菜单中找到)。

    image-20211118102225221

  3. 等待加载。

    请添加图片描述

  4. 加载完成,显示界面。

    image-20211118102432264

  5. 注册(可以不注册,非必须)。

    • 点击【Enter License】。

      image-20211118102830447

    • Name 和 Company 随便写,key随便复制一个,点击【OK】。

      image-20211118102944743

      #License key
      A-XVK340905F-uz69um1sjndaw
      A-XVK357958F-19b2w67pc0wy9
      A-XVK324407F-1saykltgf45ql
      A-XVK336714F-1079gjvozhge8
      A-XVK305570F-g3fzepj3d116
      A-XVK382630F-1ea4iqy1mgpyx6
      A-XVK389779F-h7jy4e1wwisie
      A-XVK393584F-117gf4lnhyz78
      A-XVK311336F-qg2iqj9hq6r7
      A-XVK367311F-116kxq0a2ervk
      
    • 注册成功。

      image-20211118103008814

  6. 点击【Next】。

    image-20211118104632062

  7. 勾选【“JAR in EXE” mode】,点击【Next】。

    image-20211118110454339

  8. 填写名字和输出路径,点击【Next】。

    image-20211118110721418

  9. 设置。

    image-20211118111656042

  10. 如果需要在64位系统上运行,则点击左边的【32-bit or 64-bit】,点击【Next】。

    image-20211118112040511

  11. 默认即可,点击【Next】。

    image-20211118112223213

  12. 设置-Dfile.encoding=utf-8防止乱码,点击右侧【+】。

    image-20211118112357520

  13. 导入准备好的 Jar 包

    image-20211118112510389

  14. 点击【...】。

    image-20211118112545347

  15. 选则包含 Main 方法的主类,点击 【OK】。

    image-20211118112605768

  16. 点击【Next】。

    image-20211118112632181

  17. 设置 Jre 版本。

    image-20211118112800458

  18. 如果你不需要在无安装 JDK 的电脑上运行,那么可以跳过此步。

    1. 点击左侧【Search sequence】。

      image-20211118132215822

    2. 选中两个环境变量,点击右侧【×】号进行删除。

      image-20211118132322112

    3. 删除环境变量之后,点击右侧【+】。

      image-20211118132926789

    4. 添加 JRE。注意使用 JRE 的相对路径。

      image-20211118132820669

      注:exe4j 并不能将 JRE 和 JAR 一块打包,所以想要在无安装 JDK 的电脑上运行时就需要附带 JRE 或者将 JRE 存放到设置的相对路径下。

    5. 添加之后,点击【Next】。

      image-20211118133731263

    6. 选则默认的 JVM,点击【Next】。

      image-20211118133812030

  19. 点击【Next】。

    image-20211118112824828

  20. 点击【Next】。

    image-20211118112838843

  21. 等待完成

    image-20211118112851482

  22. 转换完成,点击 Click Here to Start the Application 运行

    image-20211118112907534

  23. 运行完成

    image-20211118112925478

运行转换完成的 .exe 文件

点击 exe4j 的 Click Here to Start the Application 运行

image-20211118112907534

运行

image-20211118112925478

双击 .exe 运行

exe4j_exe

注:运行时 DOS 命令窗口自动关闭是正常现象,Java 项目运行完成就会自动关闭命令窗口。

通过 DOS 命令窗口运行

通过 DOS 命令窗口运行,可以直接在窗口看到运行结果,窗口也不会自动关闭。

exe4j_exe2