尝试 return Unix 命令的输出作为 Java 中的字符串...未获得预期结果

Attempting to return the output of a Unix command as a string in Java... not getting expected results

我正在尝试在 Java 中创建一个小程序,它将允许我访问远程 Unix 服务器,运行 一些命令,然后在 IDE.

我一直在拼凑一些代码,并试图在我进行的过程中了解它是如何工作的,这可能并不理想!

目前,我能够 运行 从控制台执行命令并且我能够在服务器上看到结果,但是我一直无法将输出发送回控制台。每当我尝试将它们作为字符串发回时,我都会得到类似于 java.lang.UNIXProcess$ProcessPipeInputStream@9x57d1ad 的内容,而不是我在服务器上看到的通常输出。我敢肯定我做错了一些明显的事情,但我是初学者,尽管花了几个小时进行搜索,但我还没有找到解决方案。我真的很感激一些帮助。谢谢!

运行 在服务器上:

public static void main(String[] args) {
    try {
        ServerSocket serverSocket = new ServerSocket(portNumber);
        Socket clientSocket = serverSocket.accept();
        PrintWriter out =
                new PrintWriter(clientSocket.getOutputStream(), true);
        BufferedReader in = new BufferedReader(
                new InputStreamReader(clientSocket.getInputStream()));

        while ((inputLine = in.readLine()) != null) {

            out.println(inputLine);

            if (inputLine.equals("storage")) {
                runStorage();
                printResults(runStorage()); }

     catch (Exception e) {

        e.printStackTrace();

    }

public static Process runStorage() throws IOException {

    ProcessBuilder processBuilder = new ProcessBuilder();
    processBuilder.command("df", "-h");

    Process process = processBuilder.start();
    printResults(process);

    return process;

}

public static String printResults(Process process) throws IOException {
    BufferedReader reader = new BufferedReader(new InputStreamReader(process.getInputStream()));
    String line = "";
    while ((line = reader.readLine()) != null) {
        System.out.println(line);
    }
    return line;  }

客户:

        public class ClientInitiator {

            public static void main(String[] args) throws IOException, InterruptedException {

                String hostName = "myHostNameGoesHere";
                Socket echoSocket = new Socket(hostName, portNumber);
                PrintWriter out =
                        new PrintWriter(echoSocket.getOutputStream(), true);
                BufferedReader in =
                        new BufferedReader(
                                new InputStreamReader(echoSocket.getInputStream()));
                BufferedReader stdIn =
                        new BufferedReader(
                                new InputStreamReader(System.in));


                {
                    String userInput;

                    while (!(userInput = stdIn.readLine()).equals("")) {
                        out.println(userInput);
                        if (userInput.equals("storage")) {
                            String serverResponse = in.readLine();
                            System.out.println(serverResponse);}

为了获得 运行 进程的输出,请尝试以下代码。

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);  
}