当前位置:主页 > 软件编程 > JAVA代码 >

java代码关闭tomcat程序及出现问题解析

时间:2020-10-06 21:54:21 | 栏目:JAVA代码 | 点击:

1.通过java代码实现tomcat的关闭

2.tomcatStop.java

package test;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
public class tomcatStop {
  public static void main(String[] args) {
    // TODO Auto-generated method stub
    System.out.println("访问已超出日访问量");
    String command = "D:\\apache-tomcat-8.5.39\\bin\\shutdown.bat";// 关闭tomcat命令
    try {
      callCommand(command);
    } catch (IOException e) {
      System.out.println("执行命令时出错:" + e.getMessage());
    }
  }
  public static void callCommand(String command) throws IOException {
    Runtime runtime = Runtime.getRuntime();// 返回与当前的Java应用相关的运行时对象
    // 指示Java虚拟机创建一个子进程执行指定的可执行程序,并返回与该子进程对应的Process对象实例
    Process process = runtime.exec(command);
    runtime.gc();// 运行垃圾回收器
    String line = null;
    String content = "";
    BufferedReader br = new BufferedReader(new InputStreamReader(process.getInputStream()));
    while ((line = br.readLine()) != null) {
      content += line + "\r\n";
    }
    System.out.println(content);
  }
}

3.执行关闭命令

4.常见错误

执行代码后可能出现:

The CATALINA_HOME environment variable is not defined correctly

This environment variable is needed to run this program

此时需设置环境变量:

本人设置后运行依然报错,但是将电脑注销一下再次运行即可。

总结

您可能感兴趣的文章:

相关文章