If you use java to call the top Command, the result is incorrect-Linux general technology-Linux programming and kernel information. For more information, see the following. Which NR can explain to me? I used java to call the top command of linux, but the result was not output. The printed result is as follows:
Buffer size =-1
Return code = 1
And the top: failed tty get error is reported.
Replace the top command with free, and the result is printed correctly. Why?
CODE: import java. io .*;
Public class Execute {
Public static void main (String [] args ){
Try {
Process process = runtime.getruntime(cmd.exe c ("top ");
InputStream is = process. getInputStream ();
Byte [] buffer = new byte [1024];
While (true ){
Int size = is. read (buffer );
System. out. println ("buffer size =" + size );
If (size> 0 ){
System. out. write (buffer, 0, size );
} Else {
Break;
}
}
Int returnCode = process. waitFor ();
System. out. println ("Return code =" + returnCode );
} Catch (Exception e ){
E. printStackTrace ();
}
}
} |