为了让程序在后台运行并实现开机自启,最后还是决定使用Windows服务,并编写一个bat脚本放在注册表中开机start 服务来实现以上需求
- exe4j打包jar和打包类型Console application没有区别,除了选择类型时为Service
Inno SetUp脚本
- 实现思路: 用exe4j生成service,创建Windows服务,使用bat脚本,创建访问浏览器的快捷方式,使用bat脚本运行服务,并将它注册在注册表中实现开机运行。
; Script generated by the Inno Setup Script Wizard.
; SEE THE DOCUMENTATION FOR DETAILS ON CREATING INNO SETUP SCRIPT FILES!
#define MyAppName "程序名称"
#define MyAppVersion "版本"
#define MyAppPublisher "发布人信息"
#define MyAppURL "发布人信息网址"
#define MyAppExeName "程序名称.exe"
#define MyAppStartExeName "程序启动脚本.bat"
#define MyInitAppExeName "程序初始化脚本.bat"
#define MyAppAssocName MyAppName + " File"
#define MyAppAssocExt ".myp"
#define MyAppAssocKey StringChange(MyAppAssocName, " ", "") + MyAppAssocExt
[Setup]
; NOTE: The value of AppId uniquely identifies this application. Do not use the same AppId value in installers for other applications.
; (To generate a new GUID, click Tools | Generate GUID inside the IDE.)
AppId=自动生成
AppName={#MyAppName}
AppVersion={#MyAppVersion}
;AppVerName={#MyAppName} {#MyAppVersion}
AppPublisher={#MyAppPublisher}
AppPublisherURL={#MyAppURL}
AppSupportURL={#MyAppURL}
AppUpdatesURL={#MyAppURL}
DefaultDirName={autopf}\{#MyAppName}
ChangesAssociations=yes
DisableProgramGroupPage=yes
; Uncomment the following line to run in non administrative install mode (install for current user only.)
;PrivilegesRequired=lowest
OutputDir=输出路径
Compression=lzma
SolidCompression=yes
WizardStyle=modern
ShowLanguageDialog=yes
;Windows服务启动需要管理员权限,网上搜了一篇教程可以使InnoSetUp打包成需要管理员权限的程序,但是实际运行下来没实现想要的结果=参考网址
https://www.freesion.com/article/2004434312/。最后还是决定在启动服务的bat脚本中获取管理员权限。
PrivilegesRequired=admin
[Languages]
Name: "cs"; MessagesFile: "compiler:Languages\ChineseSimplified.isl"
Name: "english"; MessagesFile: "compiler:Default.isl"
[Components]
Name: main;Description:"主程序(必选)";Types:full compact custom;Flags:fixed
Name: data;Description:"数据文件";Types:full
Name: helpo;Description:"帮助文件";Types:full
[Tasks]
Name: "desktopicon"; Description: "{cm:CreateDesktopIcon}"; GroupDescription: "{cm:AdditionalIcons}"; Flags: unchecked
[Files]
;Source: "D:\workstation\exe4j打包目录\{#MyAppExeName}"; DestDir: "{app}"; Flags: ignoreversion;Components:main
Source: "D:\workstation\exe4j打包目录\{#MyInitAppExeName}"; DestDir: "{app}"; Flags: ignoreversion;Components:main
Source: "D:\workstation\exe4j打包目录\需要的文件.bat"; DestDir: "{app}"; Flags: ignoreversion;Components:main
Source: "D:\workstation\exe4j打包目录\需要的文件.exe"; DestDir: "{app}"; Flags: ignoreversion;Components:main
Source: "D:\workstation\exe4j打包目录\需要的文件.jar"; DestDir: "{app}"; Flags: ignoreversion;Components:main
Source: "D:\workstation\exe4j打包目录\jre\*"; DestDir: "{app}\jre"; Flags: ignoreversion recursesubdirs createallsubdirs; Components:main
Source: "D:\workstation\exe4j打包目录\readme.txt"; DestDir: "{app}"; Flags: isreadme
; NOTE: Don't use "Flags: ignoreversion" on any shared system files
[Registry]
;HKCR(HKEY_CLASSES_ROOT)
;HKCU(HKEY_CURRENT_USER)
;HKLM(HKEY_LOCAL_MACHINE)
;HKU(HKEY_USERS)
;HKCC(HKEY_CURRENT_CONFIG)
Root: HKA; Subkey: "Software\Classes\{#MyAppAssocExt}\OpenWithProgids"; ValueType: string; ValueName: "{#MyAppAssocKey}"; ValueData: ""; Flags: uninsdeletevalue
Root: HKA; Subkey: "Software\Classes\{#MyAppAssocKey}"; ValueType: string; ValueName: ""; ValueData: "{#MyAppAssocName}"; Flags: uninsdeletekey
Root: HKA; Subkey: "Software\Classes\{#MyAppAssocKey}\DefaultIcon"; ValueType: string; ValueName: ""; ValueData: "{app}\{#MyAppExeName},0"
Root: HKA; Subkey: "Software\Classes\{#MyAppAssocKey}\shell\open\command"; ValueType: string; ValueName: ""; ValueData: """{app}\{#MyAppExeName}"" ""%1"""
Root: HKA; Subkey: "Software\Classes\Applications\{#MyAppExeName}\SupportedTypes"; ValueType: string; ValueName: ".myp"; ValueData: ""
Root: HKLM; Subkey: "SOFTWARE\Microsoft\Windows\CurrentVersion\Run"; ValueType: string; ValueName: "注册表Key名称"; ValueData: "{app}\{#MyInitAppExeName}"
[Icons]
Name: "{autoprograms}\{#MyAppName}"; Filename: "{app}\{#MyAppStartExeName}"
Name: "{autodesktop}\{#MyAppName}"; Filename: "{app}\{#MyAppStartExeName}"; Tasks: desktopicon
[dirs]
Name:"{app}\帮助文件";Components:helpo
[Messages]
BeveledLabel=安装界面右下角名称
[Run]
Filename: {sys}\sc.exe; Parameters: "create 服务名 start= auto binPath= ""{app}\{#MyAppExeName}""" ; Flags: runhidden
Filename: "{app}\{#MyInitAppExeName}"; Description: "服务名描述"; Flags: postinstall
[UninstallRun]
Filename: {app}\{#MyAppExeName}; RunOnceId: "服务名"
Filename: {sys}\sc.exe; Parameters: "stop 服务名" ; Flags: runhidden; RunOnceId: "服务名"
Filename: {sys}\sc.exe; Parameters: "delete 服务名" ; Flags: runhidden; RunOnceId: "服务名"
- 快捷方式访问Web项目
{#MyAppStartExeName}
start 浏览器地址
- 运行创建服务 注意这里获取管理员权限
{#MyInitAppExeName}
@echo off
@%1 mshta vbscript:CreateObject("Shell.Application").ShellExecute("cmd.exe","/c %~s0 ::","","runas",1)(window.close)&&exit
@cd /d "%~dp0"
sc start "服务名"
if not errorlevel 1 goto :eof
pause