Set the java project to start automatically under Windows

Here is to register the java project as a Windows service to achieve boot self-starting.

Check the .NET framework version

Because the winsw tool needs to be used when running .NET framework, basically the current win10 system comes with .NET framework4.0. In order to select the appropriate version, we can check the local .NET Framework version and select the appropriate winsw version according to the version.

  • Open the local resource manager, enter in the address bar C:\Windows\Microsoft.NET\Framework, enter the directory
  • Check the .NET Framework version in the current directory,
    insert image description here
    you can see that the highest version is 4

winsw

The winsw tool, full name Windows Service Wrapper, is a small tool developed using C#.
Its use principle is that winsw.exe itself is registered as a windows service, which can be set to start automatically. When it starts, it cooperates with the configuration in the xml file with the same name as the exe, and executes the set command to achieve the effect of self-starting.
Winsw open source address: https://github.com/winsw/winsw
I am using v2.11.0 version, download address: https://github.com/winsw/winsw/releases/tag/v2.11.0
insert image description here
According to .Net Framework Just download the version, such as: WinSW.NET4.exe
sample-minimal.xml is a minimal configuration example.
sample-allOptions.xml is an example of all configurable parameters.

Modify and configure

  1. Change the name of winsw.exe to a meaningful name, because the exe started by the service after registration is this exe, and the name displayed in the service list is also this name, which is generally the same as sample-minimal.xml, and is modified together to be packaged with the Springboot project jar with the same name, such as appstore.exe, appstore.xml, appstore.jar
    insert image description here
  2. xml settings
<service>
 
     <!-- 服务名称 -->
 
     <id>test</id>
 
     <name> appstore </name>
 
     <description>应用市场</description>
 
     <!-- java环境变量 -->
 
 
     <executable>java</executable>
 
    <arguments>-Dspring.profiles.active=prod -server -Xms512m -Xmx1024m -XX:MaxNewSize=1024m -XX:MaxPermSize=1024m -XX:CompressedClassSpaceSize=512m -XX:MetaspaceSize=512m -XX:MaxMetaspaceSize=512m -jar "%BASE%\appstore.jar"</arguments>
 
     <!-- 开机启动 -->
 
     <startmode>Automatic</startmode>
 
     <!-- 日志配置 -->
 
     <logpath>%BASE%\log</logpath>
 
     <logmode>rotate</logmode>
 
 </service>

The relevant parameters are described as follows:

  • id: The service ID after installing the windows service, which must be unique.
  • name: Service name, which must also be unique. Generally, it should be the same as the id.
  • description: service description, can use Chinese, can be used as a note.
  • executable: The command to execute, such as the command java to start the springboot application.
  • arguments: command execution parameters, such as package path, class path, etc.

After configuration, put appstore.exe, appstore.xml, appstore.jar in the same directory

registration service

Enter cmd in the address bar of the Java project folder and execute the command to install and register.

appstore.exe install

insert image description here
insert image description here

Tool Miscellaneous Commands

Command Line meaning
appstore.exe install install service
appstore.exe start start service
appstore.exe stop Out of service
appstore.exe restart restart service
appstore.exe uninstall delete service
appstore.exe status check status

Guess you like

Origin blog.csdn.net/qq_41596778/article/details/130110949