将标准的java程序封装为Windows下面的服务

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

 

Java代码 复制代码
  1.     
  2. # Java Application完整路径   
  3. wrapper.java.command=java   
  4.   
  5. wrapper.java.mainclass=org.tanukisoftware.wrapper.WrapperSimpleApp   
  6.   
  7. #定义程序运行的classpath   
  8. ## 注意你需要的jar都写上,包括wrapper.jar和当前目录(点)   
  9. wrapper.java.classpath.1=wrapper.jar   
  10. wrapper.java.classpath.2=d:/tomcat/shared/lib/commons-logging-1.1.jar   
  11. wrapper.java.classpath.3 = .    
  12.   
  13.   
  14. # Java Library Path (location of Wrapper.DLL or libwrapper.so)   
  15. wrapper.java.library.path.1=./   
  16.   
  17. # Java Additional Parameters   
  18. #wrapper.java.additional.1=   
  19.   
  20. # Initial Java Heap Size (in MB)   
  21. # 初始化的内存使用数量   
  22. #wrapper.java.initmemory=16  
  23.   
  24. # Maximum Java Heap Size (in MB)   
  25. # 最大的内存使用数量   
  26. #wrapper.java.maxmemory=64  
  27.   
  28. # 运行的Main Class   
  29. # 请携带路径,写上完整的运行主类的全名   
  30. wrapper.app.parameter.1= mypackage.MyMainService   
  31.   
  32. # Port which the native wrapper code will attempt to connect to   
  33. # 占用的端口,不要可存在的有冲突即可。用于与服务的通信。   
  34. wrapper.port=15013  
  35.   
  36. wrapper.console.format=PM   
  37. wrapper.console.loglevel=INFO   
  38. wrapper.logfile=Beanskt.log   
  39. wrapper.logfile.format=LPTM   
  40. wrapper.logfile.loglevel=INFO   
  41.   
  42. wrapper.logfile.maxsize=0  
  43. wrapper.logfile.maxfiles=0  
  44. wrapper.syslog.loglevel=NONE   
  45.   
  46. #window服务配置   
  47. #在控制台运行时的标题   
  48. wrapper.console.title=My Service   
  49. #服务名   
  50. wrapper.ntservice.name=My Service   
  51.   
  52. # 显示名称   
  53. wrapper.ntservice.displayname= My Service   
  54.   
  55. # 描述   
  56. wrapper.ntservice.description=sample Service   
  57.   
  58. # 依赖项   
  59. wrapper.ntservice.dependency.1=   
  60.   
  61. # 启动模式: AUTO_START or DEMAND_START   
  62. wrapper.ntservice.starttype=AUTO_START   
  63.   
  64. # 是否交互.   
  65. 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代码 复制代码
  1.     
  2. #********************************************************************   
  3. # Wrapper Properties   
  4. #********************************************************************   
  5. # Java Application   
  6. wrapper.java.command=java   
  7. # Java Main class.  This class must implement the WrapperListener interface  
  8. #  or guarantee that the WrapperManager class is initialized.  Helper   
  9. #  classes are provided to do this for you.  See the Integration section   
  10. #  of the documentation for details.   
  11. wrapper.java.mainclass=org.tanukisoftware.wrapper.WrapperSimpleApp   
  12. # Java Classpath (include wrapper.jar)  Add class path elements as   
  13. #  needed starting from 1  
  14. wrapper.java.classpath.1=d:/tomcat/shared/lib/java2000.jar   
  15. wrapper.java.classpath.2=d:/tomcat/shared/lib/commons-logging-1.1.jar   
  16. wrapper.java.classpath.3=wrapper.jar   
  17. wrapper.java.classpath.4=.   
  18. # Java Library Path (location of Wrapper.DLL or libwrapper.so)   
  19. wrapper.java.library.path=./   
  20. # Java Additional Parameters   
  21. # wrapper.java.additional.1=-Djava.ext.dirs=../lib;../tools/lib   
  22. # wrapper.java.additional.2=-Dphoenix.home=..   
  23. # wrapper.java.additional.3=-Djava.security.policy=jar:file:phoenix-loader.jar!/META-INF/java.policy   
  24. # wrapper.java.additional.4=-Djava.security.manager   
  25. # Initial Java Heap Size (in MB)   
  26. wrapper.java.initmemory=16  
  27. # Maximum Java Heap Size (in MB)   
  28. wrapper.java.maxmemory=64  
  29. # Application parameters.  Add parameters as needed starting from 1  
  30. wrapper.app.parameter.1=com.triuse.tools.Downloador   
  31. #********************************************************************   
  32. # Wrapper Advanced Properties   
  33. #********************************************************************   
  34. # Port which the native wrapper code will attempt to connect to   
  35. wrapper.port=15013  
  36. # Number of seconds to allow for the JVM to be launched and contact the wrapper before the   
  37. #  wrapper should assume that the JVM is hung and terminate the JVM process.  0 means never   
  38. #  time out.  Defaults to 30 seconds.   
  39. wrapper.startup.timeout=30  
  40. # Number of seconds to allow between the wrapper pinging the JVM and the response.  0 means   
  41. #  never time out.  Defaults to 30 seconds.   
  42. wrapper.ping.timeout=30  
  43. # Number of seconds to allow for the JVM to shutdown before the wrapper should assume that   
  44. #  the JVM is hung and terminate the JVM process.  0 means never time out.  Defaults to 30  
  45. #  seconds.   
  46. wrapper.shutdown.timeout=30  
  47. # The Wrapper detects when an application calls System.exit() and treats this as a request   
  48. #  to stop the server by default.   
  49. #wrapper.disable_shutdown_hook=TRUE   
  50. # Tell the Wrapper to request a JVM thread dump if the JVM fails to exit when requested.   
  51. #wrapper.request_thread_dump_on_failed_jvm_exit=TRUE   
  52. #********************************************************************   
  53. # Wrapper Logging parameters   
  54. #********************************************************************   
  55. # Format of output for the console.  (See docs for formats)   
  56. wrapper.console.format=PM   
  57. # Log Level for console output.  (See docs for log levels)   
  58. wrapper.console.loglevel=INFO   
  59. # Log file to use for wrapper output logging.   
  60. wrapper.logfile=./logs/wrapper.log   
  61. # Format of output for the log file.  (See docs for formats)   
  62. wrapper.logfile.format=LPTM   
  63. # Log Level for log file output.  (See docs for log levels)   
  64. wrapper.logfile.loglevel=INFO   
  65. # Maximum size that the log file will be allowed to grow to before   
  66. #  the log is rolled. Size is specified in bytes.  The default value   
  67. #  of 0, disables log rolling.  May abbreviate with the 'k' (kb) or   
  68. #  'm' (mb) suffix.  For example: 10m = 10 megabytes.   
  69. wrapper.logfile.maxsize=5m   
  70. # Maximum number of rolled log files which will be allowed before old   
  71. #  files are deleted.  The default value of 0 implies no limit.   
  72. wrapper.logfile.maxfiles=0  
  73. # Log Level for sys/event log output.  (See docs for log levels)   
  74. wrapper.syslog.loglevel=NONE   
  75. #********************************************************************   
  76. # Wrapper Unix daemon parameters   
  77. #********************************************************************   
  78. # File to write process ID to   
  79. #wrapper.pidfile=/var/run/phoenix.pid   
  80. #********************************************************************   
  81. # Wrapper Windows Properties   
  82. #********************************************************************   
  83. # Title to use when running as a console   
  84. wrapper.console.title=Triuse Downloador 1.0  
  85. #********************************************************************   
  86. # Wrapper Windows NT/2000/XP Service Properties   
  87. #********************************************************************   
  88. # WARNING - Do not modify any of these properties when an application   
  89. #  using this configuration file has been installed as a service.   
  90. #  Please uninstall the service before modifying this section.  The   
  91. #  service can then be reinstalled.   
  92. # Name of the service   
  93. wrapper.ntservice.name=Triuse Downloador   
  94. # Display name of the service   
  95. wrapper.ntservice.displayname=Triuse Downloador   
  96. # Description of the service   
  97. wrapper.ntservice.description=Triuse Downloador 1.0  
  98. # Service dependencies.  Add dependencies as needed starting from 1  
  99. wrapper.ntservice.dependency.1=   
  100. # Mode in which the service is installed.  AUTO_START or DEMAND_START   
  101. wrapper.ntservice.starttype=AUTO_START   
  102. # Priority at which the service is run.  NORMAL, LOW, HIGH, or REALTIME   
  103. wrapper.ntservice.process_priority=NORMAL   
  104. # Allow the service to interact with the desktop.   
  105. 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

 
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值