当前页面: 开发资料首页 → Java 专题 → 使用ProcessBuilder调用外部命令,并返回大量结果
摘要: 使用ProcessBuilder调用外部命令,并返回大量结果
process =runtime.exec(cmd)
is = process.getInputStream();
isr=new InputStreamReader(is);
br =new BufferedReader(isr);
while( (line = br.readLine()) != null )
{
out.println(line);
out.flush();
}
import java.io.*;
/**
*
* @author tyrone
*
*/
public class CMDExecute
{
/**
* @param cmd
* @return
* @throws IOException
*/
public synchronized String run(String[] cmd,String workdirectory) throws IOException{
String line=null;
String result="";
try {
ProcessBuilder builder = new ProcessBuilder(cmd);
//set working directory
if (workdirectory!=null)
builder.directory(new File(workdirectory));
builder.redirectErrorStream(true);
Process process = builder.start();
InputStream in=process.getInputStream();
byte[] re=new byte[1024];
while (in.read(re)!= -1) {
System.out.println(new String(re));
result = result + new String(re);
}
in.close();
} catch (Exception ex) {
ex.printStackTrace();
}
return result;
}
/**
* @param args=cvs log */
public static void main(String[] args){
String result=null;
CMDExecute cmdexe=new CMDExecute();
try {
result= cmdexe.run(args,"D:\\MyProject\\colimas\\axis_c");
System.out.println(result);
}catch ( IOException ex ){
ex.printStackTrace();
}
}
}
process =runtime
↑返回目录
前一篇: Eclipse使用技巧之插件管理
后一篇: J2SE发展演变史