Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Access shell environment variables in Java

Does anyone know how to access the environment variables of the standard shell environment in Java? I am using the ProcessBuilder class and I have to specify specific environment variables used in a shell script I am running, these variables exist in the standard shell environment.

Accessing ProcessBuilder environment does not work.

like image 957
dominic Avatar asked Oct 18 '10 13:10

dominic


People also ask

How do you access shell environment variables?

Shell variables are only present in the shell in which they were defined. Environment variables are inherited by child shells but shell variables are not. Shell variable can be made an environment variable by using export command. A script is simply a collection of commands that are intended to run as a group.

How do you read an environment variable in Java?

How to get the value of Environment variables? The System class in Java provides a method named System. getenv() which can be used to get the value of an environment variable set in the current system.

How do I change environment variables in shell?

To make permanent changes to the environment variables for all new accounts, go to your /etc/skel files, such as . bashrc , and change the ones that are already there or enter the new ones. When you create new users, these /etc/skel files will be copied to the new user's home directory.

What is the environment variable for the shell value?

In the C shell, a set of these shell variables have a special relationship to a corresponding set of environment variables. These shell variables are user, term, home, and path. The value of the environment variable counterpart is initially used to set the shell variable.


1 Answers

You can get at the environment variables that existed when your program was created through System.getenv():

http://download.oracle.com/javase/tutorial/essential/environment/env.html

When a Java application uses a ProcessBuilder object to create a new process, the default set of environment variables passed to the new process is the same set provided to the application's virtual machine process. The application can change this set using ProcessBuilder.environment.

It looks like your child process should get your environment automatically.

like image 72
dsolimano Avatar answered Sep 26 '22 00:09

dsolimano