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
广告
© . All rights reserved.