The following uses Windows cmd as an example.
Import Java. io. bufferedreader; import Java. io. ioexception; import Java. io. inputstreamreader; public class runtimetest {public static void main (string [] ARGs) {invokeouterapplication ();} Private Static Boolean invokeouterapplication () {Boolean issuccess = false; runtime = runtime. getruntime (); int exitval = 1; Process = NULL; try {string [] ARGs = {"C: \ WINDOWS \ system32 \ cmd.exe ","/ C ipconfig-all "}; process = runtime.exe C (" CMD/C netstat-an ");/* the process for uploading files must be synchronized with the master process, the main process needs to know whether the file can be returned after the upload is successful. In this way, the waitfor () method must be called. * The waitfor () method of process may block thread execution, if the waitfor () method is written to the cache, including the standard input cache and the standard error cache, if the * cache is full and the two are not read and used, then waitfor () the method will be blocked, waiting for the cache to be read, so you need to add the following section to * Ensure that the cache is correctly read at any time, so as to ensure waitfor () the method is executed smoothly without blocking the deadlock. */FINAL stringbuilder errorinfo = new stringbuilder (); Final bufferedreader inbr = new bufferedreader (New inputstreamreader (process. getinputstream (); Final bufferedreader errbr = new bufferedreader (New inputstreamreader (process. geterrorstream (); new thread () {public void run () {string Infoline = NULL; try {While (Infoline = inbr. readline ())! = NULL) {system. out. println (Infoline);} inbr. close ();} catch (ioexception e) {e. printstacktrace ();}}}. start (); New thread () {public void run () {string errline = NULL; try {While (errline = errbr. readline ())! = NULL) {errorinfo. append (errline);} errbr. close ();} catch (ioexception e) {e. printstacktrace ();}}}. start (); exitval = process. waitfor ();} catch (interruptedexception e) {e. printstacktrace ();} catch (ioexception e) {e. printstacktrace ();} finally {try {process. getinputstream (). close (); process. geterrorstream (). close ();} catch (ioexception e) {e. printstacktrace () ;}}if (exitval = 0) {issuccess = true;} return issuccess ;}}
The first thread to be started can print the output of the application. If necessary, you can also process the output content of the application, as long as you add your own processing code.
The above program can display the same content in the Java console as that output by executing ipconfig-all on the CMD command line.
Here, the/C parameter is described as follows:
Start a new instance of the Windows Command Interpreter
CMD [/A|/U] [/Q] [/D] [/E:ON|/E:OFF] [/F:ON|/F:OFF] [/V:ON|/V:OFF] [[/S] [/C|/K] string]
/C: Execute the specified string command and then terminate
/K executes the specified string command but retains
/S Modify/C
Or string processing after/K (see below)
/Q close echo
/D disable the autorun command from the Registry (see below)
/A outputs Internal commands to pipelines or files into ANSI
/U: Convert the Internal Command output to the pipeline or file to Unicode
/T: FG sets the foreground/background color (for details, see
Color /?)
/E: Enable command extension on (see below)
/E: Off disable command extension (see below)
/F: Enable the file and directory name completion characters on (see below)
/F: Off disabled file and directory name completion characters (see below)
/V: Use on!
Enable delayed environment variables as separators
Extension. For example,/V: On
Yes! VaR!
Extend the variable VAR during execution. The VaR syntax extends variables when input, which is different from in a for loop.
/V: Off disables delayed environment extension.