java->jar->exe->setup.exe

本文详细介绍如何将Java应用程序打包成exe文件及安装包,包括使用Eclipse导出jar文件、利用Fatjar插件合并第三方库、借助exe4j生成可执行文件、通过InnoSetup制作安装程序等步骤。

http://blog.youkuaiyun.com/mamba10/article/details/8007047

简要过程:通过eclipse自带的expory导出jar文件或者适用工具Fat jar到处..(工具更方便些)

然后用exe4j 生成.exe执行文件,这里需要注意的是:(前辈的总结,..)

exe4j是不可以将jre直接打包进exe中去的。但exe4j提供了一个搜索jre路径,这个路径可以是系统的环境变量,也可以是注册表,也可以是具体的某路径。
由于要实现在没有jvm的电脑上运行,搜索环境变量与注册表自然是无用功的了,所以只能搜某路径的。把jre的路径,加进去,就行了。但要注意一点:不然用绝对路径哦。我之前就是用绝对路径,导致无效的,而走了弯路的。比如可以用:./jre

这样把jre环境复制一份到jar同一个目录下. 用 ./ 表示当前目录


打成安装包时,要将生成的exe文件与jre一起打包。为了确认打完包,可以用,在没打包之前,点该exe文件,要确认能运行,不然打包要白忙了。


打包工具用 Inno Setup .. 可能还有其他工具,但我只试了这个..很方便.


详细步骤:


导出jar

一、导出jar文件:

如果你的程序开发已经接近尾声了,正准备将其生成桌面可执行文件却不知道该如何操作时,你只要按照以下步骤实行即可:选中你的项目---然后右击弹出选项列表---选择Export---Java---jar file---next---browser(选择jar的存放路径)---next---next(不管太多继续下一步)---browser(选择你程序的主类--OK)---finish;查看你存放jar的目录,是否已经生成了?如果没有你一定没按照步骤操作.

二、运行jar文件:

生成的jar文件你可以双击运行它,但默认情况下会以压缩包的方式打开,如果这不是你想要的结果,请右击文件---打开方式---选择java(TM)platform来运行.我想这时你应该看到了你的软件界面.先恭喜你了..

三、连同第三方文件一并导出至jar文件:

如果你在程序开发的时候用到了第三方提供的API或者其它附属资源,在你导出生成jar文件时你必须将第三方的文件一并导出,不然你的程序运行将达不到你想要的结果.

你可以利用Fat jar插件生成jar,Fat jar 的下载地址:http://sourceforge.net/projects/fjep/ 下载的文件是net.sf.fjep.fatjar_0.0.31.zip,解压缩后你会看到plugins文件夹中的net.sf.fjep.fatjar_0.0.31.jar文件(简称Fat jar).

插件安装:把Fat jar文件拷贝到你的eclipse目录下的plugins中..重新启动你的eclipse平台,然后查看Window---preferences,弹出的窗口中有Fat jar preferences这一项则说明你已经安装成功了,没有这一项则未安装成功,需要多做如下几步操作:到你的eclipse目录下的configuration---org.eclipse.update,并删除platform.xml文件,然后重新启动eclipse.查看Window---preferences,你会发现Fat jar preferences这一项.恭喜你安装成功了.(未成功可直接去墙撞死)..

用Fat jar打包:右击你的项目,选项列表中有Build Fat jar这一项,选择它,弹出的窗口你会觉得特别亲切,一目了然使用很方便,点击Browser(Main-Class选择你的主类)---next---finish(勾选你需要一起打包的资源,默认为全部勾选).瞧,jar文件就生成在你的工程项目中了.运行它,一切顺利,否则就是你编程的问题了..

另:如果你不想用Fat jar来打包,有一个更快捷的方法可以帮助你,但也是有缺陷的.

步骤:将你需要用到的附属文件放到你的jre安装目录下的/lib/ext目录中(安装JDK的同时也安装了jre),再通过(一、导出jar文件)的方式生成jar.这样就可以了.它有个致命的缺点就是,这样的jar文件只能在你本机可以使用,如果要在别的机子上运行必需在/lib/ext放置同样的附属资源.[另的方法我没有实验]


生成.exe(exe4j软件)


exe4j。

