Setting a java ProcessBuilder environment with a bash script - java

I've been using ProcessBuilder to successfully invoke a process with various environment variables using env.put("VAR","value").
Now I'd like to source some bash scripts to set a whole bunch of environment variables that are not predetermined within java.
Anyone know of an easy way to do this?

bash supports the environment variable BASH_ENV on startup. Set the variable to your script and its contents will be sourced before execution. See bash(1) for details.

If your "batch scripts" to be sourced are in properties format, you can always load them using Properties and merge into the env.

Related

Is it possible to "fool" java that an environment variable is set from the command line

I'm running in an environment where I can pass parameters but not set environment variables using a normal commandline.
I would like to set environment variables nevertheless. Is there an alternative way to "fool" java that an environment variable is set, e.g. using -D parameters?
(I'm running spark in oozie through hue; all in the Cloudera stack).
The java command itself doesn't seem to allow that. It has a -D parameter, but that sets Java 'system properties':
$ java -help 2>&1 | grep -A1 '\-D'
-D<name>=<value>
set a system property
Java system properties are a sort of Java properties. Like environment variables, java properties are key-value pairs, but aren't the same thing as environment variables: If your Java application reacts to a specific environment variable, setting a system property of the same name won't have any effect unless the application explicitly reacts to that property, too.
If your environment allows you to run arbitrary Java applications and if it allows your Java applications to execute other processes, you can write a little wrapper that sets the environment variables on a ProcessBuilder (see the question Arnon linked in his comment: How do I set environment variables from Java?) to then invoke java with your actual JAR from it. You could either hard-code the environment variables to set, or set them according to system properties the wrapper receives. (Or you could even implement your own shell in Java and pass a script to it.)
Though, if you can modify the source of that actual JAR, a much more idiomatic solution would be to make it itself react to properties instead of (or additionally to) environment variables. (Unless you have to control environment variables that the java command / the JVM reacts to rather than the JAR you'd like to run. Then this approach would not be applicable, of course.)
If you can run arbitrary Java code, you can create and run a Process using ProcessBuilder, including an environment of your choice.
Hence, write a java program that parses a command-line like
java -cp .... your.Prog FOO=BAR BAZ=BOOM command arguments ...
and starts command with the environment extended by FOO and BAZ
(Note that command could be java ...)

setting classpath using setenv.sh for a project

I want to write an sh file that will set classpath in Linux.
I tried using:
export ClASSPATH=$CLASSPATH:$ABC_HOME
However, when I run the .sh file it doesn't set the classpath.
Executing the bash script only sets the environment for the child process. The "source" command may do what you want...
https://askubuntu.com/questions/53177/bash-script-to-set-environment-variables-not-working
You should make it a shell function, or source it. Because the environment variables are local to the shell (the one started by invoking your shell script).
Please read the advanced bash scripting guide. See also this answer to a similar question.

Setting\Getting Environment Variables through batch file inside java program

I am facing a problem where I need to run a bat file from java program.
This Batch file contains certain SET ms-dos command which i need to access further down in my application.
I used ProcessBuilder Class to invoke the batch file but processBuilder.environment() is not returning me the variables set by the batch file.
DOS SET command sets the variables on that prompt only.
I am unable to find the solution for this problem.
Is there any ulternate approach for such issue. I cant change the Batch file.
Regards
Abhay
System.getenv lets you get an environmental variable. It should work in this context too. There is another version which returns a Map of environmental variables.
Your ProcessBuilder instance holds the environment variables used by the child process. You should reference processBuilder.environment() rather than referencing the environment variables for the parent process found in System.getenv().
However, you may find that it's still a one-way street. You can set environment variables there to pass to your process shell, but the changes made by the SET commands will not survive the return trip.
So, here's an alternate approach. Write a batch file as a wrapper script and have that output your desired variables to somewhere more easily accessed, like stdout, stderr, or a file.
wrapper.bat
===========
#echo off
call "D:\build\XL_7_12\XL_7_12\build\xl_env.cmd"
echo ROOT=%ROOT%,FOLDER=%FOLDER%
Then you call your wrapper.bat from Java and collect and parse the ROOT and FOLDER values from the last line of your process.getInputStream().
Another approach would be to redirect the output of the ECHO command to a file, then read this later from Java. eg.
echo ROOT=%ROOT%,FOLDER=%FOLDER% > \temp\vars.txt
Then read \temp\vars.txt from java. This might be a little easier than reading the input stream. Finally, be aware that instead of a wrapper.bat file, you can issue two commands to a single copy of the command.exe interperter,, thus sharing environments, so you could:
D:\build\XL_7_12\XL_7_12\build\xl_env.cmd & ECHO %ROOT% > \temp\vars.txt

Setting Shell Environment Variables from Within a Process from Java on Linux

If I had a java application that needed specific environment variables to be set, but I cannot place them inside the bash.rc or bash_profile, nor can I place them in /etc/profile.d is it possible to have them set within the Java process? Or do these need to be set before the java process is run? Also manually setting them each time is not an option because I want them to be transparent to the user.
Start the java process from a shell script. The script would first define and export the environment variables.
I suspect this is not possible. System.getenv() is an unmodifiable map, meaning you can't modify elements in it.
Environment Variables
And for setting environment variables, ProcessBuilder
Have you try ?
System.setProperty("KEY", "VALUE");

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.
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.
processBuilder.environment().get("variablename");

Categories