Java ProcessBuilder directory() 方法



描述

Java ProcessBuilder directory() 方法返回此进程构建器的当前工作目录。随后由此对象的 start() 方法启动的子进程将使用此目录作为其工作目录。返回值可能为 null,这意味着使用当前 Java 进程的工作目录(通常是系统属性 user.dir 指定的目录)作为子进程的工作目录。

声明

以下是 java.lang.ProcessBuilder.directory() 方法的声明

public File directory()

参数

返回值

此方法返回此进程构建器的当前工作目录。

异常

从 Process Builder 示例中获取记事本的目录详细信息

以下示例演示了 ProcessBuilder directory() 方法的使用。在这个程序中,我们创建了一个字符串数组,并将 notepad.exe 和 test.txt 添加到其中。使用该列表,我们初始化了一个 ProcessBuilder 实例。现在使用 directory() 方法,我们打印了底层目录的详细信息。

package com.tutorialspoint;

public class ProcessBuilderDemo {

   public static void main(String[] args) {

      // create a new list of arguments for our process
      String[] list = {"notepad.exe", "test.txt"};

      // create the process builder
      ProcessBuilder pb = new ProcessBuilder(list);

      // get the working directory of the process
      System.out.println(pb.directory());
   }
}

输出

让我们编译并运行上面的程序,这将产生以下结果:

null

从 Process Builder 示例中获取计算器的目录详细信息

以下示例演示了 ProcessBuilder directory() 方法的使用。在这个程序中,我们创建了一个字符串数组,并将 calc.exe 添加到其中。使用该列表,我们初始化了一个 ProcessBuilder 实例。现在使用 directory() 方法,我们打印了底层目录的详细信息。

package com.tutorialspoint;

public class ProcessBuilderDemo {

   public static void main(String[] args) {

      // create a new list of arguments for our process
      String[] list = {"calc.exe"};

      // create the process builder
      ProcessBuilder pb = new ProcessBuilder(list);

      // get the working directory of the process
      System.out.println(pb.directory());
   }
}

输出

让我们编译并运行上面的程序,这将产生以下结果:

null

从 Process Builder 示例中获取 Windows 资源管理器的目录详细信息

以下示例演示了 ProcessBuilder directory() 方法的使用。在这个程序中,我们创建了一个字符串数组,并将 explorer.exe 添加到其中。使用该列表,我们初始化了一个 ProcessBuilder 实例。现在使用 directory() 方法,我们打印了底层目录的详细信息。

package com.tutorialspoint;

public class ProcessBuilderDemo {

   public static void main(String[] args) {

      // create a new list of arguments for our process
      String[] list = {"explorer.exe"};

      // create the process builder
      ProcessBuilder pb = new ProcessBuilder(list);

      // get the working directory of the process
      System.out.println(pb.directory());
   }
}

输出

让我们编译并运行上面的程序,这将产生以下结果:

null
java_lang_processbuilder.htm
广告
© . All rights reserved.