欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

Java 运行 bat 文件

程序员文章站 2022-07-12 23:39:18
...

     项目需要通过web页面控制Tomcat6的多实例启动停止等操作,结合网上资料整理的实例以备用。附有源码,请把源码test目录Copy到Tomcat6的安装目录,未测试Tomcat5, Tomcat5.5下的情况。

 

核心类:CmdProcess

 

package com.cmd.test;

import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;

/**
 * 该类启动doc界面
 * @author Administrator
 *
 */
public class CmdProcess extends Thread{
	
	InputStream inputStream;

	public InputStream getInputStream() {
		return inputStream;
	}

	public void setInputStream(InputStream inputStream) {
		this.inputStream = inputStream;
	}

	public CmdProcess() {
		super();
		// TODO Auto-generated constructor stub
	}
	
	public CmdProcess(InputStream inputStream) {
		super();
		this.inputStream = inputStream;
	}

	public void run() {
		// TODO Auto-generated method stub
		try {
			InputStreamReader inputStreamReader = new InputStreamReader(inputStream);
			BufferedReader bufferedReader = new BufferedReader(inputStreamReader);
			String line = null;
			while ((line = bufferedReader.readLine()) != null){
			//while ((bufferedReader.readLine()) != null) {//此处是关键,原因我也不知道,望牛人解答
				System.out.println(line);
			}
			try{				
			}finally{
				if(bufferedReader!=null) bufferedReader.close();
				if(inputStreamReader!=null) inputStreamReader.close();
				if(inputStream!=null) inputStream.close();
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		String command = "D:\\Program Files\\Tomcat6\\minor\\qin\\qin-startup.bat";
		try {
			Process process = Runtime.getRuntime().exec(command);
			CmdProcess cmdProcess = new CmdProcess(process.getInputStream());
			cmdProcess.start();
			process.waitFor();
		}catch(Exception e){
			e.printStackTrace();
		}
	}
}

 启动测试类 StartTest

package com.cmd.test;

/**
 * 启动测试
 * @author Administrator
 *
 */
public class StartTest {

	/**
	 * 启动
	 */
	public void start(){
		String command = "D:\\Program Files\\Tomcat6\\test\\test-startup.bat";
		try {
			Process process = Runtime.getRuntime().exec(command);
			CmdProcess cmdProcess = new CmdProcess(process.getInputStream());
			cmdProcess.start();
			process.waitFor();
			
		}catch(Exception e){
			e.printStackTrace();
		}	
	}
	/**
	 * @param args
	 */
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		new StartTest().start();
	}

}

 

停止测试类 StopTest

package com.cmd.test;

/**
 * 停止测试
 * @author Administrator
 *
 */
public class StopTest {

	/**
	 * 停止
	 */
	public void stop(){
		String command = "D:\\Program Files\\Tomcat6\\test\\test-shutdown.bat";
		try {
			Process process = Runtime.getRuntime().exec(command);
			CmdProcess cmdProcess = new CmdProcess(process.getInputStream());
			cmdProcess.start();
			process.waitFor();
			
		}catch(Exception e){
			e.printStackTrace();
		}
	}
	/**
	 * @param args
	 */
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		new StopTest().stop();
	}

}

 

相关标签: Java Web thread