欢迎您来到GIS动力

加入收藏 免费注册 用户登陆 帮助中心
首页 新闻动态 技术专栏 银杏树下 学习考研 软件下载 求职招聘 许愿瓶 节日祝福 用户中心 精彩推荐 资源搜索 地图
专栏导航: AO开发 | SO开发 | ArcGIS桌面 | 超图桌面 | 开发语言 | 数据库 | WebGIS | 银杏文学 | 研究生考题 | FreeMap 谈天说地
   您现在位于: 首页技术专栏开发语言 → 正文
Java中调用外部命令实现
07-10-13 09:57:02 作者:半块点心 出处:本站原创
Java中调用外部命令 
public clas* **ecCommond{ 
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; 


}
(本文已被浏览 次)
发布人:admin
推荐给好友:发送给好友
上篇新闻:
下篇新闻:
相关评论
发表我的评论
  • 尊重网上道德,遵守《全国人大常委会关于维护互联网安全的决定》及中华人民共和国其他各项有关法律法;
  • 本站有权保留或删除您发表的任何评论内容;
  •   相关文章  

    关于我们 友情链接 ┋ 与我在线 ┋ 管理 ┋ TOP
    网站当前版本:GisPower CMS V3.0
    『GIS 动力』- http://www.gispower.org/
    联系我们:webmaster#gispower.org
    Copyright (c) 2003-2007 GisPOwer.Org. All Rights Reserved.

                   滇ICP备05006901号