如何正确使用java中的runtime

package com.test.common;

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

public class TESTRuntime {
	
	public String cmdexeStr = "F:\\tool\\JsonView\\JsonView\\JsonView.exe";
	
	public String startRunTime(){
		try {
			Process proc = Runtime.getRuntime().exec(cmdexeStr);
			ProcessStreamUtil errorProcStream = new ProcessStreamUtil(proc.getErrorStream(),"Error");
			ProcessStreamUtil outputProcStream = new ProcessStreamUtil(proc.getInputStream(),"Output");
			errorProcStream.start();
			outputProcStream.start();
			int num = proc.waitFor();
			System.out.println("------num:"+num);
			proc.destroy();
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return null;
	}
	
	public String reStartRunTime(){
		try {
			Process proc = Runtime.getRuntime().exec(cmdexeStr);
			InputStream errorProcStream = proc.getErrorStream();
			InputStream outputProcStream = proc.getInputStream();
			InputStreamReader errorReader = new InputStreamReader(errorProcStream);
			InputStreamReader outputReader = new InputStreamReader(outputProcStream);
		   BufferedReader errorBr = new BufferedReader(errorReader);
		   String errorline = null;
		   while ((errorline = errorBr.readLine()) != null) {
			   System.out.println("-------error:\n"+errorline);
		   }
		   BufferedReader outputBr = new BufferedReader(outputReader);
		   String outputline = null;
		   while ((outputline = outputBr.readLine()) != null) {
			   System.out.println("-------output:\n"+outputline);
		   }
		   int num = proc.waitFor();
		   System.out.println("------num:"+num);
		   proc.destroy();
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return null;
	}
	
	public static void main(String[] args) { 
		System.out.println("------------start run--------------");
		TESTRuntime test = new TESTRuntime();
		test.reStartRunTime();
		System.out.println("------------end run--------------");
	}

}

你可能感兴趣的:(如何正确使用java中的runtime)