本文介绍了是否可以在运行时从 Java 设置环境变量?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否可以在运行时从 Java 设置环境变量应用?在 Java 1.5 java.lang.System 类中有 getenv() 方法,我会只需要一个 setenv() 方法...

Is it possible to set an environment variable at runtime from a Javaapplication?In Java 1.5 java.lang.System class there is the getenv() method, I wouldonly need a setenv() method...

是否可以在java进程本身修改环境变量;不在子进程中.

Is it possible to modify the environment variables in the java process itself; not in the child process.

可以通过JNI来实现吗?那将如何运作?

Is it possible to achieve it through JNI? And how would that work?

谢谢.

好的,让我这样说 - 我们可以用 Java 做以下事情吗?请回答.

Ok let me put it this way - Can we do the following with Java. Please answer.

  1. 我们可以修改当前进程的环境吗?
  2. 我们可以修改父进程的环境吗?
  3. 我们可以修改子进程的环境吗?

Hemal Pandya 回答说:您可以修改当前和子进程的环境,但不能修改生成该进程的父进程的环境."你同意吗?

Hemal Pandya has answered that "You can modify the environment of current and child processes but not of the parent process that spawned this process." Do you agree with this ?

推荐答案

如果我的直觉是正确的,并且您实际上想要修改环境以使生成的(分叉的)子进程受益(Runtime.getRuntime().exec()),然后使用 ProcessBuilder 而不是 exec().您可以通过 ProcessBuilder 实例的 environment() 方法.

If my intuition is correct, and you actually want to modify the environment for the benefit of a spawned (forked) sub-process (Runtime.getRuntime().exec()), then use ProcessBuilder instead of exec(). You can build a custom environment via your ProcessBuilder instance's environment() method.

如果这不是你想要达到的目标,那么请忽略这个答案.

If this is not what you are trying to achieve then kindly disregard this answer.

更新

您的三个更新的具体问题的答案如下:

The answer to your three updated, specific questions is as follows:

  1. 我们可以修改当前进程的环境吗?
    • 不容易.取决于您是要更改进程的环境,还是要更改同一 JVM 中 System.getenv() 返回的值,还是两者兼而有之.
    • 正如 Greg Hewgill 指出的那样,要更改当前进程的环境,您可以通过 JNI 调用 setenv 或其特定于平台的等效项.您也可以使用下面 第 2 点中的极其复杂的方法,该方法适用于任何进程(只要您有权限).但是,请注意,在大多数 JVM 中,这种更改可能永远不会反映在值中由 System.getenv() 返回,因为环境通常在虚拟机启动时缓存在 java.util.Map(或等效项)中.
    • 要更改环境的 JVM 缓存副本,当使用缓存时(请参阅 System.java 中的源代码,在您将用于部署的任何 JVM 发行版中),您可以尝试 hacking实现(通过类加载顺序,reflection,或 instrumentation.) 在 SUN 的 v1 的情况下.6 JVM,例如,环境缓存由未记录的 ProcessEnvironment 类管理(您可以对其进行修补.)
  1. Can we modify the environment of the current process?
    • Not easily. Depends whether you want to change the process' environment, to change the value(s) returned by System.getenv() in the same JVM, or both.
    • As Greg Hewgill pointed out, to change the current process' environment you can call setenv or its platform-specific equivalent via JNI. You may also employ the extremely convoluted method from point 2 below, which works for any process (provided you have the permissions.) However, be aware that in most JVMs this change might never be reflected in the values returned by System.getenv(), as the environment is more often than not cached at virtual machine startup in a java.util.Map (or equivalent.)
    • To change the JVM's cached copy of the environment, when a cache is used (see the source code in System.java in whichever JVM distribution you will be using to deploy), you may try hacking the implementation (via class loading order, reflection, or instrumentation.) In the case of SUN's v1.6 JVM, for example, the environment cache is managed by the undocumented ProcessEnvironment class (which you can patch.)
  • 极其困难,非常不便携.如果您绝对必须这样做,您可以使用一些非常具体的技巧:
    • Extremely difficult, and highly non-portable. If you absolutely and imperatively have to, there are very specific hacks that you can employ:
      • Windows: Dynamically Add/Edit Environment variables of Remote process
      • *nix: Is there a way to change another process’s environment variables? -- this is a performance killer, as any process instrumented by gdb will be suspended for a non-zero amount of time.
      • ,在生成进程时通过 ProcessBuilder.
      • 如果在需要环境改变的时候进程已经被派生了,你需要上面的方法2(或者一些同样复杂的方法,比如在spawn时注入代码,通过socket进行隐蔽控制由父进程.)
      • Yes, through ProcessBuilder when spawning the process.
      • If the process has already been spawned when the environment alteration is required, you need method 2 above (or some equally convoluted method, such as code-injection at spawn time, ulteriorly controlled through e.g. socket by the parent process.)

      请注意,除了涉及 ProcessBuilder 的方法之外,上述所有方法都很脆弱、容易出错、在不同程度上不可移植,并且在多线程环境中容易出现竞争条件.

      Note that all methods above, except for the one involving ProcessBuilder, are brittle, error prone, non-portable to various degrees, and prone to race conditions in multi-threaded environments.

      这篇关于是否可以在运行时从 Java 设置环境变量?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-23 08:02
查看更多