哪位NR能给我解释一下,我用java调linux的top命令,结果没取到输出,打印结果如下:
buffer size = -1
Return code = 1
并且报top: failed tty get的错误
把top命令换成free,结果能正确打印。为什么会这样?[code]import java.io.*;
public class Execute {
public static void main(String[] args) {
try {
Process process = Runtime.getRuntime().exec("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();
}
}
}[/code]