Here we do a simple introduction to the runtime class in Java. If you do not often think of impermanence and death, although there are extremely clever, it is supposed to say and the same as a nerd.
Use of the Runtimeo class one, get some information of system memory
@Test Public voidRuntimeinfo () {Runtime runtime=Runtime.getruntime (); intprocessors =runtime.availableprocessors (); LongFreememory =runtime.freememory (); LongMaxMemory =runtime.maxmemory (); LongTotalMemory =runtime.totalmemory (); //processors=4, freememory=165713400, maxmemory=2837446656, totalmemory=192937984Logger.debug ("processors={}, freememory={}, maxmemory={}, totalmemory={}", processors, Freememory, MaxMemory, totalmemory);}
Second, get the environment variable of the system
@Test Public voidDirruntimeprocess ()throwsIOException, interruptedexception {process process= Runtime.getruntime (). EXEC ("cmd.exe/c echo%java_home%"); BufferedReader BufferedReader=NewBufferedReader (NewInputStreamReader (Process.getinputstream ())); String String=NULL; while(String = Bufferedreader.readline ())! =NULL) {System.out.println (string);//D:\Java\jdk\jdk1.8.0_152} process.waitfor (); System.out.println ("Return:" + process.exitvalue ());//return:0}
Third, get the Java version number, this is not the same as the above
@Test Public voidgetjavaversion () {Try{Process Process= Runtime.getruntime (). EXEC ("Javac-version"); BufferedReader BR=NewBufferedReader (NewInputStreamReader (Process.geterrorstream ())); String Line=NULL; while(line = Br.readline ())! =NULL) System.out.println (line); //Javac 1.8.0_152process.waitfor (); System.out.println ("Process exitvalue:" +Process.exitvalue ()); } Catch(Throwable t) {t.printstacktrace (); }}
Iv. results obtained from the execution of external commands
@Test Public voidEXECPROGRAMC () {Try{Process Process= Runtime.getruntime (). EXEC ("C:/users/76801/desktop/huhx.exe"); BufferedReader BR=NewBufferedReader (NewInputStreamReader (Process.getinputstream ())); String Line=NULL; while(line = Br.readline ())! =NULL) System.out.println (line); //Hello World.process.waitfor (); System.out.println ("Process exitvalue:" +Process.exitvalue ()); } Catch(Throwable t) {t.printstacktrace (); }}
HUHX.C is relatively simple, is to print a word.
#include <stdio.h>void main () { printf ("Hello World"). " );}
V. Using the runtime class to export MySQL scripts
@Test Public voidExecmysqldump ()throwsIOException, interruptedexception {String execcommand= "cmd c/d:/java/mysqldump.exe-uhuhx-phuhx boot_learn > D:/bootlearn.sql"; System.out.println ("EXEC command:" +execcommand); Runtime Runtime=Runtime.getruntime (); Process P=runtime.exec (execcommand); Streamgobbler Errorgobbler=NewStreamgobbler (P.geterrorstream (), "Error"); Streamgobbler Outputgobbler=NewStreamgobbler (P.getinputstream (), "Output"); Errorgobbler.start (); Outputgobbler.start (); P.waitfor (); System.out.println ("Successful." +P.exitvalue ());}
The above also uses the content in the standard output buffer of the readout window which is said on the net, still does not solve the waitFor blocking problem of the process. The following is the thread code that empties the buffer:
Public classStreamgobblerextendsThread {InputStream is; String type; PublicStreamgobbler (InputStream is, String type) { This. is =is ; This. Type =type; } Public voidrun () {Try(InputStreamReader ISR =NewInputStreamReader (IS);) {BufferedReader br=NewBufferedReader (ISR); String Line=NULL; while(line = Br.readline ())! =NULL) { if(Type.equals ("Error") {System.out.println ("Error:" +Line ); } Else{System.out.println ("Debug:" +Line ); } } } Catch(IOException e) {e.printstacktrace (); } }}
The goal of the code is to export the MySQL database script. No solution was found for the problem, and the operating environment was win10,jdk1.8.
Friendship Link
Java Foundation----Use of the >runtime class (i)