qw7692336
V2EX  ›  问与答

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

  •  
  •   qw7692336 · Mar 29, 2015 · 1968 views
    This topic created in 4081 days ago, the information mentioned may be changed or developed.

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

    1120101929
        1
    1120101929  
       Mar 30, 2015
    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
       Mar 30, 2015
    @1120101929 那C的呢
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Solana   ·   844 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 96821589 · 33ms · UTC 21:02 · PVG 05:02 · LAX 14:02 · JFK 17:02
    ♥ Do have faith in what you're doing.