1.运行后直接点Next.

2.选择第二项“JAR in EXE mode”,之后点击Next.

3.在General---Short name of your application框中输入你的应用程序的名字(随便即可),在Directories---Output directory框中输入生成exe后的保存路径,然后Next.

4.在Excutable name框中输入将要生成的exe的名字,“Icon File”是应用程序显示的小图标,不选也行.继续Next.

5.必须先配置Class-Path,点击+号添加文件,即jar文件,如果有用到其它资源需要把资源一起添加进去.

6.General --- Main Class选择主类.

7.Java Version --- Minimum version(低版本必须填写),Maximum version(高版本可以为空),如果都有填写那么高版本的值必须大于低版本的值.

在这个时候需要添加jre路径.如果想要软件在没有jre环境的机器上运行,先把jre环境复制到和jar包一个目录下.

把我们自带的jre指定为首选的运行环境,如图选择:
Search sequence


默认情况下程序会去系统的注册表,环境变量及相关的目录查找jre,因为我们只需要自带的jre,所以这里我把默认的选项移除了,然后添加我们自己的jre所在的目录路径,点+号开始:


这里还是建议使用相对的目录路径: jar包就在myloading路径下


指定jre所在的目录,如图:


最后我的jre目录路径就是这样的:.\jre1.6



8.勾选Allow JREs with a beta version number以及Only allow JDKs and no JREs.接着Next.到这一步你可以直接finish.这样就把jar转换成exe文件了.你可以点击Click Here to Start the Application按钮测试生成的文件.


这样就生成了.exe文件. 



然后使用Inno Setup       生成安装包setup.exe

下载地址:http://download.youkuaiyun.com/detail/mamba10/4587518


解压..运行Compil32.exe



Inno Setup的下载安装请到网上搜索,安装后启动脚本向导,如图所示:


整个过程很简单:



这里指定你的程序的启动文件,即刚才用exe4j创建的exe文件,并将你的整个程序目录都包含进去就可以了,接下来的基本上一直按下一步就可以了,直到脚本向导完成,


这里很重要,不要忘记 "添加文件夹"  !MyLoading 文件夹里面包含了程序的所有的东西





然后执行该脚本即可:





然后找到Inno Setup的默认输出目录,就可以看到编译输出后的安装包了,如下图:MyLoading安装.exe


利用这个就可以快速轻松的安装你的Java桌面应用了。

