【JavaService】部署Java jar为Windows后台服务
【JavaService】部署Java jar为Windows后台服务将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() {// TODOFileOutputStream 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 offrem 修改控制台颜色color 1drem * 使用JavaService将TestTimer安装为Windows服务的脚本rem *rem * JavaService - Windows NT Service Daemon for Java applicationsrem * Copyright (C) 2006 Multiplan Consultants Ltd. LGPL Licensing appliesrem * Information about the JavaService software is available at the ObjectWebrem * web site. Refer to http://javaservice.objectweb.org for more details.rem 开始批处理文件中环境改动的本地化操作,在使用endlocal后环境将恢复到原先的内容SETLOCALrem 设置环境变量,指向当前路径SET BASE_PATH=%CD%rem 设置Java path: jre_homeSET JRE_HOME=D:\Program Files\Java\jre1.5.0_11rem 判断JRE_HOME是否正确if "%JRE_HOME%" == "" goto no_javaif not exist "%JRE_HOME%\bin\java.exe" goto no_javarem 设置jvm内存分配情况set JVM_MEMORY=-Xms128m -Xmx256m rem 设置jvmdll使用哪一种模式SET jvmdll=%JRE_HOME%\bin\client\jvm.dllif not exist "%jvmdll%" SET jvmdll=%JRE_HOME%\bin\hotspot\jvm.dllif not exist "%jvmdll%" goto no_javarem 设置JavaService路径set JSBINDIR=E:\AccountingTool\javaservice\JavaService-2.0.10set JSEXE=%JSBINDIR%\JavaServiceDebug.exerem 判断jar是否正确SET acctjar=%BASE_PATH%\TestTimer.jarif 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=-manualif "%1" == "-auto" SET svcmode=-autorem 设置JAVA_OPTSset JAVA_OPTS=%JAVA_OPTS% -Djava.class.path="%BASE_PATH%\TestTimer.jar"set JAVA_OPTS=%JAVA_OPTS% %JVM_MEMORY%rem 设置startstopSET START_STOP=-start cn.test.timer.TestTimerrem 设置Log文件路径set OUT_ERR=-out "%BASE_PATH%\service_out.log" -err "%BASE_PATH%\service_err.log"rem 设置despset DESP=-description "TestTimer Service" rem 设置执行命令行set runcmd="%JSEXE%" -install TestTimerServiceset runcmd=%runcmd% "%jvmdll%"set runcmd=%runcmd% %JAVA_OPTS%set runcmd=%runcmd% %START_STOP%set runcmd=%runcmd% %OUT_ERR%set runcmd=%runcmd% -currentset runcmd=%runcmd% "%BASE_PATH%"set runcmd=%runcmd% %svcmode%set runcmd=%runcmd% -overwriteset runcmd=%runcmd% -startup 6set runcmd=%runcmd% %DESP%echo %runcmd%rem 执行安装命令%runcmd%rem 启动服务net start TestTimerServiceif ERRORLEVEL 1 goto js_errorgoto 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:endENDLOCAL@echo .if "%2" NEQ "-np" @pause
注意: 其中JRE_HOME 和 JSBINDIR(javaservice路径) 视安装路径而异。
5. 停止并卸载服务的批处理文件,其中service 名称必须与上面注册的service名称一致
@echo offrem 修改控制台颜色color 1drem * 使用JavaService卸载TestTimerService服务的脚本rem *rem * JavaService - Windows NT Service Daemon for Java applicationsrem * Copyright (C) 2006 Multiplan Consultants Ltd. LGPL Licensing appliesrem * Information about the JavaService software is available at the ObjectWebrem * web site. Refer to http://javaservice.objectweb.org for more details.rem 开始批处理文件中环境改动的本地化操作,在使用endlocal后环境将恢复到原先的内容SETLOCALrem 设置环境变量SET BASE_PATH=E:\AccountingTool\Release\AccountingTool\rem 设置JavaService的路径set JSBINDIR=%BASE_PATH%\JavaService-2.0.10set JSEXE=%JSBINDIR%\JavaService.exerem 卸载服务前先停止服务net stop TestTimerServicerem 设置执行命令行set runcmd="%JSEXE%" -uninstall TestTimerService%runcmd%@echo .ENDLOCAL@echo .if "%2" NEQ "-np" @pause
页:
[1]