SpringBoot打成jar包,开机启动运行(后台服务)

一、背景
当使用SpringBoot打包以后,我需要使用cmd控制台命令 java -jar jar包路径+名称去部署启动,往往Jar包有改动,再次重新打包就要再次启动运行命令启动;另外,每次关机开机也需要重新启动;多次使用,就会让人很烦。所以,可以将jar包做成后台服务,开机自启。
二、应用软件WinSW
下载地址:WinSW.NET4.exe下载地址

下载下面框选的两个即可
在这里插入图片描述
三、配置过程

  1. 下载winsw后,安装 .net framework4 ,否则后面会出问题。(已安装请忽略)
  2. SpringBoot项目通过执行mvn clean——> package命令后得到可执行jar包:main.jar。
  3. 将sample-minimal.xml和WinSW.NET4.exe改为main.xml和main.exe.
  4. .exe文件、.xml文件名和jar包名称保持一致即可。
  5. 将jar包、.exe文件、.xml文件放在同一目录下。
  6. 配置mian.xml文件如下:
<!--
  MIT License

  Copyright (c) 2008-2020 Kohsuke Kawaguchi, Sun Microsystems, Inc., CloudBees,
  Inc., Oleg Nenashev and other contributors

  Permission is hereby granted, free of charge, to any person obtaining a copy
  of this software and associated documentation files (the "Software"), to deal
  in the Software without restriction, including without limitation the rights
  to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  copies of the Software, and to permit persons to whom the Software is
  furnished to do so, subject to the following conditions:

  The above copyright notice and this permission notice shall be included in all
  copies or substantial portions of the Software.

  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  SOFTWARE.
-->

<!--
 This is an example of a minimal Windows Service Wrapper configuration, which includes only mandatory options.
 
 This configuration file should be placed near the WinSW executable, the name should be the same.
 E.g. for myapp.exe the configuration file name should be myapp.xml
 
 You can find more information about the configuration options here: https://github.com/kohsuke/winsw/blob/master/doc/xmlConfigFile.md
 Full example: https://github.com/kohsuke/winsw/blob/master/examples/sample-allOptions.xml
-->
<service>
  
  <!-- ID of the service. It should be unique accross the Windows system-->
  <!--服务ID:启动、关闭、删除服务时,都是通过ID来操作的-->
  <id>main</id>
  <!-- Display name of the service -->
  <!--服务名称-->
  <name>main (powered by WinSW)</name>
  <!-- Service description -->
  <!--服务描述-->
  <description>This service is a service for main Project(http://www.eachtravel.com/)</description>
  
  <!-- Path to the executable, which should be started -->
  <!--当前电脑配置了java环境变量,直接写成“java”就行;你也可以写成类似这样:E:\jar\jdk1.8\jre\bin\java-->
  <executable>java</executable>
  
  <!--启动参数-->
  <arguments>-jar main.jar</arguments>
  <!--<arguments>-jar "C:\Users\Administrator\Desktop\winsw\statement-0.0.1-SNAPSHOT.jar"</arguments>-->
  <!--日志配置-->
  <logmode>rotate</logmode>

</service>

  1. 配置完成,安装main.exe,切换目标目录(三个文件都在),执行命令,,安装服务。
cmd命令:main.exe install
  1. 安装完成,启动服务
通过命令:net start main  (服务ID)启动服务,或者是直接去服务界面手动启动服务。
通过命令:net stop main 停止服务,或者是直接去服务界面手动停止服务
停止服务后:sc delete ServiceName删除服务,即sc delete main

Guess you like

Origin blog.csdn.net/firstlt0217/article/details/116290425