简体   繁体   中英

Sourcing env variables into Java from a text file, bash-style

I have a bash file (let's call it bash_file) which contains a number of environment variable assignments (eg TOOLS_DIR="/opt/tools" ) as well as bash code which sources another file (eg source /opt/other_file ) and constructs environment variables from the environment variables sourced from other_file (eg 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:

  1. In the same process with the source bash_file command, add the current env to 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.

  2. Again in the same process as source_bash_file , save current env to 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.

  3. Figure out where Java's bash environment is instantiated (likely in java.System.lang) and modify in order to perform the source bash_file before 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!

No, there's no general method for running bash code inside Java. But it's very common for Java programs to be invoked via shell script; are you sure yours doesn't already work that way? If it does, that's the most straightforward place to source the other bash file before executing your program.

If that's not an option for some reason, then #2 is the best solution. #1 and #3 are no good; you don't want to be changing system-wide or account-wide settings just to be able to run this program- especially if you ever plan to have other people use it.

The parsing for the #2 option doesn't need to be very hairy. Just output the environment variables using the set builtin, or something similar, and in your Java parser simply read one line at a time and split on the first = sign on each line. If a variable contains a newline, bash should escape it inside a dollar-quoted string, which would probably confuse your parsing but you probably don't need to worry about embedded newlines, so treat the result as garbage. You also probably want to dequote values contained in '' or "" , and maybe even handle backslash escapes in that second case, but in most cases like this the backslash-parsing wouldn't be necessary.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM