使用Java Service Wrapper将jar包安装成Windows服务

在windows上运行jar包,需要在工作目录下使用命令行运行jar包,这样会出现一个命令行窗口。而且这个命令行窗口有可能会不小心被关闭,且服务器启动后需要人为去点击开启。

对于有些服务性的程序来说,我们需要将其部署为windows服务,在系统启动的时候自动启动。

前面我使用了yajsw来进行,请参考我的博文:http://www.cnblogs.com/kxxx/p/6744309.html

但本次在另外一台服务器上进行处理时,安装了服务之后不能打开,服务每次都报找不到指定文件的错误,试了很久也未搞定,不得不将眼光转移到另外一款软件Java Service Wrapper.

我在使用之前参考了如下文章:

http://blog.csdn.net/fly_sky520/article/details/8809325

http://blog.csdn.net/lqclh502/article/details/51557015

参考了之后,准备下载文件,下载地址:http://wrapper.tanukisoftware.com/doc/english/download.jsp

由于我的服务器是64位的,在下载的时候发现64位下没有免费版的,又担心其他2个版本要收费,故也没有下载其他2个版本,在网上寻找破解版的,最后在这个网址上下载了一个:

https://www.krenger.ch/blog/java-service-wrapper-3-5-14-for-windows-x64/

我下载的版本是3.5.14。

然后开始进行配置:

1.在我的服务器上新建了一个文件夹,取名 jar_jsw。

2.解压下载的zip包。

3.在新建的文件夹jar_jsw下新建bin、conf、lib、logs文件夹,并且将需要启动的jar包复制到jar_jsw目录下,我的jar包为:processapp.jar

4.把解压后的文件夹中srcin中的文件复制到新建的jar_jsw/bin文件夹下面。并把所有文件的in后缀去掉。同时把解压后文件夹中bin下的wrapper.exe也放到新建的bin下

5.把解压后的文件夹中srcconf中的文件复制到新建的conf文件夹中。把in后缀去掉,其实只有一个文件,即wrapper.conf。

6.把解压后的文件夹中lib中的wrapper.jar与wrapper.dll放到新建的lib下面。同时把启动jar包程序所需要的第三方jar包也放在这里。

7.开始配置wrapper.conf文件。配置选项请参见其它的文章。

8.配置以后,点击bin文件夹下面的App.bat进行测试,如果能够在console中出现正常结果的话就表明配置正确。然后点击InstallApp-NT.bat安装服务,也可以点击UninstallApp-NT.bat卸载服务。成功安装服务后可以在window服务管理中看到。

注:我的jar包使用了 System.getProperty("user.dir")  来获取工作目录,按照默认的设置,我启动的程序要报错,总是报找不到文件。于是我在网上查看jsw是否有修改工作目录的参数,最后在此进行了参考:

https://wrapper.tanukisoftware.com/doc/english/properties.html

发现有 wrapper.working.dir 这个设置,于是参考其设置:https://wrapper.tanukisoftware.com/doc/english/prop-working-dir.html

当修改了工作目录后,其对应的classpath等参数也需要做相应的变动,如上图所示的参数。我修改之后一切OK。 

 至此,我将jar包包装成windows服务完成。

 写在后面,我发现activemq也是使用此方法安装的服务。

下面给出我的配置文件:

encoding=UTF-8
# Configuration files must begin with a line specifying the encoding
#  of the the file.

#********************************************************************
# Wrapper License Properties (Ignored by Community Edition)
#********************************************************************
# Professional and Standard Editions of the Wrapper require a valid
#  License Key to start.  Licenses can be purchased or a trial license
#  requested on the following pages:
# http://wrapper.tanukisoftware.com/purchase
# http://wrapper.tanukisoftware.com/trial

# Include file problems can be debugged by removing the first '#'
#  from the following line:
##include.debug

# The Wrapper will look for either of the following optional files for a
#  valid License Key.  License Key properties can optionally be included
#  directly in this configuration file.
#include ../conf/wrapper-license.conf
#include ../conf/wrapper-license-%WRAPPER_HOST_NAME%.conf

# The following property will output information about which License Key(s)
#  are being found, and can aid in resolving any licensing problems.
#wrapper.license.debug=TRUE

#********************************************************************
# Wrapper Localization
#********************************************************************
# Specify the locale which the Wrapper should use.  By default the system
#  locale is used.
#wrapper.lang=en_US # en_US or ja_JP

# Specify the location of the Wrapper's language resources.  If these are
#  missing, the Wrapper will default to the en_US locale.
wrapper.lang.folder=../lang

#********************************************************************
# Wrapper Java Properties
#********************************************************************
# Java Application
#  Locate the java binary on the system PATH:
wrapper.java.command=C:Program FilesJavajdk1.8.0_25/bin/java
#  Specify a specific java binary:
#set.JAVA_HOME=/java/path
#wrapper.java.command=%JAVA_HOME%/bin/java

