简体   繁体   English

Java ProcessBuilder从Linux终端命令获取输出

[英]Java ProcessBuilder get Output from Linux Terminal Commands

I want to communicate with the linux terminal with java code. 我想用Java代码与linux终端通信。 I want to store the output and work with it. 我想存储输出并使用它。 I implemented the code below following the instructions in given link. 我按照给定链接中的说明实施了以下代码。

I would expect the complete terminal output of help. 我希望完整的终端输出帮助。 But neither for the standard commands, nor for the mosquitto commands I get anything from the input stream. 但是无论对于标准命令还是mo​​squitto命令,我都无法从输入流中得到任何东西。 Where is the mistake? 错误在哪里? Or are you supposed to do it completely differnt? 还是应该完全不同?

Stackoverflow link: how to run a command at terminal from java program? StackOverflow链接: 如何从Java程序在终端上运行命令?

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;

public class LinuxInputStream
{
    public static void main(String[] args)
    {
        // Linux Terminal
        String prefix = "/bin/bash";
        String terminalCommand = "help";
        String mosquittoCommand = "mosquitto --help";

        ProcessBuilder pb1 = new ProcessBuilder(
                new String[] {prefix, terminalCommand});
        ProcessBuilder pb2 = new ProcessBuilder(
                new String[] {prefix, mosquittoCommand});

        try
        {
            executeCommand(pb1);
        }
        catch (IOException e)
        {
            System.out.println("IO Error in Terminal Command execution!");
            e.printStackTrace();
        }
        try
        {
            executeCommand(pb2);
        }
        catch (IOException e)
        {
            System.out.println("IO Error in Mosquitto Command execution!");
            e.printStackTrace();
        }

    }

    private static void executeCommand(ProcessBuilder pb) throws IOException
    {
        Process terminalCommandProcess = pb.start();
        InputStream inputStream = terminalCommandProcess.getInputStream();
        BufferedReader br = new BufferedReader(
                new InputStreamReader(inputStream));
        String line;
        int i = 0;
        while ((line = br.readLine()) != null)
        {
            System.out.println("Line: " + line);
            i++;
        }
        if (i == 0) System.out.println("Nothing read from input stream");
    }
}

Output: 输出:

Nothing read from input stream 没有从输入流中读取任何内容
Nothing read from input stream 没有从输入流中读取任何内容

Just found the solution immidiatly after the post: The "-c" part is missing. 刚在帖子发布后立即找到了解决方案:“-c”部分丢失。 Correct code snippet is: 正确的代码段是:

        // Linux Terminal
        String prefix = "/bin/bash";
        String c = "-c";
        String terminalCommand = "help";
        String mosquittoCommand = "mosquitto --help";

        ProcessBuilder pb1 = new ProcessBuilder(
                new String[] {prefix, c, terminalCommand});
        ProcessBuilder pb2 = new ProcessBuilder(
                new String[] {prefix, c, mosquittoCommand});

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

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