Java calls Windows commands Java calls Windows commands primarily in two classes: Java.lang.Runtime
Each Java application has a runtime class instance that enables the application to connect to the environment in which it is running. The current runtime can be obtained through the GetRuntime method. The application cannot create its own instance of the Runtime class. Java.lang.Process
The Processbuilder.start () and Runtime.exec methods create a native process and return an instance of the process subclass that can be used to control the process and obtain relevant information. The process class provides a way to perform input from the process, perform output to the process, wait for the process to complete, check the exit status of the process, and destroy (kill) the process. For Java processes with process objects, it is not necessary to execute the processes represented by the process object asynchronously or concurrently.
Here is a simple example:
Package Test;import Java.io.ioexception;import Java.io.bufferedreader;import java.io.inputstream;import java.io.inputstreamreader;/** * Java call Windows command Test * @author Liuyazhuang * */public class Test {public static void main (STR ing args[]) {testwincmd (); Diropt (); } public static void Testwincmd () {System.out.println ("------------------testwincmd ()--------------------"); Runtime runtime = Runtime.getruntime (); System.out.println (Runtime.totalmemory ()); System.out.println (Runtime.freememory ()); System.out.println (Runtime.maxmemory ()); System.out.println (Runtime.availableprocessors ()); Number of processors try {//execute an EXE file runtime.exec ("Notepad"); Runtime.exec ("C:\\Program Files\\Microsoft Office\\office11\\winword.exe C:\\test.doc"); Execute batch processing runtime.exec ("C:\\x.bat"); Execute system Command runtime.exec ("cmd/c dir"); Runtime.exec ("cmd/c dir c:\\");// --------------file Operation--------------runtime.exec ("cmd/c copy C:\\x.bat d:\\x.txt"); Copy and rename runtime.exec ("cmd/c rename d:\\x.txt x.txt.bak"); Rename Runtime.exec ("cmd/c move D:\\x.txt.bak c:\\"); Mobile Runtime.exec ("Cmd/c del C:\\x.txt.bak"); Delete//--------------directory Operation--------------Runtime.exec ("cmd/c md c:\\_test"); Delete} catch (IOException e) {e.printstacktrace (); }}/** * Executes batch file and gets output stream re-output to console */public static void Diropt () {System.out.println ("----------- -------diropt ()--------------------"); Process process; try {//execute command process = Runtime.getruntime (). EXEC ("C:\\x.bat"); The output stream that obtains the command result inputstream FIS = Process.getinputstream (); Use a read output stream class to read BufferedReader BR = new BufferedReader (new InputStreamReader (FIS)); String line = null; Row-by-line read output to the console while (lines = Br.readline ())! = null) {System.out.println; }} catch (IOException e) {e.printstacktrace (); } }}
X.bat
Del C:\del.txtcd D:dir
Operation Result:
------------------testwincmd ()--------------------20316161652120666501122------------------diropt ()----------- ---------D:\_dev_stu\fileopt>del c:\del.txt d:\_dev_stu\fileopt>cd D: d:\_dev_stu\fileoptd:\_dev_ Stu\fileopt>dir the volume in drive D is the disk1_vol2 volume's serial number is the 70fb-dfafd:\_dev_stu\fileopt directory 2008-07-18 09:30 <DIR> . 2008-07-18 09:30 <DIR> .. 2008-07-18 09:30 <DIR> src2008-07-18 09:31 549 fileopt.iml2008-07-18 09:31 10,292 fileopt.ipr2008-07-18 14:27 <DIR> classes2008-07-18 16:42 26,265 FILEOPT.IWS 3 files 37,106 bytes 4 directories 8,095,498,240 Available Bytes process finished with exit code 0
Java calls Windows commands