使用java代码重启tomcat

private static    String location="F:\apache-tomcat-7.0.62";

private static void executeCmd(String location) {
        System.out.println(location);
        Runtime run = Runtime.getRuntime();
        try {
            Process ps = run.exec("" + location + "\bin\restart.bat");
            // 我很奇怪 下面的代码去掉的话 tomcat的黑框就不能出现
            BufferedReader br = new BufferedReader(new InputStreamReader(ps.getInputStream(), "GBK"));// 注意中文编码问题
            String line;
            while ((line = br.readLine()) != null) {
                System.out.println("StartedLog==>" + line);
            }
            br.close();
        } catch (Exception e) {
            e.printStackTrace();
        }

    }
    
    private static void createCmdFile(String location) {  
        File f = new File(location + "\bin\restart.bat");  
        try {  
            FileWriter fw = new FileWriter(f);  
            BufferedWriter bw = new BufferedWriter(fw);  
             //下面的必须加上  
            bw.write("set CATALINA_HOME=" + location);  
            bw.newLine();  
            bw.write("call " + location + "\bin\shutdown.bat");  
            bw.newLine();  
            bw.write(" ping 127.0.0.1 -n 5  1>nul ");  
            bw.newLine();  
            bw.write("call " + location + "\bin\startup.bat ");  
  
            bw.close();  
            fw.close();  
        } catch (Exception e1) {  
            // TODO Auto-generated catch block  
            e1.printStackTrace();  
        }  
  
    }  
原文地址:https://www.cnblogs.com/kasher/p/7513818.html