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
Could Not Reserve Enough Space for Object Heap to Start Jvm
Rjava Install Error "Java_Home Cannot Be Determined from the Registry"
How to Support Both Ipv4 & Ipv6 on Java
How to Create Native Binaries for Your Java App
Commons Vfs and Java.Net.Url - Adding Support for "Sftp://" Protocol
Local_Policy.Jar and Us_Export_Policy.Jar Different with Unlimited Strength VS Default
Create a Shell Script to Run a Java Program on Linux
How to Use Chef to Update-Alternatives for Java Using Execute
Why Stringbuilder Stops Adding Elements After Using the Null Character
Eclipse Release Heap Back to System
Benefits/Drawbacks to Running 64-Bit Jvm on 64-Bit Linux Server
How to Set Up Sublime Text 3 to Run and Compile Java on Linux
Can't Start Cassandra (Single-Node Cluster on Centos7)
Connect as User with No Password Set on Postgresql 8.4 via Jdbc
How to Pass an Error Up the Stack Trace in Swift