简体   繁体   English

在Java中运行系统命令

[英]Run system commands in java

I am a little confused about why I can't run the following command ls -l If I run ls or pwd it works fine. 我对为什么不能运行以下命令ls -l感到有些困惑,如果我运行lspwd它可以正常工作。

Am I missing something? 我想念什么吗?

    ProcessBuilder pb = new ProcessBuilder("ls -l");
    pb.redirectErrorStream(true);
    Process process = pb.start();

    InputStream is = process.getInputStream();
    InputStreamReader isr = new InputStreamReader(is);
    BufferedReader br = new BufferedReader(isr);

    String line;
    while ( (line = br.readLine()) != null) {
        System.out.println(line);
    }
    br.close();

One more question: How can I run multiple system commands concurrently? 另一个问题:如何同时运行多个系统命令? Using while loop or for loops will run the command one by one. 使用while循环或for循环将逐个运行命令。 Any advice? 有什么建议吗?

Thanks in advance. 提前致谢。

Change: 更改:

new ProcessBuilder("ls -l");

To: 至:

new ProcessBuilder("ls", "-l");
    String[] st = {"ls", "bin"};
    ProcessBuilder pb = new ProcessBuilder(st);
    pb.redirectErrorStream(true);
    Process process = pb.start();

    InputStream is = process.getInputStream();
    InputStreamReader isr = new InputStreamReader(is);
    BufferedReader br = new BufferedReader(isr);

    String line;
    while ( (line = br.readLine()) != null) {
        System.out.println(line);
    }
    br.close();  

Using while loop or for loops will run the command one by one. 使用while循环或for循环将逐个运行命令。

Only when you are doing the whole start-then-read-stdout business for each one of them, one-by-one. 仅当您逐一完成整个“ 开始-然后-读-输出”业务时。 The processes are indeed run in parallel, it's just the reading part that's stopping you from running them concurrently. 这些过程确实是并行运行的,只是阅读部分使您无法并行运行它们。 All you need to do is breaking the start and read into two parts: 您需要做的就是从头开始,并将其分为两部分:

    Stream.of(Arrays.asList("ls", "-l"),
              Arrays.asList("python", "-h"),
              Arrays.asList("df"))
            .map(cmd->{
                // Create a process for each command, but don't read the output
                try {
                    return new AbstractMap.SimpleImmutableEntry<>(cmd,
                            new ProcessBuilder(cmd)
                                    .redirectErrorStream(true)
                                    .start().getInputStream());
                } catch (IOException e) {
                    e.printStackTrace();
                    return null;
                }
            })
            .filter(p->p!=null)
            .parallel()
            .forEach(in->{
                // Read and print STDOUT for each process.
                try (BufferedReader reader = new BufferedReader(new InputStreamReader(in.getValue()))){
                    String line;
                    while ((line = reader.readLine()) != null) {
                        System.out.printf("%20s: %s\n", in.getKey(), line);
                    }
                } catch (IOException e) {
                    e.printStackTrace();
                }
            });

The parallel() call is making the output really hard to read, it's there only to demonstrate that the processes are really running concurrently. parallel()调用确实使输出很难阅读,它只是用来证明进程实际上是并发运行的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM