How to Set an Environment Variable at Runtime from Java

How do I set environment variables from Java?

(Is it because this is Java and therefore I shouldn't be doing evil nonportable obsolete things like touching my environment?)

I think you've hit the nail on the head.

A possible way to ease the burden would be to factor out a method

void setUpEnvironment(ProcessBuilder builder) {
Map<String, String> env = builder.environment();
// blah blah
}

and pass any ProcessBuilders through it before starting them.

Also, you probably already know this, but you can start more than one process with the same ProcessBuilder. So if your subprocesses are the same, you don't need to do this setup over and over.

How to set environment variable in java

Simple example for how to set path with setx.exe in command line:

setx path "jdk bin path"

ex

setx path "C:\Program Files (x86)\Java\jdk1.7.0_04\bin"

try this on your code

like

    try {

// using the Runtime exec method:
Process p = Runtime.getRuntime().exec("setx path C:\Program Files (x86)\Java\jdk1.7.0_04\bin");

BufferedReader stdInput = new BufferedReader(new
InputStreamReader(p.getInputStream()));

BufferedReader stdError = new BufferedReader(new
InputStreamReader(p.getErrorStream()));

// read the output from the command
System.out.println("Here is the standard output of the command:\n");
while ((s = stdInput.readLine()) != null) {
System.out.println(s);
}

// read any errors from the attempted command
System.out.println("Here is the standard error of the command (if any):\n");
while ((s = stdError.readLine()) != null) {
System.out.println(s);
}

System.exit(0);
}
catch (IOException e) {
System.out.println("exception happened - here's what I know: ");
e.printStackTrace();
System.exit(-1);
}

Runtime reload of environment variables

Depending on your OS platform, this is not possible; each process gets a copy of the environment variables, there isn't really such a thing as a 'global' environment table - the environment variables of a process that is in charge of launching most processes seems global-ish, perhaps, as you inherit environment variables from the process that launched your process (unless during your process being launched, modifications to the environment variables were applied), so what you're effectively asking for is 'can I observe any changes to environment variables as in the process that spawned me', which is, amongst other things, rather hairy from a security perspective, which leads to the rather obvious answer of: No, you can't do that.

Maybe take a step back and describe the actual problem you're trying to solve. Maybe you want the ability to live-configure your application, and you chose 'this setting will be conveyed to the app by way of my OS's environment variable system' - which is one of many ways to accomplish the goal of having a setting system. It's a way that does not lend itself to live updates very well.

If you instead convey settings via a file, well, it is much simpler to keep a 'watch' on a file and trigger a re-read of it when it changes, for example.

How to set environment variable dynamically in spring test

You can supply the password (or any Spring property) at runtime via System or Environment or Command Line variables. All of those sources are (a) defined at runtime and (b) external to your code base.

For example:

  • export password=...; java -jar app.jar sets an environment variable named password which will then be present in your Spring Environment
  • java -Dpassword=... -jar app.jar sets a JVM system parameter which will then be present in your Spring Environment
  • java -jar myapp.jar --password=... sets a command line variable which will then be present in your Spring Environment

You can even source a property from JNDI.

More details in the docs.

How to set an environment variable in Java using exec?

This won't work. When you start a new process, that process receives a copy of the environment. Any changes it then makes to environment variables are made within that copy, and at no point will become visible to the caller.

What are you actually trying to achieve?

How to set environment variable from java code and use this variable without restarting my workspace

By default the environemnt block is copied from parent process to child process, but you can change the environment copy of the process you start.

public class EnvironmentTest
{
public static void main(String[] args) throws Exception
{
ProcessBuilder pb = new ProcessBuilder(
"CMD.exe"
, "/C"
, "start \"\" cmd /c \"set e & pause\""
);
pb.environment().put("ENVVAR", "This is a test");
Process p = pb.start();
}
}

The cmd instance started inside the ProcessBuilder has the environment variable defined. To show it, this instance is starting another one (the start "" cmd ...) to show in screen the value in the variable.

Probably in your case you will need something like

public class EnvironmentTest
{
public static void main(String[] args) throws Exception
{
ProcessBuilder pb = new ProcessBuilder(
"CMD.exe"
, "/C"
, "start \"\" \"COMMANDTHATUSEEN‌​VAR\""
);
pb.environment().put("ENVVAR", "This is a test");
Process p = pb.start();
}
}

How to make environment variables work in a JAR file?

Simplest solution is to run it as: R_HOME=/usr/lib64/R java -jar xxx.jar

This would set an environment variable named R_HOME for the execution of your jar



Related Topics



Leave a reply



Submit