Java calls the shell command and gets execution results

Source: Internet
Author: User

Using the process and runtime two classes, the return value is obtained by the getInputStream () method of the Process class[Plain] View plain copy package ark;      import java.io.bufferedreader;   Import  java.io.IOException;   import java.io.inputstreamreader;   import  java.util.arraylist;   import java.util.list;      public class  readcmdline {       public static void main (String args[ ])  {           Process process = null;            List<String> processList =  New arraylist<string> ();           try {                process =  Runtime.getruntime (). EXEC ("Ps -aux");                bufferedrEader input = new bufferedreader (New inputstreamreader (Process.getInputStream ()));                string line =   "";               while  (line  = input.readline ())  != null  {                    processlist.add (line);                }                input.close ();           }  catch  (ioexception e)  {                e.printstacktrace ();           }       &NBsp;       for  (string line : processlist)  {    &NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;SYSTEM.OUT.PRINTLN (line);            }       }  }   

Call the shell script to determine whether the normal execution, if the normal end, the process of the WAITFOR () method return 0

[Java] View Plain copy Public static void callshell (string shellstring)  {        try {           process process  = runtime.getruntime (). EXEC (shellstring);            int exitvalue = process.waitfor ();            if  (0 != exitvalue)  {                log.error ("CALL&NBSP;SHELL&NBSP;FAILED.&NBSP;ERROR&NBSP;CODE&NBSP;IS&NBSP;:"  +  exitvalue);           }        } catch  (throwable e)  {            Log.error ("call shell failed. "  + e);       }   } 

SOURCE Blog: http://blog.csdn.net/arkblue/article/details/7897396

Related Article

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.