13

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.

4
  • What do you mean by Linux command here? Commented Dec 24, 2012 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. Commented Dec 24, 2012 at 14:22
  • Similar answers: stackoverflow.com/questions/580085/… - though using ProcessBuilder will only set the environment for all forked processes (processes run as a result of being invoked from your Java process). Commented Dec 24, 2012 at 14:23
  • 1
    There is a long discussion on this subject here: stackoverflow.com/questions/318239/… Commented Dec 24, 2012 at 14:24

3 Answers 3

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.

Sign up to request clarification or add additional context in comments.

Comments

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.

3 Comments

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.
@MatsPetersson My typo. I just found I missed NOT. I meant "it's not a java problem".
Ah, yes, that makes the whole sentence much better!
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.

4 Comments

everytime there will be new TEST varible is inserted into /etc/profile.
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.
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
Plus One for actually answering the question asked by OP.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.