如何在Java程序里面启动和关掉tomcat
1个回答
2016-07-22
展开全部
package d2008;
import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
public class ControlTomcat {
/**
* @param args
*/
public static void main(String[] args) throws Exception{
// TODO Auto-generated method stub
Process process = Runtime.getRuntime().exec("cmd.exe /c D:\\apache-tomcat-6.0.18\\bin\\startup.bat"); // 调用外部程序
final InputStream in = process.getInputStream();
BufferedReader br=new BufferedReader(new InputStreamReader(in));
StringBuilder buf = new StringBuilder();
String line = null;
while((line = br.readLine()) != null) buf.append(line);
System.out.println("输出结果为:" + buf);
// new Thread(new Runnable() {
// public void run() {
// BufferedReader br = new BufferedReader(new InputStreamReader(in));
// try {
// while(br.readLine() != null) ;
// } catch (IOException e) {
// // TODO Auto-generated catch block
// e.printStackTrace();
// }
// }
// }).start();// 启动单独的线程来清空process.getInputStream()的缓冲区
// InputStream error = process.getErrorStream();
// BufferedReader br = new BufferedReader(new InputStreamReader(error));
// StringBuilder buf = new StringBuilder(); // 保存输出结果流
// String line = null;
// while((line = br.readLine()) != null) buf.append(line); // 循环等待ffmpeg进程结束
// System.out.println("输出结果为:" + buf);
}
}
关键是exe参数命令cmd.exe /c D:\\apache-tomcat-6.0.18\\bin\\startup.bat
如果要停止cmd.exe /c D:\\apache-tomcat-6.0.18\\bin\\shutdown.bat
import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
public class ControlTomcat {
/**
* @param args
*/
public static void main(String[] args) throws Exception{
// TODO Auto-generated method stub
Process process = Runtime.getRuntime().exec("cmd.exe /c D:\\apache-tomcat-6.0.18\\bin\\startup.bat"); // 调用外部程序
final InputStream in = process.getInputStream();
BufferedReader br=new BufferedReader(new InputStreamReader(in));
StringBuilder buf = new StringBuilder();
String line = null;
while((line = br.readLine()) != null) buf.append(line);
System.out.println("输出结果为:" + buf);
// new Thread(new Runnable() {
// public void run() {
// BufferedReader br = new BufferedReader(new InputStreamReader(in));
// try {
// while(br.readLine() != null) ;
// } catch (IOException e) {
// // TODO Auto-generated catch block
// e.printStackTrace();
// }
// }
// }).start();// 启动单独的线程来清空process.getInputStream()的缓冲区
// InputStream error = process.getErrorStream();
// BufferedReader br = new BufferedReader(new InputStreamReader(error));
// StringBuilder buf = new StringBuilder(); // 保存输出结果流
// String line = null;
// while((line = br.readLine()) != null) buf.append(line); // 循环等待ffmpeg进程结束
// System.out.println("输出结果为:" + buf);
}
}
关键是exe参数命令cmd.exe /c D:\\apache-tomcat-6.0.18\\bin\\startup.bat
如果要停止cmd.exe /c D:\\apache-tomcat-6.0.18\\bin\\shutdown.bat
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询