---
title: "将标准的java程序封装为Windows下面的服务"
author: "lanmh"
date: "2010-10-19T14:02:45Z"
tags: []
categories: ["编程语言"]
slug: "java-windows"
draft: false
lastmod: "2010-10-19T14:03:06Z"
id: 788482
url: "https://lanmh.iteye.com/blog/788482"
---

1、首先到请下载最新版本的Java Service Wrapper，  
可以到 [http://wrapper.tanukisoftware.org/doc/english/introduction.html](http://wrapper.tanukisoftware.org/doc/english/introduction.html) 原始站点下载最新版。  
也可以使用本站使用中的一个版本 [点我下载](http://www.java2000.net/download/Wrapper.zip) 。  
2、准备如下文件，复制到test目录，这些文件可以从下载包中找到：  
wrapper.dll  
wrapper.exe  
wrapper.jar  
3、编写wrapper.conf，也放在同一级目录下面，内容如下

 

Java代码 [![复制代码](/images/icon_copy.gif)](# "复制代码")

01. # Java Application完整路径
02. wrapper.java.command=java
03. wrapper.java.mainclass=org.tanukisoftware.wrapper.WrapperSimpleApp
04. #定义程序运行的classpath
05. ## 注意你需要的jar都写上，包括wrapper.jar和当前目录(点)
06. wrapper.java.classpath.1=wrapper.jar
07. wrapper.java.classpath.2=d:/tomcat/shared/lib/commons-logging-1.1.jar
08. wrapper.java.classpath.3 = .
09. # Java Library Path (location of Wrapper.DLL or libwrapper.so)
10. wrapper.java.library.path.1=./
11. # Java Additional Parameters
12. #wrapper.java.additional.1=
13. # Initial Java Heap Size (in MB)
14. # 初始化的内存使用数量
15. #wrapper.java.initmemory=16
16. # Maximum Java Heap Size (in MB)
17. # 最大的内存使用数量
18. #wrapper.java.maxmemory=64
19. # 运行的Main Class
20. # 请携带路径，写上完整的运行主类的全名
21. wrapper.app.parameter.1= mypackage.MyMainService
22. # Port which the **native** wrapper code will attempt to connect to
23. # 占用的端口，不要可存在的有冲突即可。用于与服务的通信。
24. wrapper.port=15013
25. wrapper.console.format=PM
26. wrapper.console.loglevel=INFO
27. wrapper.logfile=Beanskt.log
28. wrapper.logfile.format=LPTM
29. wrapper.logfile.loglevel=INFO
30. wrapper.logfile.maxsize=0
31. wrapper.logfile.maxfiles=0
32. wrapper.syslog.loglevel=NONE
33. #window服务配置
34. #在控制台运行时的标题
35. wrapper.console.title=My Service
36. #服务名
37. wrapper.ntservice.name=My Service
38. # 显示名称
39. wrapper.ntservice.displayname= My Service
40. # 描述
41. wrapper.ntservice.description=sample Service
42. # 依赖项
43. wrapper.ntservice.dependency.1=
44. # 启动模式： AUTO\_START or DEMAND\_START
45. wrapper.ntservice.starttype=AUTO\_START
46. # 是否交互.
47. wrapper.ntservice.interactive=**false**

```
 
# Java Application完整路径
wrapper.java.command=java

wrapper.java.mainclass=org.tanukisoftware.wrapper.WrapperSimpleApp

#定义程序运行的classpath
## 注意你需要的jar都写上，包括wrapper.jar和当前目录(点)
wrapper.java.classpath.1=wrapper.jar
wrapper.java.classpath.2=d:/tomcat/shared/lib/commons-logging-1.1.jar
wrapper.java.classpath.3 = . 


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

# Maximum Java Heap Size (in MB)
# 最大的内存使用数量
#wrapper.java.maxmemory=64

# 运行的Main Class
# 请携带路径，写上完整的运行主类的全名
wrapper.app.parameter.1= mypackage.MyMainService

# Port which the native wrapper code will attempt to connect to
# 占用的端口，不要可存在的有冲突即可。用于与服务的通信。
wrapper.port=15013

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=My Service
#服务名
wrapper.ntservice.name=My Service

# 显示名称
wrapper.ntservice.displayname= My 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

提示：  
服务一般都是连续运行的，所以你的代码一般都是包含在一个死循环里面的。比如  
while(true){  
if(forceStop){  
break;  
}  
try{  
// 代码的主体  
}catch(Exception ex){  
// 异常处理，防止循环退出  
}  
}

附属一个我的完整版的配置文件

 

Java代码 [![复制代码](/images/icon_copy.gif)](# "复制代码")

001. #\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\**
002. # Wrapper Properties
003. #\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\**
004. # Java Application
005. wrapper.java.command=java
006. # Java Main **class**.  This **class** must implement the WrapperListener **interface**
007. #  or guarantee that the WrapperManager **class** is initialized.  Helper
008. #  classes are provided to **do** **this** **for** you.  See the Integration section
009. #  of the documentation **for** details.
010. wrapper.java.mainclass=org.tanukisoftware.wrapper.WrapperSimpleApp
011. # Java Classpath (include wrapper.jar)  Add **class** path elements as
012. #  needed starting from 1
013. wrapper.java.classpath.1=d:/tomcat/shared/lib/java2000.jar
014. wrapper.java.classpath.2=d:/tomcat/shared/lib/commons-logging-1.1.jar
015. wrapper.java.classpath.3=wrapper.jar
016. wrapper.java.classpath.4=.
017. # Java Library Path (location of Wrapper.DLL or libwrapper.so)
018. wrapper.java.library.path=./
019. # Java Additional Parameters
020. # wrapper.java.additional.1=-Djava.ext.dirs=../lib;../tools/lib
021. # wrapper.java.additional.2=-Dphoenix.home=..
022. # wrapper.java.additional.3=-Djava.security.policy=jar:file:phoenix-loader.jar!/META-INF/java.policy
023. # wrapper.java.additional.4=-Djava.security.manager
024. # Initial Java Heap Size (in MB)
025. wrapper.java.initmemory=16
026. # Maximum Java Heap Size (in MB)
027. wrapper.java.maxmemory=64
028. # Application parameters.  Add parameters as needed starting from 1
029. wrapper.app.parameter.1=com.triuse.tools.Downloador
030. #\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\**
031. # Wrapper Advanced Properties
032. #\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\**
033. # Port which the **native** wrapper code will attempt to connect to
034. wrapper.port=15013
035. # Number of seconds to allow **for** the JVM to be launched and contact the wrapper before the
036. #  wrapper should assume that the JVM is hung and terminate the JVM process.  0 means never
037. #  time out.  Defaults to 30 seconds.
038. wrapper.startup.timeout=30
039. # Number of seconds to allow between the wrapper pinging the JVM and the response.  0 means
040. #  never time out.  Defaults to 30 seconds.
041. wrapper.ping.timeout=30
042. # Number of seconds to allow **for** the JVM to shutdown before the wrapper should assume that
043. #  the JVM is hung and terminate the JVM process.  0 means never time out.  Defaults to 30
044. #  seconds.
045. wrapper.shutdown.timeout=30
046. # The Wrapper detects when an application calls System.exit() and treats **this** as a request
047. #  to stop the server by **default**.
048. #wrapper.disable\_shutdown\_hook=TRUE
049. # Tell the Wrapper to request a JVM thread dump **if** the JVM fails to exit when requested.
050. #wrapper.request\_thread\_dump\_on\_failed\_jvm\_exit=TRUE
051. #\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\**
052. # Wrapper Logging parameters
053. #\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\**
054. # Format of output **for** the console.  (See docs **for** formats)
055. wrapper.console.format=PM
056. # Log Level **for** console output.  (See docs **for** log levels)
057. wrapper.console.loglevel=INFO
058. # Log file to use **for** wrapper output logging.
059. wrapper.logfile=./logs/wrapper.log
060. # Format of output **for** the log file.  (See docs **for** formats)
061. wrapper.logfile.format=LPTM
062. # Log Level **for** log file output.  (See docs **for** log levels)
063. wrapper.logfile.loglevel=INFO
064. # Maximum size that the log file will be allowed to grow to before
065. #  the log is rolled. Size is specified in bytes.  The **default** value
066. #  of 0, disables log rolling.  May abbreviate with the 'k' (kb) or
067. #  'm' (mb) suffix.  For example: 10m = 10 megabytes.
068. wrapper.logfile.maxsize=5m
069. # Maximum number of rolled log files which will be allowed before old
070. #  files are deleted.  The **default** value of 0 implies no limit.
071. wrapper.logfile.maxfiles=0
072. # Log Level **for** sys/event log output.  (See docs **for** log levels)
073. wrapper.syslog.loglevel=NONE
074. #\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\**
075. # Wrapper Unix daemon parameters
076. #\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\**
077. # File to write process ID to
078. #wrapper.pidfile=/var/run/phoenix.pid
079. #\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\**
080. # Wrapper Windows Properties
081. #\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\**
082. # Title to use when running as a console
083. wrapper.console.title=Triuse Downloador 1.0
084. #\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\**
085. # Wrapper Windows NT/2000/XP Service Properties
086. #\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\**
087. # WARNING - Do not modify any of these properties when an application
088. #  using **this** configuration file has been installed as a service.
089. #  Please uninstall the service before modifying **this** section.  The
090. #  service can then be reinstalled.
091. # Name of the service
092. wrapper.ntservice.name=Triuse Downloador
093. # Display name of the service
094. wrapper.ntservice.displayname=Triuse Downloador
095. # Description of the service
096. wrapper.ntservice.description=Triuse Downloador 1.0
097. # Service dependencies.  Add dependencies as needed starting from 1
098. wrapper.ntservice.dependency.1=
099. # Mode in which the service is installed.  AUTO\_START or DEMAND\_START
100. wrapper.ntservice.starttype=AUTO\_START
101. # Priority at which the service is run.  NORMAL, LOW, HIGH, or REALTIME
102. wrapper.ntservice.process\_priority=NORMAL
103. # Allow the service to interact with the desktop.
104. wrapper.ntservice.interactive=**false**

```
 
#********************************************************************
# Wrapper Properties
#********************************************************************
# Java Application
wrapper.java.command=java
# 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=d:/tomcat/shared/lib/java2000.jar
wrapper.java.classpath.2=d:/tomcat/shared/lib/commons-logging-1.1.jar
wrapper.java.classpath.3=wrapper.jar
wrapper.java.classpath.4=.
# Java Library Path (location of Wrapper.DLL or libwrapper.so)
wrapper.java.library.path=./
# Java Additional Parameters
# wrapper.java.additional.1=-Djava.ext.dirs=../lib;../tools/lib
# wrapper.java.additional.2=-Dphoenix.home=..
# wrapper.java.additional.3=-Djava.security.policy=jar:file:phoenix-loader.jar!/META-INF/java.policy
# wrapper.java.additional.4=-Djava.security.manager
# Initial Java Heap Size (in MB)
wrapper.java.initmemory=16
# Maximum Java Heap Size (in MB)
wrapper.java.maxmemory=64
# Application parameters.  Add parameters as needed starting from 1
wrapper.app.parameter.1=com.triuse.tools.Downloador
#********************************************************************
# Wrapper Advanced Properties
#********************************************************************
# Port which the native wrapper code will attempt to connect to
wrapper.port=15013
# Number of seconds to allow for the JVM to be launched and contact the wrapper before the
#  wrapper should assume that the JVM is hung and terminate the JVM process.  0 means never
#  time out.  Defaults to 30 seconds.
wrapper.startup.timeout=30
# Number of seconds to allow between the wrapper pinging the JVM and the response.  0 means
#  never time out.  Defaults to 30 seconds.
wrapper.ping.timeout=30
# Number of seconds to allow for the JVM to shutdown before the wrapper should assume that
#  the JVM is hung and terminate the JVM process.  0 means never time out.  Defaults to 30
#  seconds.
wrapper.shutdown.timeout=30
# The Wrapper detects when an application calls System.exit() and treats this as a request
#  to stop the server by default.
#wrapper.disable_shutdown_hook=TRUE
# Tell the Wrapper to request a JVM thread dump if the JVM fails to exit when requested.
#wrapper.request_thread_dump_on_failed_jvm_exit=TRUE
#********************************************************************
# Wrapper Logging parameters
#********************************************************************
# 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
# 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=5m
# 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 Unix daemon parameters
#********************************************************************
# File to write process ID to
#wrapper.pidfile=/var/run/phoenix.pid
#********************************************************************
# Wrapper Windows Properties
#********************************************************************
# Title to use when running as a console
wrapper.console.title=Triuse Downloador 1.0
#********************************************************************
# 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.ntservice.name=Triuse Downloador
# Display name of the service
wrapper.ntservice.displayname=Triuse Downloador
# Description of the service
wrapper.ntservice.description=Triuse Downloador 1.0
# 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
# Priority at which the service is run.  NORMAL, LOW, HIGH, or REALTIME
wrapper.ntservice.process_priority=NORMAL
# Allow the service to interact with the desktop.
wrapper.ntservice.interactive=false

```