利用Inno Setup将java程序、mysql、jre、emqx打包为Windows的可执行文件
利用Inno Setup将java程序、mysql、jre、emqx打包为Windows的可执行文件
一.软件准备
- Inno Setup
- exe4j
- mysql-5.7.42-winx64
- EMQX
- jre1.8.0_101 (暂时没下载jre,我是用的jdk安装目录下的C:\Program Files\Java\jre1.8.0_101,直接复制粘贴即可)
- VC_redist.x64.exe (mysql所需的运行库,没有它可能安装不了mysql)
二. 新建文件夹
在D盘根目录新建Biometrics文件夹,把相应的mysql-5.7.42-winx64、emqx、jre1.8.0_101放到该文件夹下
1. 完备mysql
- 在mysql-5.7.42-winx64目录下新建sql、data文件夹,新建my.ini文件和writeMyIni.bat文件,
- my.ini内容
[mysql]
#设置mysql客户端默认字符集
default-character-set=utf8mb4
[mysqld]
#设置mysql的安装目录
basedir=D:\\Biometrics\\mysql-5.7.42-winx64
#设置mysql数据库的数据存储目录,必须是data,或者是\\xxx\\data
datadir=D:\\Biometrics\\mysql-5.7.42-winx64\\data
#服务端使用的字符集默认为8比特编码的latin1字符集
character-set-server=utf8mb4
#设置端口
port = 3306
#允许最大连接数
max_connections=200
#创建新表时将使用的默认存储引擎
default-storage-engine=INNODB
#服务唯一标识
server_id = 1
#是否启用慢查询
slow_query_log = 1
#指定慢查询日志文件的路径和名字
slow_query_log_file = slow.log
#SQL语句运行时间超过该值才会被记录
long_query_time = 10
#开启binlog日志
log-bin = mysql-bin
# 关闭ONLY_FULL_GROUP_BY
sql-mode=STRICT_TRANS_TABLES,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,NO_ENGINE_SUBSTITUTION
[client]
port=3306
default-character-set=utf8mb4
为了实现basedir和datadir的值跟随用户安装时的盘符,我们用批处理写入
- writeMyIni.bat
@echo off
rem **********************************MySQL环境变量部署*****************************************
cd /d %~dp0
del /F %cd%\my.ini
echo 删除完成
echo [client]>> my.ini
echo port = 3306>> my.ini
echo default-character-set=utf8>>my.ini
echo [mysqld]>> my.ini
echo collation-server=utf8_general_ci>>my.ini
echo log-error=%cd:\=\\%\\error>>my.ini
echo character_set_server=utf8>>my.ini
echo default_storage_engine=INNODB>> my.ini
echo basedir=%cd:\=\\%\\>>my.ini
echo datadir=%cd:\=\\%\\data>> my.ini
echo server_id = 1 >> my.ini
echo slow_query_log = 1 >> my.ini
echo slow_query_log_file = slow.log>> my.ini
echo long_query_time = 10>> my.ini
echo log-bin = mysql-bin>> my.ini
echo transaction-isolation=READ-COMMITTED>> my.ini
echo port=3306>> my.ini
echo max_allowed_packet=64M>> my.ini
echo tmp_table_size=64M>> my.ini
echo sql-mode=STRICT_TRANS_TABLES,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,NO_ENGINE_SUBSTITUTION>> my.ini
echo my.ini生成成功
rem pause
- 把需要的sql脚本放入sql文件夹
- resetPwd.sql
use mysql;
select 'host' from user where user='root';
update user set host = '%' where user ='root';
flush privileges;
select 'host' from user where user='root';
ALTER USER 'root'@'%' IDENTIFIED BY '123456' PASSWORD EXPIRE NEVER;
ALTER USER 'root'@'%' IDENTIFIED WITH mysql_native_password BY '123456';
FLUSH PRIVILEGES;
- createTable.sql
CREATE DATABASE ceco_ry;
- ceco_ry为你的业务sql
- 在D:\Biometrics\mysql-5.7.42-winx64\bin目录新建init-mysql.bat
@echo off
cd /d %~d0%~p0
cd ..
set inipath=%cd%\my.ini
cd bin
"%cd%\mysqld.exe" -install mysql --defaults-file="%inipath%"
"%cd%\mysqld.exe" --initialize-insecure --user=mysql --console
net start mysql
sc config mysql start=auto
net stop mysql
net start mysql
echo 安装完毕
"%cd%\mysqladmin" -u root password 123456
echo 修改密码完毕
cd ..
"%cd%\bin\mysql.exe" -uroot -p123456 < "%cd%\sql\resetPwd.sql"
"%cd%\bin\mysql.exe" -uroot -p123456< "%cd%\sql\createTable.sql"
"%cd%\bin\mysql.exe" -uroot -p123456 ceco_ry< "%cd%\sql\ceco_ry.sql"
echo 创建数据库完成,建表完毕
pause
2.完备emqx
在D:\Biometrics\emqx\bin目录新建install.bat和mqttSatrt.bat。
- install.bat
@echo off
cd /d %~dp0
echo install EMQX
emqx.cmd install
sc config emqx start=auto
pause
- mqttStart.bat
@echo off
cd /d %~dp0
echo install start
net stop emqx
net start emqx
pause
3.准备VC_redist.x64.exe
-
在安装mysql时windows可能会缺少相应的库文件,有的电脑安装过mysql就不会出现此问题,安装
Microsoft Visual C++ 2015-2022 Redistributable (x64)
,可解决安装过程缺少vcruntime140_1.dll的错误, 链接地址,下载64位的。 -
把下载的
VC_redist.x64.exe
放到D:\Biometrics目录,并新建installVC_redist.x64.bat -
installVC_redist.x64.bat
@echo off
title 安装 VC++ 运行库
set installer=%~dp0%VC_redist.x64.exe
if not exist %installer% (
echo 安装程序不存在,请检查路径是否正确!
pause
) else (
echo 开始安装 VC++ 运行库,请稍候...
start /wait %installer% /norestart
if %errorlevel% equ 0 (
echo 安装完成,按任意键退出...
) else (
echo 安装失败,按任意键退出...
)
pause>nul
)
4.准备安装界面的提示文本
- installBefore.txt
1.默认安装目录为: D:\Biometrics,建议不要修改。
2.安装前请确保该磁盘有足够多的空间。
3.安装之前请关闭360安全卫士、金山毒霸、瑞星杀毒、百度杀毒等杀毒软件。
1. The default installation directory is: D:Biometrics, do not modify it.
2. Please do not install the software in the directory with blank space!
3. Please shut down 360 security guard, Jinshan drug bully, Ruixing antivirus, baidu antivirus, and other antivirus software before installing!
- installAfter.txt
1.安装完成。
2.已为你成功安装初始化MySQL、EMQX。
3.浏览器地址栏请输入:http://localhost:8088/ 即可访问
1.Install Successful
2.MySQL and EMQX have been successfully installed for you.
3.Please enter: http://localhost:8088/ in the browser address bar to access
5.准备java程序
利用exe4j把打包好的jar包打成可执行的运行程序,因为我的BiometricsServer_Windows_X64.exe
与jre1.8.0_101在同级目录下,一开始在打包时用的是绝对路径,后来发现踩了坑,要用相对路径,后来改成./Biometrics/jre1.8.0_101
就可以了,只要jre跟.exe在同一个目录下就可以成功启动,跟盘符无关,所以在exe4j中配置jre时要小心。
6.准备打包文件
- package.iss
; 脚本由 Inno Setup 脚本向导 生成!
; 有关创建 Inno Setup 脚本文件的详细资料请查阅帮助文档!
#define MyAppName "Biometrics"
#define MyAppVersion "1.0.0"
#define MyAppPublisher "深圳益光科技有限公司"
#define MyAppURL "http://www.example.com/"
#define MyAppExeName "BiometricsServer_Windows_X64.exe"
[Setup]
; 注: AppId的值为单独标识该应用程序。
; 不要为其他安装程序使用相同的AppId值。
; (若要生成新的 GUID,可在菜单中点击 "工具|生成 GUID"。)
AppId={{551E59C4-365A-449B-A93F-C057F3B27CCC}
AppName={#MyAppName}
AppVersion={#MyAppVersion}
AppVerName={#MyAppName} {#MyAppVersion}
AppPublisher={#MyAppPublisher}
AppPublisherURL={#MyAppURL}
AppSupportURL={#MyAppURL}
AppUpdatesURL={#MyAppURL}
DefaultDirName=D:\{#MyAppName}
DisableProgramGroupPage=yes
InfoBeforeFile=D:\Biometrics\installBefore.txt
InfoAfterFile=D:\Biometrics\installAfter.txt
; 以下行取消注释,以在非管理安装模式下运行(仅为当前用户安装)。
PrivilegesRequired=admin
OutputDir=D:\Biometrics\Output
OutputBaseFilename=Biometrics
SetupIconFile=D:\Biometrics\logo.ico
Compression=lzma
SolidCompression=yes
WizardStyle=modern
[Languages]
Name: "chinesesimp"; MessagesFile: "compiler:Default.isl"
Name: "english"; MessagesFile: "compiler:Languages\English.isl"
[Tasks]
Name: "desktopicon"; Description: "{cm:CreateDesktopIcon}"; GroupDescription: "{cm:AdditionalIcons}"; Flags: checkablealone
[Files]
;Source: "D:\Biometrics\*"; DestDir: "{app}"; Flags: ignoreversion recursesubdirs createallsubdirs
Source: "D:\Biometrics\emqx\*"; DestDir: "{app}\emqx"; Flags: ignoreversion recursesubdirs createallsubdirs
Source: "D:\Biometrics\jre1.8.0_101\*"; DestDir: "{app}\jre1.8.0_101"; Flags: ignoreversion recursesubdirs createallsubdirs
Source: "D:\Biometrics\mysql-5.7.42-winx64\*"; DestDir: "{app}\mysql-5.7.42-winx64"; Flags: ignoreversion recursesubdirs createallsubdirs
Source: "D:\Biometrics\BiometricsServer_Windows_X64.exe"; DestDir: "{app}"; Flags: ignoreversion recursesubdirs createallsubdirs
Source: "D:\Biometrics\installBefore.txt"; DestDir: "{app}"; Flags: ignoreversion recursesubdirs createallsubdirs
Source: "D:\Biometrics\installAfter.txt"; DestDir: "{app}"; Flags: ignoreversion recursesubdirs createallsubdirs
Source: "D:\Biometrics\VC_redist.x64.exe"; DestDir: "{app}"; Flags: ignoreversion recursesubdirs createallsubdirs
Source: "D:\Biometrics\uninstallMysql.bat"; DestDir: "{app}"; Flags: ignoreversion recursesubdirs createallsubdirs
Source: "D:\Biometrics\uninstallEmqx.bat"; DestDir: "{app}"; Flags: ignoreversion recursesubdirs createallsubdirs
Source: "D:\Biometrics\installVC_redist.x64.bat"; DestDir: "{app}"; Flags: ignoreversion recursesubdirs createallsubdirs
;Source: "D:\Biometrics\lib\vcruntime140_1.dll"; DestDir: "C:\Windows\System32"; Check: ShouldCopyVCRTDll
[Icons]
Name: "{userdesktop}\{#MyAppName}";Filename: "{app}\BiometricsServer_Windows_X64.exe"; WorkingDir: "{app}"
[Run]
Filename: "{app}\installVC_redist.x64.bat"; Flags: waituntilterminated
Filename: "{app}\mysql-5.7.42-winx64\writeMyIni.bat"; Flags: waituntilterminated
Filename: "{app}\mysql-5.7.42-winx64\bin\init-mysql.bat"; Flags: waituntilterminated
Filename: "{app}\emqx\bin\install.bat"; Flags: waituntilterminated
Filename: "{app}\emqx\bin\mqttStart.bat"; Flags: waituntilterminated
Filename: "{app}\{#MyAppExeName}"; Description: "{cm:LaunchProgram,{#StringChange(MyAppName, '&', '&&')}}"; Flags: nowait postinstall skipifsilent
[UninstallRun]
Filename: “{cmd}”; Parameters: “/C net stop mysql”; Flags: waituntilterminated runascurrentuser
Filename: “{cmd}”; Parameters: “/C sc delete mysql”; Flags: waituntilterminated runascurrentuser
Filename: “{cmd}”; Parameters: “/C reg delete HKEY_LOCAL_MACHINE\SYSTEM\ControlSet001\Services\mysql /f”; Flags: waituntilterminated runascurrentuser
Filename: “{cmd}”; Parameters: “/C reg delete HKEY_LOCAL_MACHINE\SYSTEM\ControlSet002\Services\mysql /f”; Flags: waituntilterminated runascurrentuser
Filename: “{cmd}”; Parameters: “/C reg delete HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services\EventLog\Application\MySQL /f”; Flags: waituntilterminated
Filename: “{cmd}”; Parameters: “/C sc stop emqx”; Flags: waituntilterminated runascurrentuser
Filename: “{cmd}”; Parameters: “/C sc delete emqx”; Flags: waituntilterminated runascurrentuser
Filename: “{cmd}”; Parameters: “/C reg delete HKEY_LOCAL_MACHINE\SOFTWARE\Ericsson\Erlang\ErlSrv\1.1\emqx_4.4.3 /f”; Flags: waituntilterminated runascurrentuser
Filename: “{cmd}”; Parameters: “/C reg delete HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services\emqx /f”; Flags: waituntilterminated runascurrentuser
Filename: “{cmd}”; Parameters: “/C schtasks /delete /TN ““MySQL”” /F”; Flags: waituntilterminated runascurrentuser
Filename: “{cmd}”; Parameters: “/C schtasks /delete /TN ““EMQX”” /F”; Flags: waituntilterminated runascurrentuser
;Filename: “{app}\uninstallMysql.bat”; Flags: waituntilterminated
;Filename: “{app}\uninstallEmqx.bat”; Flags: waituntilterminated
[Code]
function ShouldCopyVCRTDll: Boolean;
var
SourcePath: string;
begin
SourcePath := ExpandConstant('{tmp}\vcruntime140_1.dll');
Result := (not FileExists('C:\Windows\System32\vcruntime140_1.dll')) and FileExists(SourcePath);
if Result then
if not FileCopy(SourcePath, 'C:\Windows\System32\vcruntime140_1.dll', False) then
MsgBox('Failed to copy vcruntime140_1.dll to C:\Windows\System32', mbError, MB_OK);
end;
完成以上基本就可以满足安装了
在innosetup编译打包后会在Output目录下生成安装包
三.安装
- 安装直接双击打包的程序就行,在操作到下图时
- 如果安装过就点击修复
剩下的会自动安装,只需脚本完成后按任意键就行
这是安装后的目录
安装完成后默认会立即启动程序
并且默认会在桌面生成相应的快捷方式
关闭直接x掉就行了,下次再启动直接双击快捷方式就行了,很方便,并且mysql、emqx都是配置了开机自动启动的,不需要另外开启。
四.卸载
虽然innosetup生成了unins000.exe
卸载程序,但是发现卸不干净会有残留,
于是我添加了uninstallMysql.bat和uninstallEmqx.bat文件,先以管理员身份运行这两个文件,再双击unins000.exe就可以卸的很干净
- uninstallMysql.bat
@echo off
net stop MySQL
sc delete MySQL
RD /S /Q "%~dp0\\mysql-5.7.42-winx64"
::RD /S /Q "%~dp0\MySQL"
::RD /S /Q "C:\Program Files (x86)\MySQL"
::RD /S /Q "C:\ProgramData\MySQL"
REG DELETE HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services\EventLog\Application\MySQL /f
REG DELETE HKEY_LOCAL_MACHINE\SYSTEM\ControlSet001\Services\mysql /f
REG DELETE HKEY_LOCAL_MACHINE\SYSTEM\ControlSet002\Services\mysql /f
exit
- uninstallEmqx.bat
@echo off
net stop emqx
sc delete EMQX
RD /S /Q "%~dp0\emqx"
::RD /S /Q "C:\ProgramData\EMQ X"
::RD /S /Q "C:\Program Files (x86)\EMQ X"
::RD /S /Q "C:\ProgramData\EMQ X"
REG DELETE HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services\emqx /f
REG DELETE HKEY_LOCAL_MACHINE\SOFTWARE\Ericsson\Erlang\ErlSrv\1.1\emqx_4.4.3 /f
exit
五.完结
完成以上基本就可以了,每个人的电脑因人而异,不可能适配所有系统,因为准备的mysql、emqx都是Windows 64位的,所以不适配32位的操作系统。<\br>
我也是在网上看了很多博客,一个一个坑踩过来的
本文来自博客园,作者:cxf0616,转载请注明原文链接:https://www.cnblogs.com/cxfbk/p/17391152.html
风起于青萍之末,浪成于微澜之间
每一份成功都源于每日的坚持