This article writes that Java invokes the Linux shell command and returns the result as a stream.
The following is directly on the code, the code is annotated.
Very plain and easy to understand.
Import Java.io.BufferedReader;
Import java.io.IOException;
Import Java.io.InputStreamReader;
public class Main
{public
static void Main (string[] args)
{
//Get the related Runtime run object of the Java process
Runtime runtime = Runtime.getruntime ();
Try
{
///Use the Exec () method to execute the shell command Ls-al/root and return a process object that is a subprocess
//ps: Here are examples of the simplest shell commands.
Process Process = Runtime.exec ("Ls-al/root");
BufferedReader bufferreader = new BufferedReader (
new InputStreamReader (Process.getinputstream ()));
StringBuffer StringBuffer = new StringBuffer ();
String temp = null;
while (temp = Bufferreader.readline ())!= null)
{
stringbuffer.append (temp);
Stringbuffer.append ("\ n");
}
System.out.println (StringBuffer);
}
catch (IOException e)
{
e.printstacktrace ();
}
}
}