【Java】使用程序调用控制tomcat启动

2017-04-13 10:10

公司准备使用一部机子管理程序启动,减轻程序员维护的时间

所以,最近研究了一下,写了个简单的例子,引砖一下大笑


import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
 
//java程序控制tomcat启动
//如不能运行,请正确配置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.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 static void main(String[] args) throws Exception{
  // TODO Auto-generated method stub
  conTomCat con=new conTomCat();
  con.start();
 
 }
}