import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
/**
* tomcat 啓動,關閉,重啓,檢查
* @author xuliangjun
*
*/
public class DoTomcat {
public DoTomcat() {
}
public static void stop() throws IOException{
Process process = Runtime.getRuntime().exec("cmd.exe /c F:\\apache-tomcat-7.0.52\\apache-tomcat-7.0.52\\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 static void start() throws IOException {
StringBuilder buf;
Runtime runtime = Runtime.getRuntime();
Process process = runtime.exec( "cmd.exe /c F:\\apache-tomcat-7.0.52\\apache-tomcat-7.0.52\\bin\\startup.bat");
final InputStream in = process.getInputStream();
BufferedReader br=new BufferedReader(new InputStreamReader(in));
buf = new StringBuilder();
String line = null;
while((line = br.readLine()) != null){
buf.append(line);
}
System.out.println("輸出結果爲:" + buf);
}
public static void restar() throws Exception{
stop();
Thread.sleep(300);
start();
}
public static void main(String[] args) throws Exception{
// start();
tomcatstate();
// restar();
}
/**
* 項目裏添加一個頁面 tomcatestae.jsp,這個頁面就一句話TOMCAT-STATR-SUCCESS,訪問此頁面來判斷tomcat是否啓動成功
* @throws IOException
*/
public static void tomcatstate() throws IOException {
URL url = null;
InputStream is = null;
StringBuffer resultBuffer = new StringBuffer();
BufferedReader breader = null;
try {
url = new URL("http://127.0.0.1:8080/zhadmin/tomcatestae.jsp");
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
is = conn.getInputStream();
breader = new BufferedReader(new InputStreamReader(is));
String line = "";
while ((line = breader.readLine()) != null) {
resultBuffer.append(line);
}
} catch (MalformedURLException e) {
e.printStackTrace();
} finally {
if (breader != null)
breader.close();
if (is != null)
is.close();
}
String result=resultBuffer.toString();
if("TOMCAT-STATR-SUCCESS".equals(result.trim())){
System.out.println("tomcat 啓動成功,你如今能夠激情的訪問了");
}
}
}
java