怎么用Java来实现控制Tomcat启动与停止

2024-12-20 02:31:13
推荐回答(1个)
回答1:

停止
//如不能运行,请正确配置tomcat环境
public class conTomCat{
public void close() throws IOException
{
Process process = Runtime.getRuntime().exec('cmd /c D:\\apache-tomcat-7.0.16\\bin\\shutdown.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);
}
启动
public void start() throws IOException
{
Process process = Runtime.getRuntime().exec('cmd /c D:\\apache-tomcat-7.0.16\\bin\\startup.bat'); // 调用外部程序
final InputStream in = process.ge tInputStream();
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);
}