1. 程式人生 > >使用Java Service Wrapper將jar包安裝成Windows服務

使用Java Service Wrapper將jar包安裝成Windows服務

部署 detect cap xxx face port exception script 參考

在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.把解壓後的文件夾中src\bin中的文件復制到新建的jar_jsw/bin文件夾下面。並把所有文件的in後綴去掉。同時把解壓後文件夾中bin下的wrapper.exe也放到新建的bin下

5.把解壓後的文件夾中src\conf中的文件復制到新建的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 Wrappers 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 Files\Java\jdk1.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.\n\nPlease check on its status.\n

#********************************************************************
# 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

使用Java Service Wrapper將jar包安裝成Windows服務