把jar包做成windows后台服务,这样就可以开机自启动了。
1、下载Winsw.exe
Winsw.exe文件下载路径:https://github.com/winsw/winsw/releases
2、编写配置文件sample.xml
<service>
<id>sample</id>
<name>sample</name>
<description>This service runs sample system.</description>
<executable>java</executable>
<arguments>-Xrs -Xmx256m -jar "%BASE%\sample.jar" </arguments>
<log mode="roll" />
<onfailure action="restart" />
</service>
3、修改为同样的名称
把sample.jar、sample.xml、winsw.exe 放到同一目录,并且修改为和jar包同样的名称
修改前
修改后:
4、安装sample服务
打开CMD命令行,并切换到jar所在目录,运行 sample install 命令进行服务安装
此时就可以在后台中查看windows服务了。但是此时程序还是未启动状态
5、启动服务
1)界面启动:在sample服务上面右键,选择启动。或者双击sample服务,打开属性窗口,点击启动。
2)命令行启动
切换到jar包所在目录运行 sample start
6、其他一些命令行操作指令
install install the service to Windows Service Controller
uninstall uninstall the service
start start the service (must be installed before)
stop stop the service
stopwait stop the service and wait until it's actually stopped
restart restart the service
restart! self-restart (can be called from child processes)
status check the current status of the service
test check if the service can be started and then stopped
testwait starts the service and waits until a key is pressed then stops the service
version print the version info
help print the help info (aliases: -h,--help,-?,/?)