如何将jar包程序注册成Windows服务并运行起来,简单实用

阿里云国内75折 回扣 微信号:monov8
阿里云国际,腾讯云国际,低至75折。AWS 93折 免费开户实名账号 代冲值 优惠多多 微信号:monov8 飞机:@monov6

在后端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

好了大家觉得可以就点点赞吧。

阿里云国内75折 回扣 微信号:monov8
阿里云国际,腾讯云国际,低至75折。AWS 93折 免费开户实名账号 代冲值 优惠多多 微信号:monov8 飞机:@monov6
标签: windows