12

I am able to run a Linux command via RunTime class. Is there a way of setting a Linux global enviroment from Java programatically?

I want to emulate the following Linux command statement over Java

root@machine:/tmp# export TEST=v2

I have used ProcessBuilder as following, but TEST variable does not changed.

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.

Ahmet Karakaya
  • 9,899
  • 23
  • 86
  • 141
  • What do you mean by `Linux` command here? – Kazekage Gaara Dec 24 '12 at 14:21
  • If you use a `ProcessBuilder`, you can have a mutable map of the environment using `.environment()` and modify that map -- before executing the command, of course. – fge Dec 24 '12 at 14:22
  • Similar answers: http://stackoverflow.com/questions/580085/is-it-possible-to-set-an-environment-variable-at-runtime-from-java - though using `ProcessBuilder` will only set the environment for all forked processes (processes run as a result of being invoked from your Java process). – wkl Dec 24 '12 at 14:23
  • 1
    There is a long discussion on this subject here: http://stackoverflow.com/questions/318239/how-do-i-set-environment-variables-from-java – Mats Petersson Dec 24 '12 at 14:24

4 Answers4

11

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 future would include that variable.

If you're simply looking to run several processes with some variable set, then ProcessBuilder allows you to set an environment for processes spawned with it. Reusing an environment for several processes is pretty trivial with that.

Ryan Stewart
  • 126,015
  • 21
  • 180
  • 199
3

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.

TieDad
  • 9,143
  • 5
  • 32
  • 58
  • 2
    It's not a Java problem as such, is it? If I start a new terminal window, do "export TEST=v2" and then exit, it won't exist in the environment of the parent process. Same applies if I write in C, C++, pascal, assembler, Java, Python or whatever other language we can think of. – Mats Petersson Dec 24 '12 at 14:27
  • @MatsPetersson My typo. I just found I missed NOT. I meant "it's not a java problem". – TieDad Dec 24 '12 at 14:30
  • Ah, yes, that makes the whole sentence much better! – Mats Petersson Dec 24 '12 at 14:31
3

You can update the variable into /etc/profile through java

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.

jww
  • 97,681
  • 90
  • 411
  • 885
  • everytime there will be new TEST varible is inserted into /etc/profile. – Ahmet Karakaya Dec 24 '12 at 15:10
  • Only if you run the java program every time. But I assume you want to run it only once. On the other hand /etc/profile will get executed every time you a user logs in. – Vamsi Mohan Jayanti Dec 24 '12 at 15:12
  • Besides if you plan to run the Java program everytime .. then probably you should check first if the variable exists.. System.getProperty("TEST") .. if it doesn't exist then set it in both current process environment and /etc/profile – Vamsi Mohan Jayanti Dec 24 '12 at 15:17
  • ***Plus One*** for actually answering the question asked by OP. – jww Jul 08 '17 at 12:05
-2

I use this line of code in the bash

echo -e PATH=\"/usr/lib/jvm/java-7-openjdk-i386/bin:\$PATH\" >> $HOME/.profile

with concatenates the path of the local user

Rami Jamleh
  • 1,819
  • 1
  • 13
  • 10
  • This should probably be a comment; not an answer. The question was, *Set Linux environment variable programmatically in Java*. Why would you provide a Bash recipe when the OP specifically asked for *"programmatically in Java"*? – jww Jul 08 '17 at 12:00