·您的位置: 首页 » 资源教程 » 编程开发 » JAVA、JSP » Java中调用外部命令

Java中调用外部命令

类别: JAVA教程  评论数:0 总得分:0
Java中调用外部命令
public class ExecCommond{
public ExecCommond(){}
/**
* 执行一条命令
* @param execStr String 命令字符串
* @return String 执行命令错误时的信息。
*/
public static String exec(String execStr) {
Runtime runtime = Runtime.getRuntime(); 取得当前运行期对象
String outInfo=\"\"; //执行错误的输出信息
try {
String[] args = new String[] {\"sh\", \"-c\", execStr};//执行linux下的命令
//执行windows下的命令
// String[] args = new String[] {\"cmd\", \"-c\", execStr};
Process proc = runtime.exec(args); //启动另一个进程来执行命令
InputStream in = proc.getErrorStream();//得到错误信息输出。
BufferedReader br = new BufferedReader(new InputStreamReader(in));
String line = \"\";
while ( (line = br.readLine())
!= null) {
outInfo = outInfo + line + \"/n\";
System.out.println(outInfo);
}
// 检查命令是否失败。
 
try {
if (proc.waitFor() != 0) {
System.err.println(\"exit value = \" +
proc.exitValue());
}
}
catch (InterruptedException e) {
System.err.print(e);
e.printStackTrace();
}
}
catch (IOException e) {
flag = false;
System.out.println(\"exec error: \" + e.getMessage());
e.printStackTrace();
}
finally {
return outInfo;
}
}
}




-= 资 源 教 程 =-
文 章 搜 索
关键词:
类型:
范围:
纯粹空间 softpure.com
Copyright © 2006-2008 暖阳制作 版权所有
QQ: 15242663 (拒绝闲聊)  Email: faisun@sina.com
 纯粹空间 - 韩国酷站|酷站欣赏|教程大全|资源下载|免费博客|美女壁纸|设计素材|技术论坛   Valid XHTML 1.0 Transitional
百度搜索 谷歌搜索 Alexa搜索 | 粤ICP备19116064号-1