在网上常见的用java调用外部命令返回结果的方法是:
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();
}
}
}
Java Asp PHP .Net XML C/C++ CGI VB Jsp J2ee J2se J2me EJB Servlet Tomcat Resin Struts Weblogic Eclipse ANT GUI JMS Web servise IDEA Webphere Hibernate Spring Jboss Applet Swing Socket Javamail Perl Ajax P2P 安全 模式 框架 测试 开源 游戏
Windows XP Windows 2000 Windows 2003 Windows Me Windows 9.x Linux UNIX 注册表 操作系统 服务器 应用服务器