linux - Setting and getting environment variables by "source" a shell script from Java code -
I'am is writing a Java console application that takes input parameters from the environment variable (linux). To set these variables, my application can "script" open script "source" source of script with "I Runtime.exec () or ProcessBuilder, but I do not know how to reach the created environment variable Receive
my shell script (set_env.sh):
#! / Bin / csh setenv MY_DB DB_NAME To code the "script" above the Java code:
ProcessBuilder pb = new process builder ("csh", "-c", "'source set_env.sh' '); Process P = not ; Try {p = pb.start (); p.waitFor ();} hold (IOException | paused exception e) {e.printStackTrace ();} I created MY_DB variable How can I gain access or any other solution to my problem?
Any help would be greatly appreciated. Thank you very much.
OK friends, your answer Thanks for the help of my temporary solution I have written another script which creates "source" and the variable is created in the java code i'am it It is uniting, reading the inputstream of the process and parsing them.
You can access System # getenv () and System # getenv (string name) . Variation of import java.util.Map; Public class nanop {public static zero main (string [] args) {map & lt; String, string & gt; Env = System.getenv (); (String envName: env.keySet ()) {System.out.format ("% s =% s% n", envName, env.get (envName)); However, as described by Sylvain Laroux and Serge Blasta, the child's process is made with the process #start () , its environment can not be modified Parents If you want to set some variables only used by your java code, then Java is probably better suited.
Comments
Post a Comment