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