Java Runtime exec() 方法



描述

Java Runtime exec(String[] cmdarray, String[] envp) 方法在單獨的程序中使用指定的環境執行指定的命令和引數。這是一種方便的方法。形式為 exec(cmdarray, envp) 的呼叫行為與呼叫 exec(cmdarray, envp, null) 完全相同。

宣告

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

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

引數

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

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

返回值

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

異常

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

  • IOException - 如果發生 I/O 錯誤

  • NullPointerException - 如果 command 為 null

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

示例

此示例需要一個名為example.txt的檔案,該檔案位於我們的 CLASSPATH 中,內容如下:

Hello World!

示例:使用給定文字檔案開啟記事本應用程式

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

package com.tutorialspoint;

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();
      }
   }
}

輸出

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

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

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

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

package com.tutorialspoint;

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 process and execute cmdArray and currect environment
         Process process = Runtime.getRuntime().exec(cmdArray,null);

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

輸出

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

Executing calc.exe

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

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

package com.tutorialspoint;

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 process and execute cmdArray and currect environment
         Process process = Runtime.getRuntime().exec(cmdArray,null);

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

輸出

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

Executing explorer.exe
java_lang_runtime.htm
廣告

© . All rights reserved.