Runtime.getruntime(cmd.exe c () ---- record the log case, runtime. getruntime

Source: Internet
Author: User

Runtime.getruntime(cmd.exe c () ---- record the log case, runtime. getruntime
The runtime.getruntime(cmd.exe c () method is mainly used to execute external programs or commands.
Runtime.getruntime(cmd.exe c has six overload methods:
1. public Process exec (String command)
Execute the specified string command in a separate process.
2. public Process exec (String [] cmdArray)
Execute the specified command and variable in a separate process
3. public Process exec (String command, String [] envp)
Execute the specified command and variable in the independent process of the specified environment
4. public Process exec (String [] cmdArray, String [] envp)
Execute the specified command and variable in the independent process of the specified environment
5. public Process exec (String command, String [] envp, File dir)
Execute the specified string command in an independent process in the specified environment and working directory
6. public Process exec (String [] cmdarray, String [] envp, File dir)
Execute the specified command and variable in an independent process in the specified environment and working directory

Let's first compare the differences between exec (String command) and exec (String [] cmdArray). In fact, they are equivalent and will eventually call:
Exec (String [] cmdarray, String [] envp, File dir). Let's take a look at the implementation code of the exec (String cmdarray, String [] envp, File dir) throws IOException method:

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 final called code is exec (String [] cmdArray, String envp, File dir ). Exec (String command) is equivalent to exec (command, null, null), exec (String [] cmdArray) is equivalent to exec (cmdArray, null, null ).


Parameter description:
Cmdarray-an array containing the called command and its parameters.
Envp-string array. The environment variable format for each element is name = value. If the child process inherits the environment of the current process, or the parameter is null.
Dir-the working directory of the sub-process. If the sub-process should inherit the working directory of the current process, this parameter is null.

In addition, executing exec (String command) is not the same as directly executing the command line command, for example:
Javap-l xxx> output.txt
In this case, exec (String [] cmdArray) is used ). Example:
Process p = runtime.getruntime(cmd.exe c (new String [] {"/bin/sh", "-c ",

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


There are several methods to return the result type: Process:
1. destroy (): Kill sub-process
2. exitValue (): return the export value of the sub-process. The value 0 indicates normal termination.
3. getErrorStream (): gets the error stream of the sub-process
4. getInputStream (): gets the input stream of the sub-process.

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

6. waitFor (): causes the current thread to wait. If necessary, wait until the Process indicated by the Process object has been terminated. If the child process has been terminated, this method returns immediately. If the sub-process is not terminated, the called thread will be blocked until the sub-process exits. According to the Convention, 0 indicates normal termination.


For details about runtime.getruntime.exe c (), refer to the blog:Http://www.cnblogs.com/mingforyou/p/3551199.html

Case column: displays logs in logs (displayed in a dialog box. You can save the output logs to a specific file)

Public class MainActivity extends Activity implements OnClickListener {Button conLog;/** Called when the activity is first created. * // @ Override public void onCreate (Bundle savedInstanceState) {super. onCreate (savedInstanceState); setContentView (R. layout. main); conLog = (Button) findViewById (R. id. conLog); // The MyLog button found by id. isDebug = true; // enable conLog in debug mode. setOnClickListener (this);} public void onCl Ick (View v) {// The button is clicked to collect collected logs. 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> using line = new ArrayList <String> (); using line. add ("logcat"); using line. add ("-d"); // collect logs once. Stop using line. add ("-s"); // Filter Using line. add ("INFO"); System. out. println (Response line. toArray (new String [using line. size ()]); Process exec = runtime.getruntime(cmd.exe c (cmdLine. toArray (new String [using line. size ()]); // obtain the input stream InputStream inputStream = exec. getInputStream (); InputStreamReader buInputStreamReader = new InputStreamReader (inputStream); // decorator mode BufferedReader bufferedReader = new BufferedReader (buInputStrea MReader); // directly read String str = null; while (str = bufferedReader. readLine ())! = Null) {sb. append (str); // concatenate each read row into sb to remove sb. append ("\ n"); // a line break for each line} // Toast for Toast. makeText (this, sb. toString (), 1000 ). show ();}}
For Logcat commands, refer to blog: http://www.jb51.net/article/47055.htm

Copyright Disclaimer: This article is an original article by the blogger and cannot be reproduced without the permission of the blogger.

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.