在知乎上有個知友提問java
Java Runtime.exec(String[] cmdarray)與Java Runtime.exec(String cmdarray)區別,本身問了問度娘,大概得出的區別數組
cmdarray -- 數組,包含所調用命令及其參數.yii
這須要一個文件名爲example.txt在咱們的CLASSPATH中包含如下內容:code
Hello World!
下面的例子說明了如何使用lang.Runtime.exec()方法。get
package com.yiibai; public class RuntimeDemo { public static void main(String[] args) { try { // create a new array of 2 strings String[] cmdArray = new String[2]; // first argument is the program we want to open cmdArray[0] = "notepad.exe"; // second argument is a txt file we want to open with notepad cmdArray[1] = "example.txt"; // print a message System.out.println("Executing notepad.exe and opening example.txt"); // create a process and execute cmdArray and currect environment Process process = Runtime.getRuntime().exec(cmdArray,null); // print another message System.out.println("example.txt should now open."); } catch (Exception ex) { ex.printStackTrace(); } } }
讓咱們來編譯和運行上面的程序,這將產生如下結果:cmd
Executing notepad.exe and opening example.txt example.txt should now open.
而Java Runtime.exec(String cmdarray)直接執行cmd命令便可。string