Set Environment Variable in Shell Script/Access in Java Program

Set environment variable in shell script/access in Java program

How are you sourcing the script?

$./myscript.sh 

or

$source ./myscript.sh 

The second will set the environment variable to current shell. The java program looks ok.

EDIT: based on the comment

It was a problem related to subshell. A quick read is

What is the difference between executing a bash script and sourcing a bash script?

Set environment variable in shell script/access in Tomcat Application

got the solution...what i have done is i have put the export statements in /etc/init.d/tomcat6 at top and restarted the server by command sudo /etc/init.d/tomcat6 restart. So now my web-app running in tomcat server can access that variable.

Set Linux environment variable programmatically in Java

Environment variables, when set, are only available to processes spawned by the process where the variable is set, so if you're really asking to set a variable that will affect the entire system, then no. You can't really do that at all in Linux interactively. The best you could do is alter one of the system startup files to include said variable so that anything you do in the future would include that variable.

If you're simply looking to run several processes with some variable set, then ProcessBuilder allows you to set an environment for processes spawned with it. Reusing an environment for several processes is pretty trivial with that.

How to I obtain the value of the environment variables?

For obtaining only one System Variable use the following code:

 String sysEnvStr = System.getenv("JAVA_HOME");

If it returns null then make changes in your .bashrc file. Try exporting that particular variable.



Related Topics



Leave a reply



Submit