我们开发完应用程序一般把JAVA程序和JDK放在一起安装运行,默认是非服务的,在WINDOWS上,基本上是用批处理调用JAR文件来运行的方式,假如是服务端程序,那么这种方式就显得很不雅观,所以有时候需要把JAVA 后台程序注册为WINDOWS服务
我们利用APACHE tomcat注册为WINDOWS服务的方式,来为我们的JAVA程序注册成服务,需要的文件有如下目录
其中LIB里面的3个文件不需要修改,只用放好就可以了,config目录里面的log4j.properties这个根据需哟进行调整,wrapper.conf 是最重要的一个配置文件 ,内容如下
# Wrapper Properties
#********************************************************************
# Java Application
#wrapper.java.command=java
set.JAVA_HOME=/java/path
wrapper.java.command=..\\jdk1.6.0_10\\jre\\bin\\capaa-pipe-agent
# Java Main class. This class must implement the WrapperListener interface
# or guarantee that the WrapperManager class is initialized. Helper
# classes are provided to do this for you. See the Integration section
# of the documentation for details.
#wrapper.java.mainclass=org.tanukisoftware.wrapper.WrapperSimpleApp
wrapper.java.mainclass=org.tanukisoftware.wrapper.WrapperStartStopApp
# Java Classpath (include wrapper.jar) Add class path elements as
# needed starting from 1
wrapper.java.classpath.1=../lib/wrapper.jar
wrapper.java.classpath.2=../lib/wrappertest.jar
wrapper.java.classpath.3=../auditpipe.jar
wrapper.java.classpath.4=../cutDblink.jar
# Java Library Path (location of Wrapper.DLL or libwrapper.so)
wrapper.java.library.path.1=../lib
# Java Additional Parameters
wrapper.java.additional.1=-Dprogram.name=NB.bat
# Initial Java Heap Size (in MB)
#wrapper.java.initmemory=3
# Maximum Java Heap Size (in MB)
#wrapper.java.maxmemory=64
# Application parameters. Add parameters as needed starting from 1
wrapper.app.parameter.1=com.capaa.pipe.main.AuditMain
wrapper.app.parameter.2=1
wrapper.app.parameter.3=start
wrapper.app.parameter.4=com.Main
#TRUE/FALSE
wrapper.app.parameter.5=TRUE
wrapper.app.parameter.6=1
wrapper.app.parameter.7=stop
#********************************************************************
# Wrapper Logging Properties
#********************************************************************
# Format of output for the console. (See docs for formats)
wrapper.console.format=PM
# Log Level for console output. (See docs for log levels)
wrapper.console.loglevel=INFO
# Log file to use for wrapper output logging.
wrapper.logfile=../log/hzmc_pipe.log
# Format of output for the log file. (See docs for formats)
wrapper.logfile.format=LPTM
# Log Level for log file output. (See docs for log levels)
wrapper.logfile.loglevel=INFO
# Maximum size that the log file will be allowed to grow to before
# the log is rolled. Size is specified in bytes. The default value
# of 0, disables log rolling. May abbreviate with the 'k' (kb) or
# 'm' (mb) suffix. For example: 10m = 10 megabytes.
wrapper.logfile.maxsize=10m
# Maximum number of rolled log files which will be allowed before old
# files are deleted. The default value of 0 implies no limit.
wrapper.logfile.maxfiles=1
# Log Level for sys/event log output. (See docs for log levels)
wrapper.syslog.loglevel=NONE
#********************************************************************
# Wrapper NT Service Properties
#********************************************************************
# WARNING - Do not modify any of these properties when an application
# using this configuration file has been installed as a service.
# Please uninstall the service before modifying this section. The
# service can then be reinstalled.
# Name of the service
wrapper.ntservice.name=capaa-pipe-win
# Display name of the service
wrapper.ntservice.displayname=capaa-pipe-win
# Description of the service
wrapper.ntservice.description=capaa-pipe-win
# Service dependencies. Add dependencies as needed starting from 1
wrapper.ntservice.dependency.1=
# Mode in which the service is installed. AUTO_START or DEMAND_START
wrapper.ntservice.starttype=AUTO_START
# Allow the service to interact with the desktop.
wrapper.ntservice.interactive=true
我们这里做好安装文件的目录是这样的,如下图
一下是一些配置的摘要,做一个参考
在原有agent基础上增加服务相关的文件,lib目录下增加wrapper.dll,wrapper.jar,wrappertest.jar文件,config目录下增加wrapper.conf、log4j.properties,主目录下创建bin目录并在bin目录下增加InstallAgent-NT.bat,UninstallAgent-NT.bat,wrapper.exe
修改wrapper.conf配置,wrapper.java.command值设为..\\jdk1.6.0_10\\jre\\bin\\java-pipe,(JDK下面名称由javaw.exe修改),wrapper.java.classpath.3值设为../auditpipe.jar,wrapper.app.parameter.1值设为com.capaa.pipe.main.AuditMain,wrapper.logfile值设为../log/hzmc_pipe.log,wrapper.ntservice.name值设为capaa-pipe-win,wrapper.ntservice.displayname值设为capaa-pipe-win,wrapper.ntservice.description值设为capaa-pipe-win
修改log4j.properties配置,log4j.appender.R.File值为../log/java_pipe.log
修改InstallAgent-NT.bat配置,在倒数第三行"%_WRAPPER_EXE%" -i %_WRAPPER_CONF%之后,换行加上net start capaa-pipe-win
双击bin目录下的InstallAgent-NT.bat安装服务
查看进程中是是否有java-pipe.exe
查看windows服务中是否有capaa-pipe-win服务
查看日志是否正常
需要的文件见附件
下面转自网络
1、首先到请下载最新版本的Java Service Wrapper,点我下载。
2、准备如下文件,复制到test目录,这些文件可以从下载包中找到:
wrapper.dll
wrapper.exe
wrapper.jar
3、编写wrapper.conf,也放在同一级目录下面,内容如下
# Java Application完整路径
wrapper.java.command=java
wrapper.java.mainclass=org.tanukisoftware.wrapper.WrapperSimpleApp
#定义程序运行的classpath
wrapper.java.classpath.1=wrapper.jar
wrapper.java.classpath.2=[jar file/path]
# Java Library Path (location of Wrapper.DLL or libwrapper.so)
wrapper.java.library.path.1=.
# Java Additional Parameters
#wrapper.java.additional.1=
# Initial Java Heap Size (in MB)
#wrapper.java.initmemory=3
# Maximum Java Heap Size (in MB)
#wrapper.java.maxmemory=64
# 运行的Main Class
wrapper.app.parameter.1=
wrapper.console.format=PM
wrapper.console.loglevel=INFO
wrapper.logfile=Beanskt.log
wrapper.logfile.format=LPTM
wrapper.logfile.loglevel=INFO
wrapper.logfile.maxsize=0
wrapper.logfile.maxfiles=0
wrapper.syslog.loglevel=NONE
#window服务配置
wrapper.console.title=samplesvr
#服务名
wrapper.ntservice.name=samplesvr
# 显示名称
wrapper.ntservice.displayname= sample Service
# 描述
wrapper.ntservice.description=sample Service
# 依赖项
wrapper.ntservice.dependency.1=
# 启动模式: AUTO_START or DEMAND_START
wrapper.ntservice.starttype=AUTO_START
# 是否交互.
wrapper.ntservice.interactive=false
4、运行测试,安装服务
》测试运行是否正常
wrapper.exe -c wrapper.conf
》安装服务
wrapper.exe -i wrapper.conf
》卸载服务
wrapper.exe -r wrapper.conf
》启动
wrapper.exe -t wrapper.conf
》停止
wrapper.exe -p wrapper.conf