代码如下,请高人指点,在output.readLine()无限等待,不知何故.
public int runCommand(String command)throws IOException {
try {
logger.debug("enter OPMNUtil runCommand....");
this.command=command;
logger.debug("command----\n"+this.command);
Process process = Runtime.getRuntime().exec(this.command);
BufferedReader output = new BufferedReader(new InputStreamReader(process.getInputStream()));
BufferedReader error = new BufferedReader(new InputStreamReader(process.getErrorStream()));
String line=null;
while((line=output.readLine())!=null)
{
logger.debug("output is----"+line);
}
while((line=error.readLine())!=null)
{
logger.debug("error is----"+line);
}
int exitValue=process.exitValue();
//int exitValue = process.waitFor();
logger.debug("waitcode---------"+exitValue);
return exitValue;
}
catch(IOException e){
throw new IOException();
}
catch (Exception e) {
e.printStackTrace();
return -1000;
}
}
public int runCommand(String command)throws IOException {
try {
logger.debug("enter OPMNUtil runCommand....");
this.command=command;
logger.debug("command----\n"+this.command);
Process process = Runtime.getRuntime().exec(this.command);
BufferedReader output = new BufferedReader(new InputStreamReader(process.getInputStream()));
BufferedReader error = new BufferedReader(new InputStreamReader(process.getErrorStream()));
String line=null;
while((line=output.readLine())!=null)
{
logger.debug("output is----"+line);
}
while((line=error.readLine())!=null)
{
logger.debug("error is----"+line);
}
int exitValue=process.exitValue();
//int exitValue = process.waitFor();
logger.debug("waitcode---------"+exitValue);
return exitValue;
}
catch(IOException e){
throw new IOException();
}
catch (Exception e) {
e.printStackTrace();
return -1000;
}
}
我试了一下没有问题呀
不过好象stdout与stderr不会同时输出
不过我觉得最好还是专门用一个Thread来接收err的流
{ public static void main(String[] args)
{
Run r = new Run();
r.runCommand("opmnctl");
} public int runCommand(String command) {
try {
System.out.println("enter runCommand...."); System.out.println("command----" + command);
final Process process = Runtime.getRuntime().exec(command); BufferedReader output = new BufferedReader
(new InputStreamReader(process.getInputStream()));
String line=null;
Runnable rr = new Runnable()
{
public void run()
{
try {
BufferedReader error = new BufferedReader
(new InputStreamReader(process.getErrorStream())); String line2 = null; System.out.println("handle err stream");
while((line2=error.readLine())!=null)
{
System.out.println("error is----"+line2);
}
} catch (Exception e) {
e.printStackTrace();
}
}
};
(new Thread(rr)).start(); System.out.println("handle std stream");
while((line=output.readLine())!=null)
{
System.out.println("output is----"+line);
} int exitValue=process.exitValue();
//int exitValue = process.waitFor();
System.out.println("waitcode---------"+exitValue);
return exitValue; }
catch (Exception e) {
e.printStackTrace();
return -1000;
}
}
}另,问一下,你执行“opmnctl”需要多久时间
/*
* Created on 2005-11-29
*
* TODO To change the template for this generated file go to
* Window - Preferences - Java - Code Style - Code Templates
*/import java.io.*;
import java.io.IOException;
import cn.zyxel.util.LogHelper;/**
* @author Simon
*
* TODO class OPMNUtil aims to deal with OPMNCTL command
*/
public final class OPMNUtil {
//command to run with OPMNCTL
private String command;
//singleton instance
private static OPMNUtil opmnUtil;
private OPMNUtil(){
}
public static OPMNUtil getInstance(){
if(opmnUtil==null){
opmnUtil=new OPMNUtil();
}
return opmnUtil;
}
/**run arbitary OPMNCTL command
* @param command
* @return
* @throws IOException
*/
public int runCommand(String command)throws IOException {
try { LogHelper.logger.debug("enter OPMNUtil runCommand....");
this.command=command;
LogHelper.logger.debug("command----\n"+this.command);
Runtime rt = Runtime.getRuntime();
Process proc = rt.exec(this.command);
StreamGobbler errorGobbler = new
StreamGobbler(proc.getErrorStream(), "ERROR");
StreamGobbler outputGobbler = new
StreamGobbler(proc.getInputStream(), "OUTPUT");
errorGobbler.start();
outputGobbler.start();
int exitVal = proc.waitFor();
LogHelper.logger.debug("ExitValue:" + exitVal);
return exitVal;
}
catch(IOException e){
throw new IOException();
}
catch (Throwable t) {
t.printStackTrace();
return -1000;
}
}
/**get OPMN managed component's status
* @param componentType
* @param processType
* @return
* @throws IOException
*/
public String getComponentStatus(String componentType,String processType)throws IOException{
String status=Constants.EXCEPTION_STATUS;
try {
LogHelper.logger.debug("enter OPMNUtil getComponentStatus....");
this.command="opmnctl status ias-component="+componentType+" process-type="+processType;
LogHelper.logger.debug("command----\n"+this.command);
Process process = Runtime.getRuntime().exec(this.command);
BufferedReader output = new BufferedReader(new InputStreamReader(process.getInputStream()));
BufferedReader error = new BufferedReader(new InputStreamReader(process.getErrorStream()));
String line=null;
while((line=output.readLine())!=null) {
if(line.indexOf(processType)!=-1){
status=line.substring(line.lastIndexOf("|")+1,line.length()).trim();
}
}
while((line=error.readLine())!=null) {
}
int exitValue = process.waitFor();
LogHelper.logger.debug("exitValue---------"+exitValue);
return status;
}
catch(IOException e){
throw new IOException();
}
catch (Exception e) {
e.printStackTrace();
return Constants.EXCEPTION_STATUS;
}
}
}/**
* @author Simon
*
* TODO class StreamGobbler aims to kick off stdout,stderr output.
* So that OPMNUtil will work normally.
*/
class StreamGobbler extends Thread
{
private InputStream is;
private String type; StreamGobbler(InputStream is, String type){
this.is = is;
this.type = type;
} public void run() {
try{
InputStreamReader isr = new InputStreamReader(is);
BufferedReader br = new BufferedReader(isr);
String line=null;
while ( (line = br.readLine()) != null) {
LogHelper.logger.debug(type + ">" + line);
System.out.flush();
}
} catch (IOException e){
e.printStackTrace();
}
}
}