在后端Java开发中,难免会遇到自己写的程序要进行独立打包部署,在一些小公司经常会用到下面的操作,赶快收藏起来吧。
1.注册windows服务
首先,将打包出来的jar包命名成你要注册的服务ID,同时编写相同名字的xml文件,如下图所示。
xml 文件内容如下所示:
<service>
<!-- ID of the service. It should be unique across the Windows system-->
<id>ZLPowerService</id>
<!-- Display name of the service -->
<name>智能充电柜服务端</name>
<!-- Service description -->
<description>智能充电柜服务端</description>
<!-- Path to the executable, which should be started -->
<executable>start.bat</executable>
</service>
注意xml 文件中 start.bat其实是在设置运行环境等
start.bat的内容如下,复制时请修改相应参数
set path=%cd%\jdk1.8.0_291\bin;%path%
java -jar %cd%\ZLPowerService.jar
pause
另外,就要说到上面图中的ZLPowerService.exe,这其实是将xml文件中的内容注册到win上用到的一个C程序,其中设置了日志路径输出位置,大家可以直接使用就行。注意命名要跟注册服务名一样。注册服务程序放在某盘上自取:链接:https://pan.baidu.com/s/1Hk_03Adg70YqDnm1guMy4w
提取码:cyps。
2.服务程序安装、启动、停止、卸载
上面都是用的批处理文件程序,具体直接看内容:
a.安装程序
%1 mshta vbscript:CreateObject("Shell.Application").ShellExecute("cmd.exe","/c %~s0 ::","","runas",1)(window.close)&&exit
cd /d "%~dp0"
ZLPowerService.exe install
pause
b.启动服务
del /a /f /s ZLPowerService.out.log
del /a /f /s ZLPowerService.wrapper.log
del /a /f /s ZLPowerService.err.log
%1 mshta vbscript:CreateObject("Shell.Application").ShellExecute("cmd.exe","/c %~s0 ::","","runas",1)(window.close)&&exit
cd /d "%~dp0"
net start ZLPowerService
pause
c.停止服务
%1 mshta vbscript:CreateObject("Shell.Application").ShellExecute("cmd.exe","/c %~s0 ::","","runas",1)(window.close)&&exit
cd /d "%~dp0"
net stop ZLPowerService
pause
d.删除服务
%1 mshta vbscript:CreateObject("Shell.Application").ShellExecute("cmd.exe","/c %~s0 ::","","runas",1)(window.close)&&exit
cd /d "%~dp0"
sc delete ZLPowerService
pause
e.卸载服务
%1 mshta vbscript:CreateObject("Shell.Application").ShellExecute("cmd.exe","/c %~s0 ::","","runas",1)(window.close)&&exit
cd /d "%~dp0"
ZLPowerService.exe uninstall
pause
好了,大家觉得可以就点点赞吧。