在 Java 中以编程方式设置 Linux 环境变量
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/14022487/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Set Linux environment variable programmatically in Java
提问by Ahmet Karakaya
I am able to run a Linux command via RunTime
class.
Is there a way of setting a Linux global enviroment from Java programatically?
我能够通过RunTime
类运行 Linux 命令。有没有办法以编程方式从 Java 设置 Linux 全局环境?
I want to emulate the following Linux command statement over Java
我想通过 Java 模拟以下 Linux 命令语句
root@machine:/tmp# export TEST=v2
I have used ProcessBuilder as following, but TEST variable does not changed.
我使用 ProcessBuilder 如下,但 TEST 变量没有改变。
ProcessBuilder pb = new ProcessBuilder("/bin/bash","-c","export TEST=v3" + "&& exec");
UPDATE: Actually my ultimate target is to use EMAIL_NAME enviroment as an email address, when both my application and machine are started and stopeed these actions will be sent to EMAIL_NAME. In that case I understand that it is not possible to set linux global enviroment over pure Java code. So I have a workaround solution is that EMAIL_NAME will be hold in a file and it will be updated or read by linux script or java code.
更新:实际上我的最终目标是使用 EMAIL_NAME 环境作为电子邮件地址,当我的应用程序和机器启动和停止时,这些操作将发送到 EMAIL_NAME。在那种情况下,我知道不可能在纯 Java 代码上设置 linux 全局环境。所以我有一个变通的解决方案是 EMAIL_NAME 将保存在一个文件中,它将被 linux 脚本或 java 代码更新或读取。
采纳答案by Ryan Stewart
Environment variables, when set, are only available to processes spawned by the process where the variable is set, so if you're really asking to set a variable that will affect the entire system, then no. You can't really do that at all in Linux interactively. The best you could do is alter one of the system startup files to include said variable so that anything you do in the futurewould include that variable.
环境变量在设置时仅可用于设置变量的进程产生的进程,因此如果您真的要求设置一个会影响整个系统的变量,那么不。在 Linux 中,您根本无法以交互方式真正做到这一点。您能做的最好的事情是更改系统启动文件之一以包含所述变量,以便您将来执行的任何操作都包含该变量。
If you're simply looking to run several processes with some variable set, then ProcessBuilderallows you to set an environmentfor processes spawned with it. Reusing an environment for several processes is pretty trivial with that.
如果您只是想用一些变量集运行多个进程,那么ProcessBuilder允许您为用它产生的进程设置环境。为多个进程重用环境非常简单。
回答by TieDad
When you say "Linux global environment", do you mean Linux environment variable? If so, I think the answer is NO. Because it's not a java problem. When a process set environment var, it could only impact itself and its children process.
当您说“Linux 全局环境”时,您是指 Linux 环境变量吗?如果是这样,我认为答案是否定的。因为这不是java问题。当进程设置环境变量时,它只能影响自身及其子进程。
回答by Vamsi Mohan Jayanti
You can update the variable into /etc/profile through java
您可以通过java将变量更新到/etc/profile
if(System.getProperty("TEST", "").equalsIgnoreCase(""))
{
Runtime.getRuntime().exec(new String[]{"bash","-c" ,"echo 'export TEST=v2' >> /etc/profile"}) ;
/// Also update the System Variable for current process
System.setProperty("TEST","v2") ;
}
And this will get effective from next login or if you do explicitly source it.
这将在下次登录时生效,或者如果您明确地获取它。
回答by Rami Jamleh
I use this line of code in the bash
我在 bash 中使用这行代码
echo -e PATH=\"/usr/lib/jvm/java-7-openjdk-i386/bin:$PATH\" >> $HOME/.profile
with concatenates the path of the local user
with 连接本地用户的路径