设置ProcessBuilder的环境 [英] Setting the environment for ProcessBuilder
问题描述
简而言之,我有一个运行本机进程的管道,它使用 java.lang.ProcessBuilder
。用户可以通过
HashMap
命名为环境
:
ProcessBuilder pb = new ProcessBuilder(args);
映射< String,String> env = pb.environment();
if(environment!= null)
env.putAll(environment);
进程进程= pb.start();
env
变量设置正确,if我把它转储到控制台,PATH变量的值正确。但是,运行该过程会导致抛出异常
:
java.io.IOException :error = 2,没有这样的文件或目录
相同的进程在终端shell中使用相同的环境变量运行正常。为了测试这个,我在终端设置环境后跑了Eclipse。在这种情况下, ProcessBuilder
进程正常运行。
所以必须发生的事情是,$ code> ProcessBuilder 不使用我为其设置的环境,而是当前系统环境。
我在线找不到任何令人满意的答案。也许这是一个操作系统特定的问题?还有其他的东西我失踪了?
我不认为这是一个错误,我认为这是一个你的理解问题的环境变量的边界和作用。 ProcessBuilder.environment()
包含对生成的进程将进程本地的环境变量。它们不是全系统或登录范围的,甚至不影响ProcessBuilder运行的环境。
据我所知,这不是真的可能(从Java)修改当前正在运行的进程PATH,这是我认为你期望发生的或者能够做到。)所以我认为你必须将ProcessBuilder指向你试图启动的可执行文件的完全限定路径(或者确定PATH是否正确设置,甚至启动JVM之前将使用ProcessBuilder,这是您在启动IDE之前在终端中设置它的工作场景中所做的工作。) 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 The 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 So what must be happening is that the 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. The 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的环境的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋! ProcessBuilder.environment()
map包含将被生成的进程仅 看到的进程局部变量。显然,生成处理的前提条件是查看 ProcessBuilder.environment()
是成功生成进程,这是我不认为你甚至不知道的一点。 / p>
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();
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
ProcessBuilder
process runs correctly. ProcessBuilder
is not using the environment I set for it, but the current System environment instead.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 effect the environment in which the ProcessBuilder is running.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.