windows 下tomcat重启脚本

@echo off&setlocal enabledelayedexpansion
@rem (start /min cmd.exe /c %0 :&exit)
start /min 
title Tomcat 重启脚本%1
cd ..
set CATALINA_HOME=%cd%
set STR_TASK=%1
set JAVA_HOME=%CATALINA_HOME%injdk1.7.0_55
cd /D %CATALINA_HOME%in
call %CATALINA_HOME%inshutdown.bat
ping -n 3 localhost >nul
wmic process where name="java.exe" get processid,commandline |findstr /i %STR_TASK%  >#
for /f "delims=*" %%i in (#) do (      
   set var=%%i
   set var=!var:start= #!
   for /f "tokens=3 delims=#" %%a in ("!var!") do (set tomcatpid=%%a)
)
del # >nul
if defined tomcatpid taskkill /pid !tomcatpid!
ping -n 3 localhost >nul
call %CATALINA_HOME%instartup.bat

taskkill /im cmd.exe

由于windows的任务不好锁定,所以执行的时候,需要传入tomcat的主目录的名称用来锁定进程号,保证运行的tomcat中没有重复的文件名。

下面是我代码的调用示例

String cmdcommand = "";
            String osname = System.getProperties().getProperty("os.name").toLowerCase();
            File  partfile = new File(new  File(ServerDirUtils.getServerRoot()).getParent());
            String rootdirpath = partfile.getParent();
            String rootname = partfile.getParentFile().getName();
            if(!osname.toLowerCase().contains("windows")){
                String rootPath = rootdirpath +File.separator+"bin"+File.separator+"restart.sh";
                cmdcommand = "sh "+rootPath+   "  "+rootname;
            }else{
                String rootPath = rootdirpath+File.separator+"bin"+File.separator+"restart.bat";
                cmdcommand = "cmd /c start "+rootPath+"  "+rootname;
            }
            logger.error("执行的命令为:"+cmdcommand);
            try {
                Runtime.getRuntime().exec(cmdcommand);
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }

如假如我的tomcat的目录为D:zhkapache-tomcat-8.5.39

那命令的组成为:

cmdcommand = "cmd /c start  【你的重启bat的所在的位置,建议放在bin目录下】  apache-tomcat-8.5.39

原文地址:https://www.cnblogs.com/zhanghongke/p/13032453.html