将Java jar文件部署为Windows后台服务有多种方法:Service Installer、Java service Wrapper、JavaService.exe等等。这里介绍下使用JavaService.exe来部署windows后台服务的方法。
1. 下载JavaService.exe, 最新的是2.0.10版本,请下载其zip包版本的即可
forge.ow2.org/projects/javaservice/
2. 安装JavaService.exe
将下载的zip包解压缩到任意目录即可
3. 编译一个测试jar(请从附件中下载): 每天11:00:00向time.log文件中写入当前时间
- public static void main(String[] args) {
- // schedule task执?
- scheduler.schedule(new SchedulerTask() {
- public void run() {
- // TODO
- FileOutputStream writerStream = null;
- OutputStreamWriter outSW = null;
- try {
- writerStream = new FileOutputStream("F:/TestTimer/time.log");
- outSW = new OutputStreamWriter(writerStream, "UTF-8");
- //将缓冲对文件的?出
- final SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd HH:mm:ss");
- outSW.write(sdf.format(new Date()));
- } catch (final IOException e) {
- e.printStackTrace();
- } finally {
- try {
- // 向缓冲区写成功后?执?缓冲区刷新?使文件内容生效
- if (outSW != null) {
- outSW.close();
- }
- //关?文件流
- if (writerStream != null) {
- writerStream.close();
- }
- } catch (final IOException e) {
- e.printStackTrace();
- }
- }
- }
- }, new DailyIterator(11, 0, 0));
- }
4. 在jar文件所在的目录下,建立如下批处理文件,执行后即可完成服务的注册和auto start
- @echo off
- rem 修改控制台颜色
- color 1d
- rem * 使用JavaService将TestTimer安装为Windows服务的脚本
- rem *
- rem * JavaService - Windows NT Service Daemon for Java applications
- rem * Copyright (C) 2006 Multiplan Consultants Ltd. LGPL Licensing applies
- rem * Information about the JavaService software is available at the ObjectWeb
- rem * web site. Refer to http://javaservice.objectweb.org for more details.
- rem 开始批处理文件中环境改动的本地化操作,在使用endlocal后环境将恢复到原先的内容
- SETLOCAL
- rem 设置环境变量,指向当前路径
- SET BASE_PATH=%CD%
- rem 设置Java path: jre_home
- SET JRE_HOME=D:\Program Files\Java\jre1.5.0_11
- rem 判断JRE_HOME是否正确
- if "%JRE_HOME%" == "" goto no_java
- if not exist "%JRE_HOME%\bin\java.exe" goto no_java
- rem 设置jvm内存分配情况
- set JVM_MEMORY=-Xms128m -Xmx256m
- rem 设置jvmdll使用哪一种模式
- SET jvmdll=%JRE_HOME%\bin\client\jvm.dll
- if not exist "%jvmdll%" SET jvmdll=%JRE_HOME%\bin\hotspot\jvm.dll
- if not exist "%jvmdll%" goto no_java
- rem 设置JavaService路径
- set JSBINDIR=E:\AccountingTool\javaservice\JavaService-2.0.10
- set JSEXE=%JSBINDIR%\JavaServiceDebug.exe
- rem 判断jar是否正确
- SET acctjar=%BASE_PATH%\TestTimer.jar
- if not exist "%acctjar%" goto no_peer
- @echo . Using following version of JavaService executable:
- @echo .
- "%JSEXE%" -version
- @echo .
- rem parameters and files seem ok, go ahead with the service installation
- @echo .
- rem 处理该批处理的输入参数,后台服务启动模式:自动
- SET svcmode=
- if "%1" == "-manual" SET svcmode=-manual
- if "%1" == "-auto" SET svcmode=-auto
- rem 设置JAVA_OPTS
- set JAVA_OPTS=%JAVA_OPTS% -Djava.class.path="%BASE_PATH%\TestTimer.jar"
- set JAVA_OPTS=%JAVA_OPTS% %JVM_MEMORY%
- rem 设置startstop
- SET START_STOP=-start cn.test.timer.TestTimer
- rem 设置Log文件路径
- set OUT_ERR=-out "%BASE_PATH%\service_out.log" -err "%BASE_PATH%\service_err.log"
- rem 设置desp
- set DESP=-description "TestTimer Service"
- rem 设置执行命令行
- set runcmd="%JSEXE%" -install TestTimerService
- set runcmd=%runcmd% "%jvmdll%"
- set runcmd=%runcmd% %JAVA_OPTS%
- set runcmd=%runcmd% %START_STOP%
- set runcmd=%runcmd% %OUT_ERR%
- set runcmd=%runcmd% -current
- set runcmd=%runcmd% "%BASE_PATH%"
- set runcmd=%runcmd% %svcmode%
- set runcmd=%runcmd% -overwrite
- set runcmd=%runcmd% -startup 6
- set runcmd=%runcmd% %DESP%
- echo %runcmd%
- rem 执行安装命令
- %runcmd%
- rem 启动服务
- net start TestTimerService
- if ERRORLEVEL 1 goto js_error
- goto end
- :no_java
- @echo . 没有Java运行环境,安装脚本不能运行
- goto error_exit
- :no_peer
- @echo . 启动文件TestTimer.jar不存在,安装脚本不能运行
- goto error_exit
- :no_jsexe
- @echo . 可执行文件JavaService.exe 不存在,安装脚本不能运行
- goto error_exit
- :js_error
- @echo . TestTimerService在安装为服务的过程中发生了错误,请检查相关日志文件
- goto error_exit
- :error_exit
- @echo .
- @echo . 安装失败,不能将 TestTimer安装为Windows服务
- @echo .
- @echo . 命令格式:
- @echo .
- @echo . %~n0 [-auto / -manual] [-np]
- @echo .
- @echo . 其中:
- @echo . -auto (默认) or -manual 参数说明了服务的启动模式:自动或者手动
- @echo . -np 批处理命令执行完毕后不暂停
- @echo .
- @echo . 比如:
- @echo . %~n0 -auto -np
- :end
- ENDLOCAL
- @echo .
- if "%2" NEQ "-np" @pause
注意: 其中JRE_HOME 和 JSBINDIR(javaservice路径) 视安装路径而异。
5. 停止并卸载服务的批处理文件,其中service 名称必须与上面注册的service名称一致
- @echo off
- rem 修改控制台颜色
- color 1d
- rem * 使用JavaService卸载TestTimerService服务的脚本
- rem *
- rem * JavaService - Windows NT Service Daemon for Java applications
- rem * Copyright (C) 2006 Multiplan Consultants Ltd. LGPL Licensing applies
- rem * Information about the JavaService software is available at the ObjectWeb
- rem * web site. Refer to http://javaservice.objectweb.org for more details.
- rem 开始批处理文件中环境改动的本地化操作,在使用endlocal后环境将恢复到原先的内容
- SETLOCAL
- rem 设置环境变量
- SET BASE_PATH=E:\AccountingTool\Release\AccountingTool\
- rem 设置JavaService的路径
- set JSBINDIR=%BASE_PATH%\JavaService-2.0.10
- set JSEXE=%JSBINDIR%\JavaService.exe
- rem 卸载服务前先停止服务
- net stop TestTimerService
- rem 设置执行命令行
- set runcmd="%JSEXE%" -uninstall TestTimerService
- %runcmd%
- @echo .
- ENDLOCAL
- @echo .
- if "%2" NEQ "-np" @pause