在项目的开发中,有时候需要将Java应用程序打包成Windows服务,我们就直接可以通过windows的服务来启动和关闭java程序了。
本博文将通过有两种方法实现该功能,手动创建法和Maven自动打包法。
一. 准备
下载java service wrapper
网址:http://sourceforge.net/projects/wrapper/或http://wrapper.tanukisoftware.com/doc/english/download.jsp
我下载的版本是wrapper-windows-x86-64-3.5.26-st.zip。二.手动创建法
1. 配置过程
1)首先确定你的电脑上有java运行环境,没有的话请安装。
2)将你的java程序打包成jar包。(我的jar的名称为wrapper-0.0.1-SNAPSHOT.jar,main方法所在类App)
3)在硬盘上创建文件夹test,并在其下创建文件夹bin, conf, lib, logs。
4)解压wrapper-windows-x86-64-3.5.26-st.zip, 并将其bin目录下的Wrapper.exe、src/bin目录下的App.bat.in、InstallApp-NT.bat.in、UninstallApp-NT.bat.in文件
拷贝到test的bin目录中,并分别改名为App.bat、InstallApp-NT.bat、UninstallApp-NT.bat。
5)将其lib目录下的Wrapper.DLL、wrapper.jar拷贝到test的lib目录中。并且将项目的jar和所用到的jar都拷贝到该目录(包括你自己的java程序jar)。
6)将其src/conf目录下的wrapper.conf.in拷贝到workapp的conf目录中,并命名为wrapper.conf。
2. 修改wrapper.conf文件
主要修改下面几项:
(1)JVM位置:
wrapper.java.command=C:\jdk1.5.0_07\bin\java或者 wrapper.java.command=%JAVA_HOME%/bin/java(需要在系统的环境变量里配置JAVA_HOME)
(2)MAIN CLASS 此处决定了使用Java Service Wrapper的方式
wrapper.java.mainclass=org.tanukisoftware.wrapper.WrapperSimpleApp
(3)你的Java程序所需的jar包必须全部在此标明,注意路径准确:
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)你的Wrapper.DLL或wrapper.jar所在的目录
wrapper.java.library.path.1=../lib
(5)你的Java应用程序的运行类(主类)
wrapper.app.parameter.1= window_wrapper.wrapper.App
(6)注册为服务的名称和显示名,你可以随意进行设置
wrapper.name=testwrapper
wrapper.displayname= Test Wrapper Sample Application
(7)服务描述信息
wrapper.description= Test Wrapper Sample ApplicationDescription
(8)服务的启动类型
# Mode in which the service isinstalled. AUTO_START, DELAY_START or DEMAND_START
wrapper.ntservice.starttype=AUTO_START
3. 修改好了以后,运行MyApp.bat即可运行你的java程序,这里可以测试配置的是否正确,如果可以运行,证明配置ok。
4. 运行InstallApp-NT.bat可以进行服务的注册,UninstallApp-NT.bat为注销服务。
5. 运行完注册服务InstallApp-NT.bat可以在 控制面板-管理程序-服务中看到你注册的服务名称。(如:Test Wrapper Sample Application)
三.Maven自动打包法
1. pom.xml文件引入相关的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>
<!-- 通过批处理 运行Main类 -->
<mainClass>
window_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>
<!-- 打包成windows服务的Main类 -->
<mainClass>window_wrapper.wrapper.App</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. 解压wrapper-windows-x86-32-3.5.20.zip, src/bin目录下的InstallApp-NT.bat.in、UninstallApp-NT.bat.in文件,修改成InstallApp-Nt.bat和Uninstall-Nt.bat
3. ..\target\appassembler\bin目录下的init_app.bat批处理文件 运行打包的main方法
4. 将2中的InstallApp-Nt.bat和Uninstall-Nt.bat复制到..\target\wrapper\jsw\mywrapper\bin目录,或者在程序resources目录中放入InstallApp-Nt.bat和Uninstall-Nt.bat。
同时将installApp-Nt.bat和Unistall-Nt.bat中的_WRAPPER_CONF_DEFAULT值替换成..\target\wrapper\jsw\mywrapper\bin目录mywrapper.bat中 _WRAPPER_CONF值
如果都为Maven项目,第二种比第一种简单,不需要手动的去添加生成架包和引用架包,maven会帮我们自动的打入。
示例项目下载地址:http://download.csdn.net/detail/a123demi/8390851