
正文
java代码关闭tomcat程序
提示:扫一扫查出行【扫一扫了解最新限行尾号】
复制提示
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); }
}
tomcatStop.java
3.执行关闭命令

4.常见错误
执行代码后可能出现:
The CATALINA_HOME environment variable is not defined correctly
This environment variable is needed to run this program此时需设置环境变量:

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







