public static void main(String[] args) {
String command = "D:\\awgytools\\apache-tomcat-8.0.44\\bin\\shutdown.bat";//关闭tomcat命令
// String command = "D:\\awgytools\\apache-tomcat-8.0.44\\bin\\startup.bat";//启动tomcat命令
// String command = "D:\\awgytools\\apache-tomcat-8.0.44\\bin\\tomcat7w.exe";
CallTomcat callTomcat = new CallTomcat();
try {
callTomcat.callCommand(command);
// The CATALINA_HOME environment variable is not defined correctly
// This environment variable is needed to run this program
//出现这2行代码的话, 需要通过环境变量, 进行配置,步骤:右击计算机属性->高级系统设置->环境变量->新建->录入变量名:CATALINA_HOME,变量值:D:\awgytools\apache-tomcat-8.0.44
} catch (IOException e) {
System.out.println("执行命令时出错:" + e.getMessage());
}
}
private 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);
}