I have a bash file (let's call it bash_file) which contains a number of environment variable assignments (e.g. TOOLS_DIR="/opt/tools") as well as bash code which sources another file (e.g. source /opt/other_file) and constructs environment variables from the environment variables sourced from other_file (e.g. NEW_ENV_VAR=$FILEPATH/$FILENAME where FILEPATH and FILENAME are assigned in other_file).
I have a Java application which requires visibility of these environment variables, but using the standard System.getenv() doesn't pick up these environment variables since the bash file hasn't been sourced in Java's shell yet. If I use Java's Runtime.exec("source bash_file"), then the environment variables are only present in Java's child shell, even if bash's export command is used.
If I were in pure bash, I'd simply perform my source bash_file and I'd have access to all the environment variables assigned in bash_file, but it's not that simple in Java.
I have thought of a few options already, none of which seem particularily elegant:
In the same process with the
source bash_filecommand, add the currentenvto the .bashprofile so that it will be available for the all future runtime exec calls, such as System.getenv().Problem: I'm not 100% sure that the getenv will even pick up on env vars added to the bashprofile during runtime, and having all of these variables exported globally isn't nice at all.
Again in the same process as
source_bash_file, save currentenvto a text file, and manually parse with Java.Problem: This would get the job done, but it's nowhere near as nice and elegant as the Map returned by
System.getenv(). Writing the parser in Java also seems quite tricky.Figure out where Java's bash environment is instantiated (likely in java.System.lang) and modify in order to perform the
source bash_filebefore initializing Java's environment, so the variables are inheireted by my Java app.Problem: This one seems like the messiest of all my options, and I'd have no idea where to begin looking for the initialization of Java's shell environment.
Is there any way to perform a bash-like source operation from Java, or alternatively, what is the simplest implementation for obtaining that functionality? I have also voiced a number of uncertainties in my evaluation of possible courses of action, and I would appreciate it if anyone could fill me in on those uncertainties.
Thanks!