Java程序开发完成后,通常情况下以jar包的形式发布。但有时我们需要给非软件开发人员使用程序,如制作好窗体应用,把它发给没有java开发环境的人使用,此时就需要制作exe安装包。本文介绍如何将java程序制作成exe安装包,并提供有图片和三方依赖jar包的解决方案。
1.安装exe制作软件
制作exe安装包需要用到一个打包软件,exe4j,下载地址 https://exe4j.apponic.com/
2.打包应用程序
准备一个写好的应用程序,记住main方法的位置,如果有多个main方法,一定要记住哪个是启动程序的入口。

没有图片可忽略这里)注意如果有用到图片,那么一定要放在src根目录下,引用图片的时候用this.getClass().getResource("相对路径")的方式去引用,不能用getClassLoader的方式,不然打包之后图片可能显示是空白的。相对路径"/images/arg.png"前面有一个/表示的是根路径,也就是src。

在idea中选File->Project Structure




在idea中选Build->Build Artifacts

这里jar包的名字一般和项目名是一致的,项目叫xx,那么生成的jar就是xx.jar


把所有的jar包全部复制到一个文件夹里,包括项目jar包和三方依赖jar包,如果有图片,把图片文件夹也复制过来。
【重点】因为要把项目给没有安装java环境的人用,所以要把jre一起打包发给他,把自己电脑里的jre也复制过来

3.制作exe
运行exe4j


【重点】Output directory非常重要,一定要和刚才复制出来的jar和jre放在一个目录下


选择主类所在的jar包




多个主类查看方法:进入jar包所在文件夹终端输入: jar xf your-jar-file.jar META-INF/MANIFEST.MF
"your-jar-file.jar"替换成你的jar包名称
如果有三方依赖jar包,依次添加进来

选择运行版本,前面复制的jre是什么版本们这里就写什么版本

【重点】默认打包配置是找电脑上的JAVA_HOME,但是没有配置JAVA_HOME的电脑就不能运行,所以要删除默认配置,选择我们复制过去的jre

这里的.\jre表示相对路径,.\就是后面生成exe的父路径,保持它是.\就可以,不要改




