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 ProcessBuilder
s 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 add an environment variable in Java?
Map<String, String> env = pb.environment();
env.put("MV_ENV_VAR", "1");
would set MY_ENV_VAR=1. Before you invoke the Process by
Process p = pb.start();
export
would only be interpreted by a shell.
See also ProcessBuilder
A full example:
public static void main(String[] args) throws IOException {
ProcessBuilder pb = new ProcessBuilder("CMD", "/C", "SET");
Map<String, String> env = pb.environment();
env.put("MYVAR", "myValue");
Process p = pb.start();
InputStreamReader isr = new InputStreamReader(p.getInputStream());
char[] buf = new char[1024];
while (!isr.ready()) {
;
}
while (isr.read(buf) != -1) {
System.out.println(buf);
}
}
prints among other environment values:
MYVAR=myValue
This should prove that the created process uses the manipulated environment.
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);
}
How to set JAVA environment variable in VS Code
In order to set environment variable for Spring boot application in VSCode, the recommended way is to create a launch.json file in .vscode folder of your project, then add the "env" section like the example below:
{
"configurations": [
{
"type": "java",
"name": "Spring Boot-DemoApplication<demo>",
"request": "launch",
"cwd": "${workspaceFolder}",
"console": "internalConsole",
"mainClass": "com.example.demo.DemoApplication",
"projectName": "demo",
"args": "",
"env": {
"PROJECT_NAME": "FOO_PROJECT"
}
}
]
}
How to set environment variables for Java
I suspect that the environment variable you're setting in your .bash_profile
isn't getting picked up.
If you're running from Eclipse, you need to set environment variables manually on the Environment tab in the Run Configuration.
Go to Run
-> Run Configurations...
, find or create the run configuration for your app under Java Applications
, go to the Environment
tab and add your desired environment variables there.
Click the Run
button and your program should print the environment variable as expected.
Related Topics
How to Read Xml Using Xpath in Java
Setting the Default Java Character Encoding
Retain Precision With Double in Java
Mocking Static Methods With Mockito
How to Really Read Text File from Classpath in Java
Why Don't Java'S +=, -=, *=, /= Compound Assignment Operators Require Casting
How to Download and Save a File from the Internet Using Java
How to Best Position Swing Guis
Java: Maintaining Aspect Ratio of Jpanel Background Image
What Causes Error "No Enclosing Instance of Type Foo Is Accessible" and How to Fix It
What's Wrong With Overridable Method Calls in Constructors
What Are the -Xms and -Xmx Parameters When Starting Jvm
Why Is 128==128 False But 127==127 Is True When Comparing Integer Wrappers in Java
Sending Email in Android Using Javamail API Without Using the Default/Built-In App
How to Convert a Byte Array to a Hex String in Java