【Java】使用程式呼叫控制tomcat啟動
阿新 • • 發佈:2019-01-01
公司準備使用一部機子管理程式啟動,減輕程式設計師維護的時間
所以,最近研究了一下,寫了個簡單的例子,引磚一下
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(); } }