jar.bat

  • 注意保存是编码格式选择ANSI(cmd不会乱码)
    • image.png
    • 如果想在 utf-8模式下编辑 那就在开头加入 CHCP 65001
  • 如果配置了 log 并且默认存储在jar根目录下,那使用bat启动,日志文件可能回在bat所在的目录生成

    所以 jar 时要写全路径 或则cd到 jar文件下 运行

    • 直接jar启动时会在jar所在目录生成
    • image.png
    • image.png

      start & stop.bat

      ```basic

      jar_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

  1. <a name="IuJEI"></a>
  2. ## stop.bat
  3. ```bash
  4. rem CHCP 65001
  5. @echo off
  6. @echo 根据端口关闭进程
  7. rem 设置监听的端口号
  8. rem set port=1256
  9. set /p port=端口号:
  10. echo port : %port%
  11. for /f "usebackq tokens=1-5" %%a in (`netstat -ano ^| findstr %port%`) do (
  12. if [%%d] EQU [LISTENING] (
  13. set pid=%%e
  14. )
  15. )
  16. for /f "usebackq tokens=1-5" %%a in (`tasklist ^| findstr %pid%`) do (
  17. set image_name=%%a
  18. )
  19. echo now will kill process : pid %pid%, image_name %image_name%
  20. rem 根据进程ID,kill进程
  21. taskkill /f /pid %pid%
  22. @echo %pid%的进程停止成功

start.bat

  1. CHCP 65001
  2. @echo off
  3. @echo 先关闭进程
  4. title xx
  5. color 0c
  6. rem 设置监听的端口号
  7. set port=1256
  8. rem 尽量写全路径
  9. set jarName=test.jar
  10. echo port : %port%
  11. set PROJECT_PROPERTIES=prod
  12. rem -XX:+UseConcMarkSweepGC -XX:+UseParNewGC || -XX:+UseParallelGC -XX:+UseParallelOldGC
  13. set JAVA_OPTS= -Xms1024m
  14. set JAVA_OPTS= %JAVA_OPTS% -Xmx1024m -Xmn512m -XX:+UseParallelGC -XX:+UseParallelOldGC
  15. set JAVA_OPTS= %JAVA_OPTS% -Dfile.encoding=utf-8
  16. set SERVER_PORT= --server.port=%port%
  17. set SERVER_PROPERTIES= --spring.profiles.active=%PROJECT_PROPERTIES%
  18. @echo 启动%jarName%
  19. set curdir=%~dp0
  20. cd /d %curdir%
  21. echo %curdir%
  22. java %JAVA_OPTS% -jar %jarName% %SERVER_PORT% %SERVER_PROPERTIES%
  23. 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

  1. <a name="x1Qho"></a>
  2. ## 自启说明
  3. - 把all_start.bat 复制一个到 `C:\ProgramData\Microsoft\Windows\Start Menu\Programs\StartUp 目录下`,让他开机启动all_start.bat
  4. - **此方法重启后必须要登录进系统自启才生效**
  5. - `shell:startup 快速打开自启文件夹`
  6. - windows server 利用服务器管理器工具进行脚本自启
  7. - ![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)
  8. - ![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)
  9. - ![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)
  10. - ![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)
  11. - ![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)
  12. <a name="rIscn"></a>
  13. # 参考资料
  14. ```shell
  15. cd d: ##并不能切换到d盘,只是显示d:\目录
  16. cd d:\ ##不能切换到d盘,无动作
  17. cd /d d: ##切换到d盘根目录
  18. d: ##切换到d盘根目录
  19. @echo off
  20. echo 当前盘符:%~d0
  21. echo 当前盘符和路径:%~dp0
  22. echo 当前盘符和路径的短文件名格式:%~sdp0
  23. echo 当前批处理全路径:%~f0
  24. echo 当前CMD默认目录:%cd%
  25. pause