Setting the environment for ProcessBuilder(设置 ProcessBuilder 的环境)
问题描述
I have a strange problem setting the Linux environment from Java (1.6); specifically the "PATH" variable.
In a nutshell, I have a pipeline for running native processes, which uses java.lang.ProcessBuilder
. The user can optionally set the environment variables via a HashMap
named environment
:
ProcessBuilder pb = new ProcessBuilder(args);
Map<String, String> env = pb.environment();
if (environment != null)
env.putAll(environment);
Process process = pb.start();
The env
variable gets set properly, if I dump it to the console, with a correct value for the PATH variable. However, running the process results in a thrown Exception
:
java.io.IOException: error=2, No such file or directory
The same process runs fine with identical environment variables in the terminal shell. To test this, I ran Eclipse AFTER setting the environment in the terminal. In this case the ProcessBuilder
process runs correctly.
So what must be happening is that the ProcessBuilder
is not using the environment I set for it, but the current System environment instead.
I can't find any satisfactory answers to this problem online. Perhaps this is an OS-specific issue? Or something else I'm missing?
I don't think it's a bug, I think it's a problem with your understanding of the boundaries and roles of the environment variables at play. ProcessBuilder.environment()
contains environment variables that will be "process-local" to the spawned process. They are not system-wide, or logon-wide, and they don't even affect the environment in which the ProcessBuilder is running.
The ProcessBuilder.environment()
map contains process-local variables that will be seen only by the spawned process. Obviously a prerequisite to the spawned processing seeing the ProcessBuilder.environment()
is successful spawning of the process, which is a point I do not think you're even getting to.
As far as I know, it's not really possible (from Java) to modify the currently-running process' PATH, which is what I think you're expecting to happen (or to be able to do.) So I think you must point ProcessBuilder to the fully qualified path to the executable you're trying to launch (or be certain that the PATH was set up correctly before you even launch the JVM that will use the ProcessBuilder, which is what you did in your 'working' scenario of setting it in the terminal before launching your IDE).
这篇关于设置 ProcessBuilder 的环境的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:设置 ProcessBuilder 的环境
基础教程推荐
- FirebaseListAdapter 不推送聊天应用程序的单个项目 - Firebase-Ui 3.1 2022-01-01
- 如何使用 Java 创建 X509 证书? 2022-01-01
- 设置 bean 时出现 Nullpointerexception 2022-01-01
- 无法使用修饰符“public final"访问 java.util.Ha 2022-01-01
- Java Keytool 导入证书后出错,"keytool error: java.io.FileNotFoundException &拒绝访问" 2022-01-01
- 降序排序:Java Map 2022-01-01
- “未找到匹配项"使用 matcher 的 group 方法时 2022-01-01
- 减少 JVM 暂停时间 >1 秒使用 UseConcMarkSweepGC 2022-01-01
- 在 Libgdx 中处理屏幕的正确方法 2022-01-01
- Java:带有char数组的println给出乱码 2022-01-01