使用批处理安装jar服务
@echo off
rem 请修改下面的服务名后保存,并以管理员身份再次运行
set serviceName=testjar
echo 请输入 1 安装 2 启动服务 5 停止服务 6 卸载 88 退出
set /p input=
if '%input%'=='1' (
goto install
) else (
if '%input%'=='2' (
sc start %serviceName%
sc failure %serviceName% reset= 30 actions= restart/5000
) else (
if '%input%'=='5' (
sc stop %serviceName%
) else (
if '%input%'=='6' (
sc delete %serviceName%
)
)
)
)
)
pause
exit
:install
set cp=%~dp0
cd /d %~dp0
set jarfile=
FOR %%f in (*.jar) DO set jarfile=%~dp0%%f
if '%jarfile%'=='' (
echo 未发现jar文件
pause
exit
)
echo 发现 %jarfile% ,将为其安装服务 %serviceName%
pause
@echo off & setlocal enabledelayedexpansion
set text1=tpl.xml
set text2=%serviceName%.tmp
set text3=%serviceName%.xml
set var1=servicename
set var2=%serviceName%
rem set var2=%cp:\=/%
echo %var1%
echo %var2%
del %text2% -y
del %text3% -y
chcp 65001
for /f "tokens=*" %%i in (%text1%) do (
if "%%i"=="" (echo.) else (set "line=%%i" & call :chg)
)>>%text2%
set var1=test.jar
set var2=%jarfile:\=/%
for /f "tokens=*" %%i in (%text2%) do (
if "%%i"=="" (echo.) else (set "line=%%i" & call :chg)
)>>%text3%
del %text2% -y
chcp 936
echo 安装服务
copy /y tpl.exe %serviceName%.exe
cmd /c %serviceName%.exe install
rem net start %serviceName%
echo 添加守护任务计划
schtasks /delete /f /tn "%serviceName%dog"
schtasks /create /f /tn "%serviceName%dog" /tr "cmd /c sc start %serviceName%" /sc MINUTE /MO 15
pause
exit
:chg
set "line=!line:%var1%=%var2%!"
echo !line!
goto :eof
@@#
Tpl.xml内容
<configuration>
<id>servicename</id>
<name>servicename</name>
<description>servicename</description>
<env name="JAVA_HOME" value="%JAVA_HOME%"/>
<executable>java</executable>
<arguments>-jar "test.jar"</arguments>
<startmode>Automatic</startmode>
<logpath>%BASE%\log</logpath>
<logmode>rotate</logmode>
</configuration>
@@#