How to make jar package into windows service

Reprinted: https://blog.csdn.net/qq_33443402/article/details/80168877

the first method:

1, prepare tools

Hello.jar: Runnable package. Here I am the simplest say helloworld! The class path where the Main function is located: org.springframework.boot.loader.JarLauncher (this is the startup class for general springboot projects),

JavaService: download link http://download.forge.ow2.org/javaservice/JavaService_2_0_10-amd64.zip (WIN 64 bit)

                                     http://download.forge.ow2.org/javaservice/JavaService-2.0.10.zip (WIN 32 bit)

JavaService is a utility that can turn Java applications into NT services, and is generally used to start application servers.

2, set environment variables

Environment variable 1, JAVA_HOME: JDK installation path

Environment variable 2. JAR_HOME: JAR_HOME/bin is the path where the JAR package is located, that is, our working path.

3. Unzip the zip package of JavaService, copy JavaService.exe and jar package to the %JAR_HOME%/bin directory

4. Create a directory %JAR_HOME%/logs to store log files.

5. Open cmd (as an administrator), enter the %JAR_HOME%bin directory, and execute the command:

JavaService.exe -install HelloWorld "%JAVA_HOME%/jre/bin/server/jvm.dll"

-Djava.class.path="%JAR_HOME%/bin/hello.jar;%JAVA_HOME%/lib/tools.jar"

-Xms64M –Xmx256M

-start org.springframework.boot.loader.JarLauncher

 -stop org.springframework.boot.loader.JarLauncher

-method systemExit

-out "%JAR_HOME%/logs/out.log"

-err "%JAR_HOME%/logs/err.log"

-current "%JAR_HOME%/bin"

-depends MySQL

-auto –overwrite

Note: HelloWorld: service name (can be modified by yourself);

-Xms64M-The minimum and maximum memory supported by Xmx256M Java virtual machine;

-depends MySQL is optional (generally not added);

For more details, check: JavaService, the documentation in the zip package.

Specific command line parameter description, run JavaService.exe -help to view

7, When the words "The HelloWorld automatic service was successfully installed" appear after executing the above command, it means that the service has been created successfully;

"net start HelloWord"-start the service, "net stop HelloWord"-stop the service, "sc delete HelloWorld"-delete the service

 

##Here are a few simple installation, startup and shutdown scripts, just put the jar package, JavaService.exe, script file and a configuration file config.ini in the same path. Note that the script must be managed Open it under the authority of the user:

config.ini

//The name of the Windows service to be generated
serviceName=HelloWorldService
//The name of the Jar package
jarName=HelloWorld
install.bat

@echo off
setlocal enabledelayedexpansion
cd /d %~dp0
for /f "delims=" %%i in ('type "config.ini"^| find /i "="') do set %%i
set path=%cd%
md "%path%/logs"
echo %serviceName%
echo %jarName%
echo %JAVA_HOME%
echo %path%
JavaService.exe -install %serviceName% "%JAVA_HOME%/jre/bin/server/jvm.dll" -Djava.class.path="%path%/%jarName%.jar;%JAVA_HOME%/lib/tools.jar" -Xms64M Xmx256M -start org.springframework.boot.loader.JarLauncher -stop org.springframework.boot.loader.JarLauncher -method systemExit -out "%JAR_HOME%/logs/out.log" -err "%JAR_HOME%/logs/err.log" -current "%path%" -auto -overwrite
 
 
startup.bat

@echo off
setlocal enabledelayedexpansion
cd /d %~dp0
for /f "delims=" %%i in ('type "config.ini"^| find /i "="') do set %%i
net start %serviceName%
shutdown.bat

@echo off
setlocal enabledelayedexpansion
cd /d %~dp0
for /f "delims=" %%i in ('type "config.ini"^| find /i "="') do set %%i
net stop %serviceName%
第二种方法:

1. Use maven in idea to type the program into a jar and put it in the running directory.

2. Go to github and download winsw: https://github.com/kohsuke/winsw/releases

 

3. Copy the WinSW.NET4.exe file to the folder where the java program is located

4. Rename the java program and remove the "." in the name. For example HelloWorld-1.0.jar ----> HelloWorld.jar

5. Rename WinSW.exe to HelloWorld.exe (same name as jar)

6. Create a new xml file, name it HelloWorld.xml, and write the following content (see github instructions for some parameters):

<service>

     <id>HelloWorld</id>

     <name>HelloWorld</name>

     <description>This is HelloWorld service.</description>

     <!-- java environment variable-->

     <env name="JAVA_HOME" value="%JAVA_HOME%"/>

     <executable>java</executable>

    <arguments>-jar "E:\springboot\ HelloWorld.jar"</arguments>

     <!-- Boot up-->

     <startmode> Automatic </startmode>

     <!-- Log configuration-->

     <logpath>%BASE%\log</logpath>

     <logmode>rotate</logmode>

 </service>

If the environment variables are not configured, directly throw the three files into the bin directory of java to run. Remove the tag <env name="JAVA_HOME" value="%JAVA_HOME%"/>

  7. The command line navigates to the current directory and executes: 

 test.exe  install

8. Go to the windows service list to start the program.

(If you need to update the program, just stop the service first, then rename the new file to HelloWorld.jar, and finally start the service)
 

Guess you like

Origin blog.csdn.net/xiaozaq/article/details/92778140