Windows将jar包注册成服务

1、安装.net framework 4

链接:https://www.microsoft.com/zh-cn/download/details.aspx?id=17718

 

下载对应的文件,双击安装即可

 

2、下载WINSW

链接:https://github.com/kohsuke/winsw/releases

 

下载对应的WinSW.NET4.exe文件即可

 

3、jar包重命名

去掉除后缀名需要的点

 

举例:

原jar包名为:xxx-xxx.1.0.0.jar

需改名为:xxx-xxx.jar 或者 其它你喜欢的名称

 

4、WinSW.NET4.exe文件重命名

将该文件重命名为第二步所取的名称即可

 

改名后的jar包为:xxx-xxx.jar,那么WinSW.NET4.exe就要重命名为xxx-xxx.exe

 

5、新建xml文件

Xml文件取名为xxx-xxx.xml

 

写入以下配置内容:

<service>

     <id>xxx-xxx</id>

     <name>服务名称</name>

     <description>服务描述</description>

 

     <!-- 开机启动 -->

     <startmode>Automatic</startmode>

 

     <!-- 日志配置 -->

     <logpath>%BASE%\log</logpath>

     <logmode>rotate</logmode>



     <!-- 日志配置 -->

     <executable>start.bat</executable>

     <service>rotate</service>

 </service>

 

 6、新建Xml关联启动脚本

start.bat

内容如下:

set path=%JAVA_HOME%;%path%

java -Dfile.encoding=utf-8 -jar %cd%\xxx-xxx.jar

pause

 

7、新建注册、启动、停止、卸载服务脚本

installService.bat

内容如下:

%1 mshta vbscript:CreateObject("Shell.Application").ShellExecute("cmd.exe","/c %~s0 ::","","runas",1)(window.close)&&exit

cd /d "%~dp0"

futureBridge-modules-protocol.exe install

pause

 

startService.bat

内容如下:

%1 mshta vbscript:CreateObject("Shell.Application").ShellExecute("cmd.exe","/c %~s0 ::","","runas",1)(window.close)&&exit

cd /d "%~dp0"

net start futureBridge-modules-protocol

pause

 

stopService.bat

内容如下:

%1 mshta vbscript:CreateObject("Shell.Application").ShellExecute("cmd.exe","/c %~s0 ::","","runas",1)(window.close)&&exit

cd /d "%~dp0"

net stop futureBridge-modules-protocol

pause

 

uninstallService.bat

内容如下:

%1 mshta vbscript:CreateObject("Shell.Application").ShellExecute("cmd.exe","/c %~s0 ::","","runas",1)(window.close)&&exit

cd /d "%~dp0"

futureBridge-modules-protocol.exe uninstall

pause

 

8、注册、启动服务

先双击执行installService.bat脚本

 

再双击执行startService.bat脚本

 

9、停止、卸载服务

先双击执行stopService.bat脚本

 

再双击执行uninstallService.bat脚本

posted @ 2022-12-19 17:05  tewu_spy  阅读(2269)  评论(0)    收藏  举报