Three ways to deploy Jar packages on Windows

1. Start the cmd command

This method is relatively simple, but the service will be killed after the window is closed, the command is as follows

java -jar xxx.jar

insert image description here


2. The bat script starts

2.1 Start the jar package

Create a new bat script with the following content:

@echo off
%1 mshta vbscript:CreateObject("WScript.Shell").Run("%~s0 ::",0,FALSE)(window.close)&&exit
java -jar F:\IdeaProjects\im-api\target\im-api-0.0.1-SNAPSHOT.jar > imApiLog.log  2>&1 &
exit

Among them F:\IdeaProjects\im-api\target\im-api-0.0.1-SNAPSHOT.jaris the absolute path of my jar package, and imApiLog.logthe log will be stored in this file. Right-click the bat file and run it as administrator.

Note: Listen to the boss, there should be no spaces in the bat file path, and there should be no Chinese in the jar package path, otherwise Error: Unable to access jarfile will appear.


2.2 Closing the service

According to the port number of the project, check the port process, and then kill the process

netstat -ano|findstr 8088 # 根据端口号查进程
taskkill /f /pid 13968   # 根据进程id杀进程(/f:强制结束该进程以及所有子进程)

insert image description here

The above two methods are not conducive to later maintenance, please read on.


3. Use WinSW

Address: https://github.com/winsw/winsw/releases , I choose v2.12.0 version here. Download WinSW-x64.exe (choose according to your own system version) and sample-minimal.xml, as follows

insert image description here

sample-minimal.xml is an example of a minimal configuration, and sample-allOptions.xml is an example of all configurable parameters.


3.1 Double Naming

For convenience, first put the exe and xml files in the same directory as the jar package, and rename them according to your needs, as follows

insert image description here


3.2 xml configuration

<service> 
     <!-- 服务唯一ID -->
     <id>imApiId</id>
     <!-- 服务名称-->
     <name>imApiService</name>
     <!-- 服务描述-->
     <description>this is im api,author:chaodev</description>
     <executable>java</executable> 
     <arguments>-jar imApi.jar</arguments>
     <!-- 开机启动 -->
     <startmode>Automatic</startmode>
     <!-- 日志配置 -->
     <logpath>%BASE%\logs</logpath>
     <logmode>rotate</logmode>
 </service>

The 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.
  • executable: The command to execute, such as the start command java.
  • arguments: Command execution parameters, such as specifying virtual machine parameters, configuration file path, etc.
  • startmode: Startup mode, such as Automatic when booting up.
  • logpath: log path, %BASE% represents the relative path, which is the current directory.

3.3 Installation service

Switch to the directory where the exe is located and execute the following command

imApiService.exe install #根据自己exe文件名称修改

insert image description here

After installation, you can find the service in the system service, as follows

insert image description here


3.4 Uninstall service

Use the uninstall command, as follows

imApiService.exe uninstall

insert image description here


3.5 Starting and stopping services

In addition to manually clicking the control to start and stop directly on the system service interface, you can also use the following commands

xxx.exe start #启动服务
xxx.exe stop #停止服务
xxx.exe restart #重启服务
xxx.exe status #查看状态

as follows

insert image description here



For more technical dry goods, please continue to pay attention to Programmer Dachao.
Originality is not easy, please indicate the source for reprinting.

Guess you like

Origin blog.csdn.net/xch_yang/article/details/129167189