In the development of a project, it is sometimes necessary to package a Java application into a Windows service, and we can start and close the Java program directly through the Windows service.
This blog post will be implemented in two ways, manual creation and MAVEN automatic packaging.
I. Preparation
Download Java service wrapper
URL: http://sourceforge.net/projects/wrapper/or http://wrapper.tanukisoftware.com/doc/english/ download.jsp
The version i downloaded is wrapper-windows-x86-64-3.5.26-st.zip.
Two. Manual Creation Method
1. Configuration Process
1 first make sure that your computer has a Java operating environment, and if not, install it.
2 Package your Java program into a jar package. (The name of my jar is the class app for the Wrapper-0.0.1-snapshot.jar,main method)
3 Create a folder on the hard disk test, and under it create folders bin, Conf, Lib, logs.
4 ) Unzip the Wrapper-windows-x86-64-3.5.26-st.zipand place it in the bin directory under the Wrapper.exe, Src/bin directory App.bat.in, installapp-nt.bat.in, uninstallapp-nt.bat.in file
Copy to test 's Bin directory and rename to App.bat, Installapp-nt.bat, Uninstallapp-nt.bat, respectively.
5 ) Copy the Wrapper.DLL and Wrapper.jar in its Lib directory to the Lib directory of test. and copy the jar of the project and the jar you are using to that directory (including your own Java program jar).
6 Copy the wrapper.conf.in in its src/conf directory to the Conf directory of Workapp and name it wrapper.conf.
2. Modify the wrapper.conf file
The following items are mainly modified:
(1) JVM location:
Wrapper.java.command=c:\jdk1.5.0_07\bin\java or Wrapper.java.command=%java_home%/bin/java(need to configure java_home in the environment variables of the system)
(2) MAIN CLASS here determines how Java service wrapper is used
Wrapper.java.mainclass=org.tanukisoftware.wrapper.wrappersimpleapp
(3) All the jar packages required for your Java program must be marked here, and note that the path is accurate:
wrapper.java.classpath.1=. /lib/wrapper-0.0.1-snapshot.jar
Wrapper.java.classpath.2=. /lib/wrapper.jar
wrapper.java.classpath.3=. /lib/sqljdbc4.jar
...
(4) the directory where your Wrapper.DLL or Wrapper.jar are located
wrapper.java.library.path.1=. /lib
(5) Run class for your Java application (main class)
wrapper.app.parameter.1= window_wrapper.wrapper.App
(6) register as the name and display name of the service, you can set it freely
Wrapper.name=testwrapper
Wrapper.displayname= Test Wrapper Sample Application
(7) Service Description information
wrapper.description= Test Wrapper Sample applicationdescription
(8) startup type of the service
# Mode in which the service isinstalled. Auto_start, Delay_start or Demand_start
Wrapper.ntservice.starttype=auto_start
3. After the modification, run Myapp.bat to run your Java program, here you can test the configuration is correct, if you can run, prove the configuration OK.
4. run Installapp-nt.bat to register the service and Uninstallapp-nt.bat for the logoff service.
5. After running the registration service Installapp-nt.bat can see your registered service name in the control Panel-hypervisor-service. (Example: Test Wrapper Sample application)
three. Maven Automatic Packaging Method
1. pom.xml file introduces the relevant MAVEN plugins
<span style= "FONT-SIZE:14PX;" ><build><resources><resource><directory>${project.basedir}/src/main/resources</ Directory></resource><resource><targetpath>, .... /mywrapper/etc</targetpath><directory>${project.basedir}/src/main/resources/</directory>< includes><include>**/*.xls</include><include>**/*.xml</include><include>**/*. properties</include><include>**/*.bat</include></includes></resource>< Resource><targetpath>, .... /wrapper/jsw/mywrapper/bin</targetpath><directory>${project.basedir}/src/main/resources/</ directory><includes><include>**/*.bat</include></includes></resource>< Resource><targetpath>, .... /wrapper/jsw/mywrapper/etc</targetpath><directory>${project.basedir}/src/main/resources/</ Directory><includes><include>**/*.xls</include><include>**/*.xml</include><include>**/*.properties</include><include>**/*.bat</include></ Includes></resource></resources> <testresources><testresource><directory>${ project.basedir}/src/test/java</directory></testresource><testresource><directory>${ Project.basedir}/src/main/resources</directory></testresource></testresources> <plugins ><plugin><groupId>org.apache.maven.plugins</groupId><artifactId> Maven-compiler-plugin</artifactid><version>3.1</version><configuration><source> 1.6</source><target>1.6</target></configuration></plugin><plugin>< groupid>org.codehaus.mojo</groupid><artifactid>appassembler-maven-plugin</artifactid>< Version>1.3</version><executions><execution><id>generate Start scripts</id>< Phase>package</phase><goals> <goal>assemble</goal></goals><configuration><repositorylayout>flat</ repositorylayout><programs><program><!--Run the main class--><mainclass>window_ by batch processing wrapper.wrapper.app</mainclass><name>init_app</name></program></programs></ Configuration></execution><execution><phase>package</phase><goals><goal >generate-daemons</goal></goals><configuration><repositorylayout>flat</ repositorylayout><target>${project.build.directory}/wrapper</target>< includeconfigurationdirectoryinclasspath>true</includeconfigurationdirectoryinclasspath>< Usedaemonidaswrapperconfname>true</usedaemonidaswrapperconfname><daemons><daemon><id >mywrapper</id><!--Main class--><mainclass>window_wrapper.wrapper.app</packaged as a Windows service mainclass><!--<commandLineArguments> <commandlineargument>start</commandlineargument> </commandLineArguments>--><!--<jvmSettings> < Initialmemorysize>20m</initialmemorysize> <maxMemorySize>200M</maxMemorySize> < Maxstacksize>128m</maxstacksize> </jvmSettings>--><platforms><platform>jsw</ Platform></platforms><generatorconfigurations><generatorconfiguration><generator> Jsw</generator><includes><include>linux-x86-32</include><include>linux-x86-64 </include><include>macosx-universal-32</include><include>macosx-universal-64</ include><include>windows-x86-32</include><include>windows-x86-64</include></ includes><configuration><property><name>configuration.directory.in.classpath.first</ Name><value>etc</value></property></configuration></generatorconfiguration> </generatorconfigurations></daemon≫</daemons></configuration></execution></executions></plugin><plugin> <groupId>org.codehaus.mojo</groupId><artifactId>cobertura-maven-plugin</artifactId> <version>2.5.2</version><configuration><formats><format>html</format>< Format>xml</format></formats></configuration></plugin></plugins></build ><reporting><plugins><plugin><groupId>org.codehaus.mojo</groupId>< artifactid>cobertura-maven-plugin</artifactid><version>2.5.2</version></plugin>< /plugins></reporting></span>
2. Unzip the Wrapper-windows-x86-32-3.5.20.zip, installapp-nt.bat.in, uninstallapp-nt.bat.in files under the Src/bin directory, Modified into Installapp-nt.bat and Uninstall-nt.bat
3 ... The Init_app.bat batch file under the \target\appassembler\bin directory runs the packaged Main method
4. Copy the Installapp-nt.bat and Uninstall-nt.bat from 2 to: \target\wrapper\jsw\mywrapper\bin directory, or put Installapp-nt.bat and Uninstall-nt.bat in the program Resources directory.
Also replace the _wrapper_conf_default values in Installapp-nt.bat and Unistall-nt.bat with the: \target\wrapper\jsw\mywrapper\bin _wrapper_conf value in directory Mywrapper.bat
If all are MAVEN projects, the second one is simpler than the first one and does not need to be manually added to build rack packages and reference racks, MAVEN will help us to automatically break in.
Example Project: http://download.csdn.net/detail/a123demi/8390851
Detailed MAVEN project using Java Service wrapper to generate Windows services for Java programs