V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
V2EX 提问指南
qw7692336
V2EX  ›  问与答

Java 和 C 调用 Linux 命令后,执行内容怎么获取?

  •  
  •   qw7692336 · 2015-03-29 21:41:42 +08:00 · 1522 次点击
    这是一个创建于 3322 天前的主题,其中的信息可能已经有所发展或是发生改变。

    Java用Runtime.getRuntime().exec("echo hello!");
    C用system("echo hello!");
    C会把“hello!”输出到控制台,Java则没有输出。

    1120101929
        1
    1120101929  
       2015-03-30 13:12:27 +08:00
    Runtime runtime = Runtime.getRuntime();
    Process process = runtime.exec("cmd /c dir");
    InputStream in = process.getInputStream();
    Scanner scanner = new Scanner(in);
    while (scanner.hasNext()) {
    String line = scanner.nextLine();
    System.out.println(line);
    }

    scanner.close();
    in.close();
    qw7692336
        2
    qw7692336  
    OP
       2015-03-30 17:00:56 +08:00
    @1120101929 那C的呢
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   2418 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 28ms · UTC 13:31 · PVG 21:31 · LAX 06:31 · JFK 09:31
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.