windows running java in the background

Encountered a customer, the jar package needs to run on windows, so I took some time to study it. Usually in the process of developing java projects in windows, if you need to run the jar package, a command in cmd: java -jar test.jar will do it. But there is a problem. When the cmd window is closed, the java service is closed. Generally, jar is defined as a windows service through nssm. Obtained by nssm .
Note: After downloading, decompress to get nssm.exe, it is recommended to configure it in the environment variable

Remember that the four commonly used commands are basically enough

  1. nssm install [servicename] install jar as a service
  2. nssm start [servicename] Start the service, after the installation is successful, you can right-click the service to start
  3. nssm stop [servicename] stop the service, after the installation is successful, you can right-click the service to stop
  4. nssm remove [servicename] delete service

For example , wms.jar in the local C:\Work\jar is started as a service.

One, through the form of order

  1. nssm install wmsbackend java -jar C:\Work\jar\wms.jar, you can view this service in the service list after successful installation, as shown in the following figure:
    Insert picture description here
    Insert picture description here
  2. nssm start wmsbackend

Insert picture description here
Correspondingly, you can check that the service has started in the service list
3. nssm stop wmsbackend
4. nssm remove wmsbackend

Two, through the form of script

This form can solve the problem of not being able to generate log. Note: All scripts must be opened as an administrator.

  1. Encapsulate the jar startup command in the bat script. The name of the script is the name of the service, such as wmsbackend.bat. Note: The jar package path should be specified, and it is recommended to put it in the same path as bat.
  2. Package nssm install wmsbackend in install.bat, double-click this script to register the service. As shown below:

Insert picture description here
3. Encapsulate nssm stop wmsbackend in stop.bat, double-click this script to stop the service
4. Encapsulate nssm remove wmsbackend in remove.bat, double-click this script to delete the service
so far, start the service in the form of a script and it will end, if the project The log generation mechanism is configured in, and the log can be found in the same directory as the jar. Next, let's introduce the method of window real-time view log. View the log in real time in the form of tail -f in linux. There are several methods in windows, but the safer method is to use the log monitoring method in notepad++, as shown in the following figure:
Insert picture description here

Guess you like

Origin blog.csdn.net/hongyinanhai00/article/details/108801916