Search code examples
javaunixbufferedreaderinputstreamreader

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


I'm trying to create a little program in Java which will allow me to access a remote Unix server, run some commands and then display the output of those commands in the IDE.

I've been smashing together bits of code and trying to learn how it works as I go along, which probably isn't ideal!

Currently, I'm able to run the commands from the console and I'm able to see the results on the server, but I've been unable to send the output back to the console. Whenever I try to send them back as a string, I'm getting something along the lines of java.lang.UNIXProcess$ProcessPipeInputStream@9x57d1ad instead of the usual output that I'm seeing on the server. I'm sure there's something obvious I'm doing wrong, but I'm a beginner and despite spending hours searching I've not been able to find a solution yet. I'd really appreciate some help. Thanks!

RUNNING ON SERVER:

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

CLIENT:

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

Solution

  • In order to get output of a running process, try the following code.

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