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

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

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

  • 5
    点赞
  • 23
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值