本文介绍了是否可以在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())的利益,然后使用而不是 exec()。您可以通过 ProcessBuilder 实例的方法。

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发行版中),您可以尝试黑客实现(通过类加载顺序,或。)在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.)

  • 非常困难,高度不可移植。如果你绝对和必须的,你可以使用非常具体的黑客:


    • Windows:

    • * nix: - 这是一个性能杀手,因为任何由 gdb 调用的进程将被暂停非零时间。

    • 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 (或者一些相当复杂的方法,例如在生成时的代码注入,

      • 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
查看更多