Java Runtime exec() 方法



描述

Java Runtime exec(String[] cmdarray, String[] envp, File dir) 方法在一個單獨的程序中,使用指定的環境和工作目錄執行指定的命令和引數。給定一個字串陣列 cmdarray,表示命令列的標記,以及一個字串陣列 envp,表示“環境”變數設定,此方法建立一個新的程序來執行指定的命令。

啟動作業系統程序高度依賴於系統。可能出錯的許多事情包括:

  • 找不到作業系統程式檔案。
  • 拒絕訪問程式檔案。
  • 工作目錄不存在。

在這種情況下,將丟擲異常。異常的確切性質取決於系統,但它始終是 IOException 的子類。

宣告

以下是 java.lang.Runtime.exec() 方法的宣告

public Process exec(String[] cmdarray, String[] envp, File dir)

引數

  • cmdarray − 包含要呼叫的命令及其引數的陣列。

  • envp − 字串陣列,其每個元素都以 name=value 的格式具有環境變數設定,如果子程序應該繼承當前程序的環境,則為 null。

  • dir − 子程序的工作目錄,如果子程序應該繼承當前程序的工作目錄,則為 null。

返回值

此方法返回一個新的 Process 物件,用於管理子程序。

異常

  • SecurityException − 如果存在安全管理器並且其 checkExec 方法不允許建立子程序。

  • IOException − 如果發生 I/O 錯誤。

  • NullPointerException − 如果命令為空。

  • IndexOutOfBoundsException − 如果 cmdarray 是一個空陣列(長度為 0)。

示例:在記事本中開啟檔案

此示例需要在我們的 C:/ 資料夾中有一個名為 test.txt 的檔案,其內容如下:

Hello

以下示例顯示了 Java Runtime exec() 方法的用法。在這個程式中,我們有一個字串陣列。添加了 Notepad.exe 和 test.txt 條目。我們使用 exec() 方法為記事本可執行檔案建立了一個 Process 物件。這將呼叫記事本應用程式,並將開啟 test.txt。如果發生某些異常,則會列印相應的堆疊跟蹤和錯誤訊息。如果在當前類路徑中找不到 test.txt,則記事本將報告找不到檔案的錯誤。

package com.tutorialspoint;

import java.io.File;

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] = "test.txt";

      // print a message
      System.out.println("Executing notepad.exe and opening test.txt");

      // create a file which contains the directory of the file needed
      File dir = new File("c:/");

      // create a process and execute cmdArray and currect environment
      Process process = Runtime.getRuntime().exec(cmdArray, null, dir);

      // print another message
      System.out.println("test.txt should now open.");

      } catch (Exception ex) {
         ex.printStackTrace();
      }
   }
}

輸出

讓我們編譯並執行上述程式,這將產生以下結果:

Executing notepad.exe and opening test.txt
test.txt should now open.

示例:開啟計算器

以下示例顯示了 Java Runtime exec() 方法的用法。在這個程式中,我們有一個字串陣列。添加了 calc.exe 條目。我們使用 exec() 方法為計算器可執行檔案建立了一個 Process 物件。這將呼叫計算器應用程式。如果發生某些異常,則會列印相應的堆疊跟蹤和錯誤訊息。

package com.tutorialspoint;

import java.io.File;

public class RuntimeDemo {

   public static void main(String[] args) {
      try {

      // create a new array of 1 string
      String[] cmdArray = new String[1];

      // first argument is the program we want to open
      cmdArray[0] = "calc.exe";

      // print a message
      System.out.println("Executing calc.exe");

      // create a file which contains the directory of the file needed
      File dir = new File("c:/");

      // create a process and execute cmdArray and currect environment
      Process process = Runtime.getRuntime().exec(cmdArray, null, dir);

      } catch (Exception ex) {
         ex.printStackTrace();
      }
   }
}

輸出

讓我們編譯並執行上述程式,這將產生以下結果:

Executing calc.exe

示例:開啟 Windows 資源管理器

以下示例顯示了 Java Runtime exec() 方法的用法。在這個程式中,我們有一個字串陣列。添加了 explorer.exe 條目。我們使用 exec() 方法為資源管理器可執行檔案建立了一個 Process 物件。這將呼叫 Windows 資源管理器應用程式。如果發生某些異常,則會列印相應的堆疊跟蹤和錯誤訊息。

package com.tutorialspoint;

import java.io.File;

public class RuntimeDemo {

   public static void main(String[] args) {
      try {

      // create a new array of 1 string
      String[] cmdArray = new String[1];

      // first argument is the program we want to open
      cmdArray[0] = "explorer.exe";

      // print a message
      System.out.println("Executing explorer.exe");

      // create a file which contains the directory of the file needed
      File dir = new File("c:/");

      // create a process and execute cmdArray and currect environment
      Process process = Runtime.getRuntime().exec(cmdArray, null, dir);

      } catch (Exception ex) {
         ex.printStackTrace();
      }
   }
}

輸出

讓我們編譯並執行上述程式,這將產生以下結果:

Executing calc.exe
java_lang_runtime.htm
廣告