jar.bat
- 注意保存是编码格式选择ANSI(cmd不会乱码)
- 如果想在 utf-8模式下编辑 那就在开头加入
CHCP 65001
- 如果想在 utf-8模式下编辑 那就在开头加入
如果配置了 log 并且默认存储在jar根目录下,那使用bat启动,日志文件可能回在bat所在的目录生成
所以 jar 时要写全路径 或则cd到 jar文件下 运行
- 直接jar启动时会在jar所在目录生成
-
start & stop.bat
```basicjar_stop_start.bat
rem CHCP 65001 @echo off @echo 先关闭进程 title xx color 0c rem 设置监听的端口号 set port=1256 rem 尽量写全路径 set jarName=test.jar echo port : %port% set PROJECT_PROPERTIES=prod rem -XX:+UseConcMarkSweepGC -XX:+UseParNewGC || -XX:+UseParallelGC -XX:+UseParallelOldGC set JAVA_OPTS= -Xms1024m set JAVA_OPTS= %JAVA_OPTS% -Xmx1024m -Xmn512m -XX:+UseParallelGC -XX:+UseParallelOldGC set JAVA_OPTS= %JAVA_OPTS% -Dfile.encoding=utf-8 set SERVER_PORT= —server.port=%port% set SERVER_PROPERTIES= —spring.profiles.active=%PROJECT_PROPERTIES%
for /f “usebackq tokens=1-5” %%a in (netstat -ano ^| findstr %port%
) do (
if [%%d] EQU [LISTENING] (
set pid=%%e
)
)
for /f “usebackq tokens=1-5” %%a in (tasklist ^| findstr %pid%
) do (
set image_name=%%a
)
echo now will kill process : pid %pid%, image_name %image_name% rem 根据进程ID,kill进程 taskkill /f /pid %pid% @echo %pid%的进程停止成功 @echo 启动%jarName% set curdir=%~dp0 cd /d %curdir% echo %curdir% java %JAVA_OPTS% -jar %jarName% %SERVER_PORT% %SERVER_PROPERTIES% pause
<a name="IuJEI"></a>
## stop.bat
```bash
rem CHCP 65001
@echo off
@echo 根据端口关闭进程
rem 设置监听的端口号
rem set port=1256
set /p port=端口号:
echo port : %port%
for /f "usebackq tokens=1-5" %%a in (`netstat -ano ^| findstr %port%`) do (
if [%%d] EQU [LISTENING] (
set pid=%%e
)
)
for /f "usebackq tokens=1-5" %%a in (`tasklist ^| findstr %pid%`) do (
set image_name=%%a
)
echo now will kill process : pid %pid%, image_name %image_name%
rem 根据进程ID,kill进程
taskkill /f /pid %pid%
@echo %pid%的进程停止成功
start.bat
CHCP 65001
@echo off
@echo 先关闭进程
title xx
color 0c
rem 设置监听的端口号
set port=1256
rem 尽量写全路径
set jarName=test.jar
echo port : %port%
set PROJECT_PROPERTIES=prod
rem -XX:+UseConcMarkSweepGC -XX:+UseParNewGC || -XX:+UseParallelGC -XX:+UseParallelOldGC
set JAVA_OPTS= -Xms1024m
set JAVA_OPTS= %JAVA_OPTS% -Xmx1024m -Xmn512m -XX:+UseParallelGC -XX:+UseParallelOldGC
set JAVA_OPTS= %JAVA_OPTS% -Dfile.encoding=utf-8
set SERVER_PORT= --server.port=%port%
set SERVER_PROPERTIES= --spring.profiles.active=%PROJECT_PROPERTIES%
@echo 启动%jarName%
set curdir=%~dp0
cd /d %curdir%
echo %curdir%
java %JAVA_OPTS% -jar %jarName% %SERVER_PORT% %SERVER_PROPERTIES%
pause
all_start.bat启动n个bat
写 (start jar_1.bat) bat的全路径,防止log文件夹乱生成
- 用于一个大的bat启动 n个bat ```basic @echo off set paths=C:\xx\jar
@echo 启动file cd /d %paths%\file start file-jar_start.bat
@echo 启动xx cd /d %paths%\swupl start xx-jar_start.bat
@echo 启动job ping -n 20 127.0.0.1>nul cd /d %paths%\etl start job-jar_start.bat
<a name="x1Qho"></a>
## 自启说明
- 把all_start.bat 复制一个到 `C:\ProgramData\Microsoft\Windows\Start Menu\Programs\StartUp 目录下`,让他开机启动all_start.bat
- **此方法重启后必须要登录进系统自启才生效**
- `shell:startup 快速打开自启文件夹`
- windows server 利用服务器管理器工具进行脚本自启
- ![image.png](https://cdn.nlark.com/yuque/0/2021/png/1642320/1630905886611-3c6a461d-d20d-43d9-abea-0c6eaac7740b.png#clientId=u88ccb97f-5814-4&from=paste&height=1061&id=ufa54814c&margin=%5Bobject%20Object%5D&name=image.png&originHeight=1061&originWidth=1736&originalType=binary&ratio=1&size=989597&status=done&style=none&taskId=uaa59448a-84de-4ab6-8eaf-56bc03dc6e2&width=1736)
- ![image.png](https://cdn.nlark.com/yuque/0/2021/png/1642320/1630905919845-52576c6f-c4bf-45f1-bba0-57b3c94ac4d8.png#clientId=u88ccb97f-5814-4&from=paste&height=497&id=ue3b18794&margin=%5Bobject%20Object%5D&name=image.png&originHeight=497&originWidth=1762&originalType=binary&ratio=1&size=443218&status=done&style=none&taskId=u426c9716-3a7c-40b0-9bae-dfc288dc6d1&width=1762)
- ![image.png](https://cdn.nlark.com/yuque/0/2021/png/1642320/1630905968709-462d7b65-b122-4dd8-8c81-bf3be64f2d9f.png#clientId=u88ccb97f-5814-4&from=paste&height=740&id=uee674c85&margin=%5Bobject%20Object%5D&name=image.png&originHeight=740&originWidth=872&originalType=binary&ratio=1&size=295588&status=done&style=none&taskId=ua98b4b13-c1f2-44f8-9874-f41e9843505&width=872)
- ![image.png](https://cdn.nlark.com/yuque/0/2021/png/1642320/1630906028646-1f5b2b7c-47fa-4eea-ad9a-99987cbe1edb.png#clientId=u88ccb97f-5814-4&from=paste&height=725&id=u0da40451&margin=%5Bobject%20Object%5D&name=image.png&originHeight=725&originWidth=1306&originalType=binary&ratio=1&size=456822&status=done&style=none&taskId=u0353605b-7f3f-4bd4-875f-2ff9211efe4&width=1306)
- ![image.png](https://cdn.nlark.com/yuque/0/2021/png/1642320/1630906137979-0740f3f4-7990-4d9e-845c-a770e99357e2.png#clientId=u88ccb97f-5814-4&from=paste&height=773&id=u554803ee&margin=%5Bobject%20Object%5D&name=image.png&originHeight=773&originWidth=1320&originalType=binary&ratio=1&size=410733&status=done&style=none&taskId=u1be11916-c000-4087-a64e-57517a70eb4&width=1320)
<a name="rIscn"></a>
# 参考资料
```shell
cd d: ##并不能切换到d盘,只是显示d:\目录
cd d:\ ##不能切换到d盘,无动作
cd /d d: ##切换到d盘根目录
d: ##切换到d盘根目录
@echo off
echo 当前盘符:%~d0
echo 当前盘符和路径:%~dp0
echo 当前盘符和路径的短文件名格式:%~sdp0
echo 当前批处理全路径:%~f0
echo 当前CMD默认目录:%cd%
pause