I used java to call the top command. The result is incorrect.

Source: Internet
Author: User
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 ();
}
}
}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.