Windows starts Java service

Recently, I need to temporarily use the function of Windows as a server, so I need to start the Java service on Windows. Previously, on the Linux server, this was done by configuring the system system service or through the startup script. Similarly, it can also be done on Windows through these two methods. To be done.

1. Start through script

1.1 You can start it by opening the cmd window in the directory where the jar package is located and executing java -jar test.jar. However, this method requires you to enter the command every time.
1.2 You can configure the startup command into a script to execute startup with one click

@echo

e:
cd e:\service-back
#打开E盘下的service-back文件夹

java -jar test.jar

Through this, you can click on the bat script to start it

2. Configure it as a system service

The above two startup methods are the same in principle. They both rely on the cmd command window to start. Once the window is accidentally closed or the computer is shut down and restarted, the system will stop interrupting, so it can be configured as a system service. to start.
2.1 Download WinSW.NET4.exe, the download address is: https://github.com/winsw/winsw/releases
Insert image description here
2.2 Create a new WinSW.NET4.xml file and edit it

<configuration>
    <!--安装成Windows服务后的服务名-->
    <id>test-data</id>
    <!--显示的服务名称-->
    <name>test-data</name>
    <!--对服务的描述-->
    <description>测试服务请求</description>
    <!--这里写java的路径,如何配置了环境变量直接写"java"就行-->
    <executable>java</executable>
    <!--Xmx256m 代表堆内存最大值为256MB -jar后面的是项目名-->
    <arguments>-Xmx256m -jar test-data.jar</arguments>
     <!--让服务自动启动-->
    <startmode>Automatic</startmode>
    <!--日志路径-->
    <logpath>%BASE%\logs</logpath>
    <!--日志模式-->
    <log mode="roll-by-size-time">
      <sizeThreshold>10240</sizeThreshold>
      <pattern>yyyyMMdd</pattern>
      <autoRollAtTime>00:00:00</autoRollAtTime>
      <zipOlderThanNumDays>5</zipOlderThanNumDays>
      <zipDateFormat>yyyyMM</zipDateFormat>
    </log>
</configuration>

The above content needs to be modified according to your own situation

2.3 Create new install.bat and uninstall.bat scripts to inject and delete configured system services

#install.bat
WinSW.NET4.exe install
#uninstall.bat
WinSW.NET4.exe uninstall

Finally, put the above four files and the packaged jar package under the same folder, click the install.bat script to start, open the system service, find the service that was just configured and injected, and then start and start it through the system service method. stopped.
Insert image description here
2.4 How to delete the configured system service, you can delete it by executing the uninstall.bat script, or you can delete it by the following method.
1. Find the service name to be deleted in services.msc
2. Run cmd as administrator and enter sc delete service name

The above is a record of personal usage and is only for reference.

Guess you like

Origin blog.csdn.net/s990420/article/details/128925980