gpt4 book ai didi

java - 从 Java 启动终端时修改 PATH

转载 作者:塔克拉玛干 更新时间:2023-11-03 05:13:43 27 4
gpt4 key购买 nike

我使用以下 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

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

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

最佳答案

在这种特殊情况下,您可以通过使用 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();

如果您不能在 Java 端执行 getAbsolutePath 技巧,但仍希望相对路径(相对于 osascript 的当前目录)起作用,那么您d 需要这样的技巧:

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();

这有点像俄罗斯娃娃 - 我们正在从 Java 执行 osascript,Java 又运行自己的子 shell 来执行 pwdecho $ PATH 获取当前工作目录的值和 osascript 进程的 PATH 变量 到 AppleScript 变量中,然后我们将其注入(inject)到新的终端中。通过首先对“当前”目录执行 cd,然后对指定的 dirParam 执行另一个 cd 这将同时处理相对路径和绝对路径。

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

关于java - 从 Java 启动终端时修改 PATH,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19835305/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com