6

我使用以下 Java 代码启动终端:

final ProcessBuilder processBuilder = new ProcessBuilder("/usr/bin/open", "-b",
                                                         "com.apple.Terminal",
                                                         "/Volumes");
final Map<String, String> environment = processBuilder.environment();
final String path = environment.get("PATH");
environment.put("PATH", "/mypath" + File.pathSeparator + path);
final Process process = processBuilder.start();
process.waitFor();

这会导致打开一个终端窗口,但 PATH 的修改似乎被忽略了:

Volumes$ echo $PATH
/usr/bin:/bin:/usr/sbin:/sbin:/usr/local/bin

这个问题似乎与我如何启动终端有关。通过修改环境变量启动其他应用程序工作正常。

即使终端已经打开,如何启动终端以便它接受我的环境变量修改?

4

2 回答 2

4

在这种特殊情况下,您可以使用 AppleScript 打开终端来伪造它:

final ProcessBuilder processBuilder = new ProcessBuilder(
      "/usr/bin/osascript", "-e");
final Map<String, String> environment = processBuilder.environment();
final String path = environment.get("PATH");
processBuilder.command().add("tell application \"Terminal\" to do script \"" +
    "cd /Volumes ; export PATH=\\\"/mypath:" + path + "\\\\"\"");
final Process process = processBuilder.start();
process.waitFor();

如果您想从用户提供的参数中填充cd目录和PATH值,那么我会考虑使用on run让脚本接受参数,并使用quoted form of以避免任何潜在的引用问题:

final ProcessBuilder processBuilder = new ProcessBuilder("/usr/bin/osascript",
   "-e", "on run argv",
   "-e", "  tell application \"Terminal\" to do script "
            + "\"cd \" & quoted form of item 1 of argv & \" ; "
            + "export PATH=\" & quoted form of item 2 of argv",
   "-e", "end run");

// create a File and use getAbsolutePath to resolve any relative paths
// against this Java process's working directory.
File cdDir = new File(dirParam);

// this argument will be "item 1 of argv" to the AppleScript
processBuilder.command().add(cdDir.getAbsolutePath());

final Map<String, String> environment = processBuilder.environment();
final String path = environment.get("PATH");

File pathPrefix = new File(pathParam);
// and this will be "item 2 of argv"
processBuilder.command().add(
      pathPrefix.getAbsolutePath() + File.pathSeparator + path);

final Process process = processBuilder.start();
process.waitFor();

如果你不能getAbsolutePath在 Java 端做到这一点,但仍然希望相对路径(相对于 的当前目录osascript)起作用,那么你需要这样的技巧:

final ProcessBuilder processBuilder = new ProcessBuilder("/usr/bin/osascript",
   "-e", "on run argv",
   "-e", "  set currentdir to (do shell script \"pwd\")",
   "-e", "  set currentpath to (do shell script \"echo $PATH\")",
   "-e", "  tell application \"Terminal\" to do script \""
            + "cd \" & quoted form of currentdir & \" ; ""
            + "cd \" & quoted form of item 1 of argv & \" ; "
            + "export PATH=\" & quoted form of currentpath",
   "-e", "end run",
   dirParam);

final Map<String, String> environment = processBuilder.environment();
final String path = environment.get("PATH");
environment.put("PATH", pathParam + File.pathSeparator + path);

final Process process = processBuilder.start();
process.waitFor();

这有点像俄罗斯玩偶——我们osascript从 Java 执行,它又运行自己的子 shell 来执行pwd并将echo $PATH当前工作目录的值和osascript 进程PATH的变量放入 AppleScript 变量中,然后我们将其注入进入新航站楼。通过首先对“当前”目录执行一个操作,然后对指定的另一个执行操作,这将处理相对路径和绝对路径。cdcddirParam

最后请注意osascript打印脚本的返回值,因此您应该确保在执行之前使用所有进程输出waitFor(或者如果您使用的是 Java 7,您可以使用processBuilder.redirectOutput(new File("/dev/null"))和相同的 for redirectError)。

于 2013-11-25T11:20:22.067 回答
2

一些想法 - 我无法尝试任何东西,因为我只有 Linux 在工作。

您正在通过open命令打开终端。也许如果您直接运行终端可执行文件(我想您需要转到 .app 文件夹中的实际二进制文件)您的环境设置会通过吗?

或者,另一种启动终端的方法是open保存终端首选项文件(扩展名 .terminal,通过 Terminal.app 的首选项面板创建)。这将打开一个设置了这些首选项的终端。多年来我一直没有修改终端偏好,但也许有一种方法可以在那里设置路径?

于 2013-11-28T11:29:23.880 回答