Runtime.getRuntime().exec()执行错误

jovial__ 2008-04-05 01:15:26
请问我这段程序哪儿出错了?为什么不显示结果,字符串s和“error”都不打印出来:

import java.io.IOException;
import java.io.*;

public class compile {
public static void main(String[] args){
try {
InputStream p = Runtime.getRuntime().exec("ipconfig /all").getInputStream();

String s = (new BufferedReader(new InputStreamReader(p))).readLine();

if(s!=null)System.out.println(s);
else System.out.println("error");
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
...全文
175 7 打赏 收藏 转发到动态 举报
写回复
用AI写文章
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
KKK2007 2008-04-05
  • 打赏
  • 举报
回复
JAVA高级群 41677408
jovial__ 2008-04-05
  • 打赏
  • 举报
回复
恩,确实,第一行为空。本来以为至少打印出什么东西的...

谢谢各位了。
梅小西Echo 2008-04-05
  • 打赏
  • 举报
回复
import java.io.IOException; 
import java.io.*;

public class compile {
public static void main(String[] args){
try {
InputStream p = Runtime.getRuntime().exec("cmd /c ipconfig -all").getInputStream();
BufferedReader b = new BufferedReader(new InputStreamReader(p));
String s = "";
while( (s = b.readLine()) != null)
{
System.out.println(s);
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
kokobox 2008-04-05
  • 打赏
  • 举报
回复
呵呵,lz应该循环打印一下。

dracularking 2008-04-05
  • 打赏
  • 举报
回复
system command是没错 只是你获取的方式
第一行本来就是空行
kokobox 2008-04-05
  • 打赏
  • 举报
回复
InputStream p = Runtime.getRuntime().exec("cmd /c ipconfig -all").getInputStream();
kokobox 2008-04-05
  • 打赏
  • 举报
回复
你的命令写错了好像

应该是
InputStream p = Runtime.getRuntime().exec("cmd /c ipconfig /all").getInputStream();

62,623

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