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
- nssm install [servicename] install jar as a service
- nssm start [servicename] Start the service, after the installation is successful, you can right-click the service to start
- nssm stop [servicename] stop the service, after the installation is successful, you can right-click the service to stop
- 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
- 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:
- nssm start wmsbackend
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.
- 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.
- Package nssm install wmsbackend in install.bat, double-click this script to register the service. As shown below:
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: