JAVA 中的进程生成器在 Mac OS 中不起作用 X

Process builder in JAVA not working in Mac OS X

JAVA 中的进程生成器在 Mac OS 中不起作用 X:

String[] command = { "mkdir", "one"};

ProcessBuilder process = new ProcessBuilder(command);

process.start();

此后 "one" 目录未在 Mac 'Home' 用户目录中创建。

您需要在进程构建器中指定工作目录。设置为Mac用户家,可以使用系统属性 user.home:

ProcessBuilder pb = new ProcessBuilder(command);
pb.directory(new File(System.getProperty("user.home")));
pb.start();

根据 java.lang.ProcessBuilder 的 Javadoc:

Each process builder manages these process attributes:

  • ...
  • a working directory. The default value is the current working directory of the current process, usually the directory named by the system property user.dir.