[root@yfw ~]# cd /etc/systemd/system [root@yfw system]# sudo rm /opt/openfire/lib/mysql-connector-j-8.2.0.jar rm: cannot remove '/opt/openfire/lib/mysql-connector-j-8.2.0.jar': No such file or directory [root@yfw system]# ls -l /opt/openfire/conf/database.properties -rw-r--r-- 1 openfire openfire 405 Nov 15 12:41 /opt/openfire/conf/database.properties [root@yfw system]# pkill -f openfire [root@yfw system]# [root@yfw system]# sudo rm -rf /opt/openfire/work/* [root@yfw system]# sudo rm -rf /opt/openfire/temp/* [root@yfw system]# cat /opt/openfire/conf/openfire.xml <?xml version="1.0" encoding="UTF-8"?> <jive> <adminConsole> <port>9090</port> <securePort>9091</securePort> </adminConsole> <locale>en</locale> <setup>true</setup> </jive> [root@yfw system]# /opt/openfire/bin/start.sh Using Java: /usr/lib/jvm/java-17-openjdk/bin/java Java Version: openjdk version "17.0.1" 2021-10-19 LTS OpenJDK Runtime Environment 21.9 (build 17.0.1+12-LTS) OpenJDK 64-Bit Server VM 21.9 (build 17.0.1+12-LTS, mixed mode, sharing) Classpath: /opt/openfire/lib/activation-1.1.jar:/opt/openfire/lib/apache-el-9.0.107.jar:/opt/openfire/lib/apache-jsp-9.0.107.jar:/opt/openfire/lib/asm-9.8.jar:/opt/openfire/lib/asm-commons-9.8.jar:/opt/openfire/lib/asm-tree-9.8.jar:/opt/openfire/lib/bcpg-jdk18on-1.78.1.jar:/opt/openfire/lib/bcpkix-jdk18on-1.78.1.jar:/opt/openfire/lib/bcprov-jdk18on-1.78.1.jar:/opt/openfire/lib/bcutil-jdk18on-1.78.1.jar:/opt/openfire/lib/caffeine-3.2.0.jar:/opt/openfire/lib/checker-qual-3.33.0.jar:/opt/openfire/lib/common-image-3.9.4.jar:/opt/openfire/lib/common-io-3.9.4.jar:/opt/openfire/lib/common-lang-3.9.4.jar:/opt/openfire/lib/commons-codec-1.15.jar:/opt/openfire/lib/commons-dbcp2-2.9.0.jar:/opt/openfire/lib/commons-ip-math-1.32.jar:/opt/openfire/lib/commons-lang3-3.18.0.jar:/opt/openfire/lib/commons-logging-1.2.jar:/opt/openfire/lib/commons-pool2-2.9.0.jar:/opt/openfire/lib/commons-text-1.10.0.jar:/opt/openfire/lib/dom4j-2.1.4.jar:/opt/openfire/lib/dwr-3.0.2-RELEASE.jar:/opt/openfire/lib/ecj-3.33.0.jar:/opt/openfire/lib/error_prone_annotations-2.18.0.jar:/opt/openfire/lib/failureaccess-1.0.1.jar:/opt/openfire/lib/guava-32.0.1-jre.jar:/opt/openfire/lib/hsqldb-2.7.1.jar:/opt/openfire/lib/httpclient-4.5.13.jar:/opt/openfire/lib/httpcore-4.4.13.jar:/opt/openfire/lib/i18n-5.0.2.jar:/opt/openfire/lib/imageio-bmp-3.9.4.jar:/opt/openfire/lib/imageio-core-3.9.4.jar:/opt/openfire/lib/istack-commons-runtime-3.0.11.jar:/opt/openfire/lib/j2objc-annotations-2.8.jar:/opt/openfire/lib/jakarta.activation-1.2.2.jar:/opt/openfire/lib/jakarta.annotation-api-1.3.5.jar:/opt/openfire/lib/jakarta.transaction-api-1.3.3.jar:/opt/openfire/lib/jakarta.xml.bind-api-2.3.3.jar:/opt/openfire/lib/jansi-1.18.jar:/opt/openfire/lib/javax.activation-api-1.2.0.jar:/opt/openfire/lib/javax.mail-1.6.2.jar:/opt/openfire/lib/jaxb-api-2.3.1.jar:/opt/openfire/lib/jaxb-runtime-2.3.3.jar:/opt/openfire/lib/jaxen-1.2.0.jar:/opt/openfire/lib/jcip-annotations-1.0.jar:/opt/openfire/lib/jcl-over-slf4j-2.0.9.jar:/opt/openfire/lib/jetty-ee-12.0.24.jar:/opt/openfire/lib/jetty-ee8-annotations-12.0.24.jar:/opt/openfire/lib/jetty-ee8-apache-jsp-12.0.24.jar:/opt/openfire/lib/jetty-ee8-nested-12.0.24.jar:/opt/openfire/lib/jetty-ee8-plus-12.0.24.jar:/opt/openfire/lib/jetty-ee8-security-12.0.24.jar:/opt/openfire/lib/jetty-ee8-servlet-12.0.24.jar:/opt/openfire/lib/jetty-ee8-webapp-12.0.24.jar:/opt/openfire/lib/jetty-ee8-websocket-jetty-api-12.0.24.jar:/opt/openfire/lib/jetty-ee8-websocket-jetty-common-12.0.24.jar:/opt/openfire/lib/jetty-ee8-websocket-jetty-server-12.0.24.jar:/opt/openfire/lib/jetty-ee8-websocket-servlet-12.0.24.jar:/opt/openfire/lib/jetty-http-12.0.24.jar:/opt/openfire/lib/jetty-io-12.0.24.jar:/opt/openfire/lib/jetty-jmx-12.0.24.jar:/opt/openfire/lib/jetty-jndi-12.0.24.jar:/opt/openfire/lib/jetty-plus-12.0.24.jar:/opt/openfire/lib/jetty-security-12.0.24.jar:/opt/openfire/lib/jetty-server-12.0.24.jar:/opt/openfire/lib/jetty-servlet-api-4.0.6.jar:/opt/openfire/lib/jetty-session-12.0.24.jar:/opt/openfire/lib/jetty-util-12.0.24.jar:/opt/openfire/lib/jetty-websocket-core-common-12.0.24.jar:/opt/openfire/lib/jetty-websocket-core-server-12.0.24.jar:/opt/openfire/lib/jetty-xml-12.0.24.jar:/opt/openfire/lib/jmdns-1.0.jar:/opt/openfire/lib/jsmpp-2.3.10.jar:/opt/openfire/lib/json-20231013.jar:/opt/openfire/lib/jspecify-1.0.0.jar:/opt/openfire/lib/jsr305-3.0.2.jar:/opt/openfire/lib/jtds-1.3.1.jar:/opt/openfire/lib/jzlib-1.1.3.jar:/opt/openfire/lib/libidn-1.35.jar:/opt/openfire/lib/listenablefuture-9999.0-empty-to-avoid-conflict-with-guava.jar:/opt/openfire/lib/log4j-api-2.20.0.jar:/opt/openfire/lib/log4j-core-2.20.0.jar:/opt/openfire/lib/log4j-slf4j2-impl-2.20.0.jar:/opt/openfire/lib/mssql-jdbc-9.4.1.jre11.jar:/opt/openfire/lib/mysql-connector-java-5.1.49.jar:/opt/openfire/lib/netty-all-4.1.118.Final.jar:/opt/openfire/lib/netty-buffer-4.1.118.Final.jar:/opt/openfire/lib/netty-codec-4.1.118.Final.jar:/opt/openfire/lib/netty-codec-dns-4.1.118.Final.jar:/opt/openfire/lib/netty-codec-haproxy-4.1.118.Final.jar:/opt/openfire/lib/netty-codec-http2-4.1.118.Final.jar:/opt/openfire/lib/netty-codec-http-4.1.118.Final.jar:/opt/openfire/lib/netty-codec-memcache-4.1.118.Final.jar:/opt/openfire/lib/netty-codec-mqtt-4.1.118.Final.jar:/opt/openfire/lib/netty-codec-redis-4.1.118.Final.jar:/opt/openfire/lib/netty-codec-smtp-4.1.118.Final.jar:/opt/openfire/lib/netty-codec-socks-4.1.118.Final.jar:/opt/openfire/lib/netty-codec-stomp-4.1.118.Final.jar:/opt/openfire/lib/netty-codec-xml-4.1.118.Final.jar:/opt/openfire/lib/netty-common-4.1.118.Final.jar:/opt/openfire/lib/netty-handler-4.1.118.Final.jar:/opt/openfire/lib/netty-handler-proxy-4.1.118.Final.jar:/opt/openfire/lib/netty-handler-ssl-ocsp-4.1.118.Final.jar:/opt/openfire/lib/netty-resolver-4.1.118.Final.jar:/opt/openfire/lib/netty-resolver-dns-4.1.118.Final.jar:/opt/openfire/lib/netty-resolver-dns-classes-macos-4.1.118.Final.jar:/opt/openfire/lib/netty-resolver-dns-native-macos-4.1.118.Final-osx-aarch_64.jar:/opt/openfire/lib/netty-resolver-dns-native-macos-4.1.118.Final-osx-x86_64.jar:/opt/openfire/lib/netty-transport-4.1.118.Final.jar:/opt/openfire/lib/netty-transport-classes-epoll-4.1.118.Final.jar:/opt/openfire/lib/netty-transport-classes-kqueue-4.1.118.Final.jar:/opt/openfire/lib/netty-transport-native-epoll-4.1.118.Final-linux-aarch_64.jar:/opt/openfire/lib/netty-transport-native-epoll-4.1.118.Final-linux-riscv64.jar:/opt/openfire/lib/netty-transport-native-epoll-4.1.118.Final-linux-x86_64.jar:/opt/openfire/lib/netty-transport-native-kqueue-4.1.118.Final-osx-aarch_64.jar:/opt/openfire/lib/netty-transport-native-kqueue-4.1.118.Final-osx-x86_64.jar:/opt/openfire/lib/netty-transport-native-unix-common-4.1.118.Final.jar:/opt/openfire/lib/netty-transport-rxtx-4.1.118.Final.jar:/opt/openfire/lib/netty-transport-sctp-4.1.118.Final.jar:/opt/openfire/lib/netty-transport-udt-4.1.118.Final.jar:/opt/openfire/lib/ojdbc11-23.7.0.25.01.jar:/opt/openfire/lib/ons-23.7.0.25.01.jar:/opt/openfire/lib/oraclepki-23.7.0.25.01.jar:/opt/openfire/lib/orai18n-23.7.0.25.01.jar:/opt/openfire/lib/postgresql-42.7.7.jar:/opt/openfire/lib/rsi-23.7.0.25.01.jar:/opt/openfire/lib/shaj-0.5.jar:/opt/openfire/lib/simplefan-23.7.0.25.01.jar:/opt/openfire/lib/sitemesh-2.5.0.jar:/opt/openfire/lib/slf4j-api-2.0.9.jar:/opt/openfire/lib/startup.jar:/opt/openfire/lib/taglibs-standard-impl-1.2.5.jar:/opt/openfire/lib/taglibs-standard-spec-1.2.5.jar:/opt/openfire/lib/tinder-2.1.0.jar:/opt/openfire/lib/txw2-2.3.3.jar:/opt/openfire/lib/ucp-23.7.0.25.01.jar:/opt/openfire/lib/xdb-23.7.0.25.01.jar:/opt/openfire/lib/xmppserver-5.0.2.jar:/opt/openfire/lib/xpp3-1.1.4c.0.jar Starting OpenFire... OpenFire started (PID 1129553) Check logs: tail -f /opt/openfire/logs/nohup.out [root@yfw system]# [root@yfw system]# # 实时观察日志 [root@yfw system]# tail -f /opt/openfire/logs/nohup.out DEBUG: User = openfire DEBUG: Current dir = /opt/openfire DEBUG: OPENFIRE_HOME = /opt/openfire DEBUG: Java executable = /usr/lib/jvm/java-17-openjdk/bin/java openjdk version "17.0.1" 2021-10-19 LTS OpenJDK Runtime Environment 21.9 (build 17.0.1+12-LTS) OpenJDK 64-Bit Server VM 21.9 (build 17.0.1+12-LTS, mixed mode, sharing) DEBUG: Launching OpenFire... Database setup or configuration error: Please verify your database settings and check the logs/openfire.log file for detailed error messages. java.lang.IllegalArgumentException: java.sql.SQLException: ConnectionManager.getConnection() failed to obtain a connection after 11 attempts. The exception from the last attempt is as follows: java.sql.SQLException: Check JDBC properties; data source was not be initialised at org.jivesoftware.openfire.XMPPServer.verifyDataSource(XMPPServer.java:1020) at org.jivesoftware.openfire.XMPPServer.start(XMPPServer.java:666) at org.jivesoftware.openfire.XMPPServer.<init>(XMPPServer.java:221) at java.base/jdk.internal.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method) at java.base/jdk.internal.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:77) at java.base/jdk.internal.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45) at java.base/java.lang.reflect.Constructor.newInstanceWithCaller(Constructor.java:499) at java.base/java.lang.reflect.ReflectAccess.newInstance(ReflectAccess.java:128) at java.base/jdk.internal.reflect.ReflectionFactory.newInstance(ReflectionFactory.java:347) at java.base/java.lang.Class.newInstance(Class.java:645) at org.jivesoftware.openfire.starter.ServerStarter.start(ServerStarter.java:92) at org.jivesoftware.openfire.starter.ServerStarter.main(ServerStarter.java:56) Caused by: java.sql.SQLException: ConnectionManager.getConnection() failed to obtain a connection after 11 attempts. The exception from the last attempt is as follows: java.sql.SQLException: Check JDBC properties; data source was not be initialised at org.jivesoftware.database.DbConnectionManager.getConnection(DbConnectionManager.java:204) at org.jivesoftware.openfire.XMPPServer.verifyDataSource(XMPPServer.java:1010) ... 11 more Error starting the server. Please check the log files for more information.
最新发布
11-16
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值