1

I'm writing a program that basically sends linux command through java and then prints back the output. It works fine if the output is one line only but for multiple lines output I can't figure out what I'm doing wrong. For example to check the memory usage I use the "free" command but it only returns lines 1 and 3. Here is my code:

if (clinetChoice.equals("3"))
    {
        String command = "free";

        Process process = Runtime.getRuntime().exec(command);

        BufferedReader reader = new BufferedReader(new InputStreamReader(process.getInputStream()));

        System.out.println("You Chose Option Three");

        String line;            

        while ((line = reader.readLine()) != null)
        {
            output += line;
            System.out.println(line);
            line = reader.readLine();
        }

    }

When I run this it only returns:

total  used  free  share  buffers  cached
-/+ buffers/cache:  6546546  65464645

Client Code:

while ((fromServer = input.readLine()) != null)
    {
        System.out.println("Server: " + fromServer);            
        if (fromServer.equals("Bye"))
            break;          

        System.out.print("Enter your choice: ");
        fromClient = stdIn.readLine().trim();

        if(fromClient.equals("1"))
        {
            System.out.println("Client: " + fromClient);
            output.println(fromClient);

        }
        if(fromClient.equals("2"))
        {
            System.out.println("Client: " + fromClient);
            output.println(fromClient);

        }
        if(fromClient.equals("3"))
        {
            System.out.println("Client: " + fromClient);
            output.println(fromClient);

        }
        if(fromClient.equals("4"))
        {
            System.out.println("Client: " + fromClient);
            output.println(fromClient);
            break;

        }


    }
Nick
  • 597
  • 3
  • 14
  • 34

3 Answers3

6

You're calling readLine in both your loop test and the body of the loop. So for every iteration of the loop, readLine is called twice, and one of the results is discarded: it isn't printed or added to output. This matches the results that you describe.

This loop should be sufficient:

while ((line = reader.readLine()) != null)
{
    output += line + System.getProperty("line.separator");
    System.out.println(line);
}

If you're just trying to print the entire output once, and since you're collecting the output in your output variable, you can move the println out of the loop:

while ((line = reader.readLine()) != null)
{
    output += line + System.getProperty("line.separator");
}

System.out.println(output);
pb2q
  • 58,613
  • 19
  • 146
  • 147
  • this answer is correct, just ditch the `line = reader.readLine();` at the bottom of your loop. – lynks Sep 10 '12 at 17:32
  • This works, thanks. Only thing is that it returns everything on a same line. I've added output += line + "\n"; but this prints one line at a time instead of printing everything at one time. – Nick Sep 10 '12 at 17:59
  • @Nick: `readLine` consumes the newline so you will need to add it back. If you want to print the output all at once, remove the `println` in your loop and do a `println(output)` after the loop. I'll add this to the answer – pb2q Sep 10 '12 at 18:03
  • I should have mentioned that I'm using Sockets communication between Client / Server program. So basically from Client program I'm asking Server program to run the "free" command and print it back in Client program. With line.separator I'm still getting the same thing as when I use the "\n", it prints one line, then second line... instead of printing everything at once. I will add my client code to the top post. – Nick Sep 10 '12 at 18:13
1

Simply use this... You are calling the readLine() twice....

while ((line = reader.readLine()) != null)
        {

            System.out.println(line);

        }

If you want to assign the data to output varible..then do this inside the while loop..

output = output + line;

Kumar Vivek Mitra
  • 33,294
  • 6
  • 48
  • 75
1

I should point out that in addition to the comments re. using readline() twice, you should strictly consume stdout/stderr simultaneously. Otherwise you run the risk of blocking the process output since you're not consuming it. See this SO answer for more info.

Community
  • 1
  • 1
Brian Agnew
  • 268,207
  • 37
  • 334
  • 440