Java Runtime exec() 方法



描述

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

宣告

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

public Process exec(String[] cmdarray)

引數

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

返回值

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

異常

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

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

  • NullPointerException − 如果命令為 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
         Process process = Runtime.getRuntime().exec(cmdArray);

         // 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
         Process process = Runtime.getRuntime().exec(cmdArray);

         // print another message
         System.out.println("example.txt should now open.");
      } catch (Exception ex) {
         ex.printStackTrace();
      }
   }
}

輸出

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

Executing calc.exe

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

以下示例演示了 Java Runtime exec() 方法的用法。在這個程式中,我們有一個字串陣列。添加了 explorer.exe 條目。我們使用 exec() 方法為 explorer 可執行檔案建立了一個 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] = "explorer.exe";

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

         // create a process and execute cmdArray
         Process process = Runtime.getRuntime().exec(cmdArray);

         // print another message
         System.out.println("example.txt should now open.");
      } catch (Exception ex) {
         ex.printStackTrace();
      }
   }
}

輸出

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

Executing explorer.exe
java_lang_runtime.htm
廣告
© . All rights reserved.