You need to use Java to call external commands (shell scripts, start services, and so on) before using Runtime.getruntime (). Exec calls external programs, Runtime.getruntime (). Exec is the Java Primitive command, While the Apache commons-exec encapsulates some common methods used to execute external commands. For example, we want the file information under the current Windows directory, and the command under the cmd command is dir. The code example shows the 2 method implementations.
The first type of Runtime.getruntime (). exec
String pl_cmd = "cmd.exe/c dir"; = runtime.getruntime (). exec (pl_cmd); New BufferedReader (new InputStreamReader ( p_pl.getinputstream ())); = br_pl.readline (); while NULL ) { System.out.println (stdout); = br_pl.readline (); }
The second type of Apache Commons-exec
New Bytearrayoutputstream (); New Pumpstreamhandler (stdout); = Commandline.parse ("cmd.exe/c dir"); New defaultexecutor (); Exec.setstreamhandler (PSH); Exec.execute (CL); System.out.println (Stdout.tostring ());
Code reference GitHub
Apache Common exec executes external commands