Java Runtime exec() 方法



描述

Java Runtime exec(String command, String[] envp, File dir) 方法在單獨的程序中,使用指定的環境和工作目錄執行指定的字串命令。這是一個便捷方法。 exec(command, envp, dir) 形式的呼叫與 exec(cmdarray, envp, dir) 的呼叫行為完全相同,其中 cmdarray 是命令中所有標記的陣列。

更準確地說,命令字串使用由呼叫 new StringTokenizer(command) 建立的 StringTokenizer(不進一步修改字元類別)分解成標記。然後,由標記生成器產生的標記按相同的順序放置在新字串陣列 cmdarray 中。

宣告

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

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

引數

  • command − 指定的系統命令。

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

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

返回值

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

異常

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

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

  • NullPointerException − 如果 command 為 null,或者 envp 的一個元素為 null。

  • IllegalArgumentException − 如果 command 為空。

示例:開啟記事本應用程式

以下示例顯示了 Java Runtime exec() 方法的用法。我們使用當前目錄中的 exec() 方法為記事本可執行檔案建立了一個 Process 物件。這將呼叫記事本應用程式。如果發生某些異常,則會列印帶有錯誤訊息的相應堆疊跟蹤。

package com.tutorialspoint;

import java.io.File;

public class RuntimeDemo {

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

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

      // create a file with the working directory we wish
      File dir = new File("C:/");

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

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

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

輸出

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

Executing notepad.exe...
Notepad should now open.

示例:開啟計算器應用程式

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

package com.tutorialspoint;

import java.io.File;

public class RuntimeDemo {

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

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

      // create a file with the working directory we wish
      File dir = new File("C:/");

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

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

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

輸出

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

Executing calc.exe...
Calculator should now open.

示例:開啟 Windows 資源管理器應用程式

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

package com.tutorialspoint;

import java.io.File;

public class RuntimeDemo {

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

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

      // create a file with the working directory we wish
      File dir = new File("C:/");

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

      // print another message
      System.out.println("Windows Explorer should now open.");

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

輸出

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

Executing calc.exe...
Calculator should now open.
java_lang_runtime.htm
廣告
© . All rights reserved.