Why Runtime.exec ("ls") does not have any output _java basic tutorial

Source: Internet
Author: User

This article addresses why runtime.exec ("ls") does not have any output to answer as follows:

calling the Runtime.exec method results in a local process and returns an instance of a process subclass that can be used to control the process or get information about the process. Because the child process created by calling the Runtime.exec method does not have its own terminal or console, So the standard IO of the subprocess (e.g. Stdin,stdou,stderr) is passed through Process.getoutputstream (), Process.getinputstream (), Process.geterrorstream () Method is redirected to its parent process. The user needs to use these streams to enter data into the child process or to get the output of the child process. So the proper execution of the runtime.exec ("ls") routine is as follows:

Try

{

process = Runtime.getruntime (). exec (command);

InputStreamReader Ir=newinputstreamreader (Process.getinputstream ());

LineNumberReader input = new LineNumberReader (IR);

String Line;

While (line = Input.readline ()) = null)

System.out.println (line);

}

catch (Java.io.IOException e) {

System.err.println ("IOException" + e.getmessage ());

}


This article is from the Java Learning Video tutorial blog, so be sure to keep this source http://10239772.blog.51cto.com/10229772/1653295

Why Runtime.exec ("ls") does not have any output _java basic tutorial

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.