# Tell the Wrapper to log the full generated Java command line.
#wrapper.java.command.loglevel=INFO

# 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

# 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=../processapp.jar
#wrapper.java.classpath.3=../lib/*

#更改了working.dir之后.路径将要随之变化
wrapper.working.dir=../
wrapper.java.classpath.1=lib/wrapper.jar
wrapper.java.classpath.2=processapp.jar
wrapper.java.classpath.3=lib/*

# Java Library Path (location of Wrapper.DLL or libwrapper.so)
wrapper.java.library.path.1=lib

# Java Bits.  On applicable platforms, tells the JVM to run in 32 or 64-bit mode.
wrapper.java.additional.auto_bits=TRUE

# 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

# Application parameters.  Add parameters as needed starting from 1
wrapper.app.parameter.1=com.el.cardata.processapp.HCAPPStart

#********************************************************************
# Wrapper Logging Properties
#********************************************************************
# Enables Debug output from the Wrapper.
# wrapper.debug=TRUE

# 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=logs/wrapper.log
#wrapper.pidfile=bin/wrapper.pid
#wrapper.java.pidfile=bin/java.pid

# 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=0

# 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=0

# Log Level for sys/event log output.  (See docs for log levels)
wrapper.syslog.loglevel=NONE

#********************************************************************
# Wrapper General Properties
#********************************************************************
# Allow for the use of non-contiguous numbered properties
wrapper.ignore_sequence_gaps=TRUE

# Do not start if the pid file already exists.
wrapper.pidfile.strict=TRUE

# Title to use when running as a console
wrapper.console.title=CJ_MessagePro

#********************************************************************
# Wrapper JVM Checks
#********************************************************************
# Detect DeadLocked Threads in the JVM. (Requires Standard Edition)
wrapper.check.deadlock=TRUE
wrapper.check.deadlock.interval=60
wrapper.check.deadlock.action=RESTART
wrapper.check.deadlock.output=FULL

# Out Of Memory detection.
# (Simple match)
wrapper.filter.trigger.1000=java.lang.OutOfMemoryError
# (Only match text in stack traces if -XX:+PrintClassHistogram is being used.)
#wrapper.filter.trigger.1000=Exception in thread "*" java.lang.OutOfMemoryError
#wrapper.filter.allow_wildcards.1000=TRUE
wrapper.filter.action.1000=RESTART
wrapper.filter.message.1000=The JVM has run out of memory.

#********************************************************************
# Wrapper Email Notifications. (Requires Professional Edition)
#********************************************************************
# Common Event Email settings.
#wrapper.event.default.email.debug=TRUE
#wrapper.event.default.email.smtp.host=<SMTP_Host>
#wrapper.event.default.email.smtp.port=25
#wrapper.event.default.email.subject=[%WRAPPER_HOSTNAME%:%WRAPPER_NAME%:%WRAPPER_EVENT_NAME%] Event Notification
#wrapper.event.default.email.sender=<Sender email>
#wrapper.event.default.email.recipient=<Recipient email>

# Configure the log attached to event emails.
#wrapper.event.default.email.attach_log=TRUE
#wrapper.event.default.email.maillog.lines=50
#wrapper.event.default.email.maillog.format=LPTM
#wrapper.event.default.email.maillog.loglevel=INFO

# Enable specific event emails.
#wrapper.event.wrapper_start.email=TRUE
#wrapper.event.jvm_prelaunch.email=TRUE
#wrapper.event.jvm_start.email=TRUE
#wrapper.event.jvm_started.email=TRUE
#wrapper.event.jvm_deadlock.email=TRUE
#wrapper.event.jvm_stop.email=TRUE
#wrapper.event.jvm_stopped.email=TRUE
#wrapper.event.jvm_restart.email=TRUE
#wrapper.event.jvm_failed_invocation.email=TRUE
#wrapper.event.jvm_max_failed_invocations.email=TRUE
#wrapper.event.jvm_kill.email=TRUE
#wrapper.event.jvm_killed.email=TRUE
#wrapper.event.jvm_unexpected_exit.email=TRUE
#wrapper.event.wrapper_stop.email=TRUE

# Specify custom mail content
wrapper.event.jvm_restart.email.body=The JVM was restarted.

Please check on its status.


#********************************************************************
# Wrapper Windows NT/2000/XP 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.name=CJ_MessagePro

# Display name of the service
wrapper.displayname=CJ_MessagePro

# Description of the service
wrapper.description=CJ_MessagePro

# Service dependencies.  Add dependencies as needed starting from 1
wrapper.ntservice.dependency.1=

# Mode in which the service is installed.  AUTO_START, DELAY_START or DEMAND_START
wrapper.ntservice.starttype=AUTO_START

# Allow the service to interact with the desktop.
wrapper.ntservice.interactive=false

 

原文地址:https://www.cnblogs.com/kxxx/p/7171765.html