您的位置:首页 > 编程语言 > Java开发

通 5ae 过JavaService 将java jar包注册成Windows 服务

2018-08-02 09:47 591 查看
一、 下载Javaservice ,把压缩文件解压,放到本地磁盘。
下载地址:
JavaService 64位 http://download.forge.ow2.org/javaservice/JavaService_2_0_10-amd64.zip
JavaService 32位 http://download.forge.ow2.org/javaservice/JavaService-2.0.10.zip
二、 准备jar包,查看jar 包Main函数所在的class



三、 将jar包、解压后的Javaservice执行程序、jdk免安装包放在同一个文件夹下,并在该文件下新建err.log 和 out.log 文件。
四、 注册完成后可能会遇到“The LoadLibrary function failed for the following reason 找不到指定的模块的解决”的问题,为避免该问题,在此先进行处理。
方法1:拷贝%JAVA_HOME%/bin/msvcr100.dll 文件到C:\Windows\System32,
如果是64位系统还要拷贝到C:\Windows\SysWOW64。
方法2:拷贝%JAVA_HOME%/bin/msvcr100.dll 文件到到java程序打包成的jar文件同一个目录下
采用第二种方法,将java-1.8.0_172\jre\bin\msvcr100.dll拷贝到java 程序jar包所在的目录下,这个目录结构如下:



此时start.bat 为空文件
app 目录下存放的文件



五、 编写bat脚本(start.bat)
@echo off

rem Get the current path
set C_PATH=%CD%

rem Get the installation path
set /p "I_PATH=Please enter the installation path:"

rem Check whether the installation path exists.
rem If the path already exists ,exit.
rem if no exists,creation installation path.
if exist %I_PATH% goto I_PATH_EXIT
if not exist %I_PATH% goto I_PATH_ONT_EXIT

:I_PATH_EXIT
echo %I_PATH% Already exist, please replace
set /p "I_PATH=Please enter the installation path:"
if exist %I_PATH% goto I_PATH_EXIT
if not exist %I_PATH% goto I_PATH_ONT_EXIT

:I_PATH_ONT_EXIT
mkdir %I_PATH%\app

rem echo Copy the file to the installation directory
rem xcopy directory ,copy file
xcopy %C_PATH%\app %I_PATH%\app /s

@ timeout 3
rem Register as a service
%I_PATH%\app\JavaService.exe -install Hello-test "%I_PATH%\app\java-1.8.0_172\jre\bin\server\jvm.dll" -Xms256m -Xmx1024m -jar -Djava.class.path="%I_PATH%\app\java-1.8.0_172\lib\tools.jar;%I_PATH%\app\helloworld.jar" -start "org.springframework.boot.loader.JarLauncher" -out "%I_PATH%\app\out.log" -err "%I_PATH%\app\err.log" -current "
b68
;%C_PATH%" -auto
@ timeout 5
net start Hello-test

rem Generate configuration file
echo 系统安装路径:%I_PATH%\app > %C_PATH%\confing.txt
echo 系统启动命令:net start Hello-test >> %C_PATH%\confing.txt
echo 系统关闭命令:net stop Hello-test >> %C_PATH%\confing.txt
echo 可以通过服务启动或关闭系统(已经配置开机自启动 )>> %C_PATH%\confing.txt
echo 系统服务卸载命令:sc delete Hello-test >> %C_PATH%\confing.txt
pause
注:-start "org.springframework.boot.loader.JarLauncher" –start 后面的class 为第二步中查到的class名称
六、 在Windows 10操作系统上进行测试(双击start.bat文件)





看到上面的页面,说明服务注册成功,并已经启动,按任意键退出即可。

七、 查看管理说明文档



confing.txt 在成功启动服务后自动生成



八、 查看安装路径文件



其中out.log 文件中记录服务启动日志
九、 参考文档
https://blog.csdn.net/cxj1990year/article/details/41862831
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息