Runtime.getruntime (). EXEC ()----log case

Source: Internet
Author: User

The Runtime.getruntime (). Exec () method is primarily used to performthe external program or command.
Runtime.getruntime (). Exec has a total of six overloaded methods:
1.public Process exec (String command)
Executes the specified string command in a separate process.
2.public Process exec (String [] cmdarray)
Executing specified commands and variables in a separate process
3.public Process Exec (String command, string [] envp)
Executes the specified command and variable in a separate process in the specified environment
4.public Process exec (string [] Cmdarray, String [] envp)
Executes the specified command and variable in a separate process in the specified environment
5.public Process exec (String command,string[] envp,file dir)
Executes the specified string command in a separate process with the specified environment and working directory
6.public Process exec (string[] cmdarray,string[] envp,file dir)
Executes the specified commands and variables in a separate process in the specified environment and working directory

Let's compare the difference between exec (String command) and exec (string[] cmdarray), in fact they are equivalent and will eventually be called:
EXEC (string[] cmdarray,string[] envp,file dir), let's look at the method exec (String cmdarray,string[] Envp,file dir) throws Implementation code for the IOException:
Public Process exec (String command, string[] envp, File dir) throws IOException {    if (command.length () = = 0) throw new IllegalArgumentException ("Empty command");    StringTokenizer st = new StringTokenizer (command);    string[] Cmdarray = new String[st.counttokens ()];    for (int i = 0; St.hasmoretokens (); i++)        cmdarray[i] = St.nexttoken ();    return exec (Cmdarray, envp, dir);}

From the code above, we can see that the code of the final call is: EXEC (string[] cmdarray,string envp,file dir). EXEC (String command) is equivalent to EXEC (command,null,null), and exec (string[] cmdarray) is equivalent to EXEC (cmdarray,null,null).


Parameter description:
Cmdarray- An array containing the called command and its arguments.
envp- A string array in which each element's environment variable is formatted as Name=value if the child process should inherit the current process's environment, or the parameter is null.
DIR- The working directory of the child process, or null if the child process should inherit the working directory of the current process.

In addition, execute EXEC (String command) is not equivalent to executing command line commands directly, such as commands:
javap-l xxx > Output.txt
This is to use EXEC (string[] cmdarray). As an example:
Process p = runtime.getruntime (). EXEC (new string[]{"/bin/sh", "-C",

"Javap-l xxx > Output.txt"});


About the return result type: Process, it has several methods:
1.destroy (): Kill child process
2.exitValue (): returns the exit value of the child process with a value of 0 indicating normal termination
3.getErrorStream (): Gets the error stream for the child process
4.getInputStream (): gets the input stream of the child process

5.getOutputStream (): gets the output stream of the child process

6.waitFor (): causes the current thread to wait and, if necessary, waits until the process represented by that process object has been terminated. If the child process has been terminated, this method returns immediately. If the child process is not terminated, the calling thread will be blocked until the child process exits, according to convention, 0 indicates normal termination


For Runtime.getRuntime.exec (), you can refer to the blog:http://www.cnblogs.com/mingforyou/p/3551199.html

Case: Log display in the implementation log (shown as a dialog box, you can save the output log to a specific file)

public class Mainactivity extends activity implements onclicklistener{Button conlog;/** called when the activity is fi RST created.        */@Override public void OnCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate);        Setcontentview (R.layout.main); Conlog = (Button) Findviewbyid (r.id.conlog);//button found by id mylog.isdebug = true;//debug mode on Conlog.setonclicklis    Tener (this); }public void OnClick (View v) {///button is clicked, collect collect log try {Readlog ();} catch (IOException e) {//TODO auto-generated catch Blocke. Printstacktrace ();}} /** * @author Danny QQ 858030348 * @throws ioexception * */private void Readlog () throws IOException {mylog.i ("INFO", "Start Connectlog"); StringBuffer sb = new StringBuffer (); arraylist<string> cmdline = new arraylist<string> () Cmdline.add ("Logcat"); Cmdline.add ("-D");// Collect log Stop Cmdline.add ("-S");//Filter Cmdline.add ("INFO"); System.out.println (Cmdline.toarray (New String[cmdline.size ())); Process exec = Runtime.getruNtime (). EXEC (Cmdline.toarray (new String[cmdline.size ()));//Gets the input stream after executing the command inputstream InputStream = Exec.getinputstream (); InputStreamReader buinputstreamreader = new InputStreamReader (inputstream);// Adorner mode BufferedReader BufferedReader = new BufferedReader (buinputstreamreader);//Direct read String str = Null;while ((str = Bufferedreader.readline ())!=null) {sb.append (str);//each line is stitched into SB to Sb.append ("\ n");//lines a newline character}//toast Toast.maketext ( This, sb.tostring (), +). Show ();}}
For Logcat commands, refer to the blog: http://www.jb51.net/article/47055.htm

Runtime.getruntime (). EXEC ()----log case

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.