在Windows部署Java的Jar包
阿里云国内75折 回扣 微信号:monov8 |
阿里云国际,腾讯云国际,低至75折。AWS 93折 免费开户实名账号 代冲值 优惠多多 微信号:monov8 飞机:@monov6 |
背景
使用 Java 编写了一些有用的工具因为不方便部署到服务器上所以需要把 Java 生成的 jar 包在本地 Windows 上部署。
查阅了几种部署方式认为通过 winsw 进行部署最方便。
安装 winsw
进入 winsw 的下载页面下载 sampleminimal.xml 和 WinSW-x64.exe 文件。如
配置 winsw 部署 jar 包
1、将 jar 包和刚下载的 sampleminimal.xml 和 WinSW-x64.exe 文件放置到同一个目录
2、把 sampleminimal.xml 和 WinSW-x64.exe 文件修改为同一个名称建议使用『项目名 + Service』的命名方式。如localUtilsService.xml, localUtilsService.exe
3、修改 xml 文件配置如下
<configuration>
<!-- 唯一ID -->
<id>localUtilsId</id>
<!-- 服务名称启动服务时使用 -->
<name>localUtilsService</name>
<description>This is local utils</description>
<executable>java</executable>
<arguments>-jar local-utils-0.0.1.jar</arguments>
<!-- 开机启动 -->
<startmode>Automatic</startmode>
<!-- 日志配置 -->
<logpath>%BASE%\logs</logpath>
<logmode>rotate</logmode>
</configuration>
注意
- 在 logpath 中不使用
%Base%
相对路径会导致 jar 包的日志不输出 - 在 arguments 中还可以指定虚拟机参数配置文件路径等。如
<arguments>-Xms256m -Xmx256m -jar "%BASE%\demo.jar" -Dspring.config.additional-location=application-db.properties</arguments>
4、安装或卸载 Windows 服务
- 使用管理员身份运行 Powershell并进入到 jar 包所在目录如
cd D:\project
- 在 Powershell 通过以下命令安装、卸载服务
# 安装服务
./localUtilsService.exe install
# 卸载服务需要先停止服务
./localUtilsService.exe uninstall
注意没有使用管理员身份运行 Powershell在执行安装时会申请权限。
5、 启动或停止 Windows 服务
- 通过命令进行管理脚本如下
# 启动服务
net start localUtilsService
# 停止服务
net stop localUtilsService
# winsw 命令行帮助手册
.\localUtilsService.exe /?
- 或者进入 Windows 服务管理器找到对应服务进行管理win+r 快捷键输入services.msc即可打开如图
认识作者
本文首发于微信公众号『守厚』欢迎关注留言