I have created a RHEL Linux VM and installed Oracle JDK in it.
Post creation of VM, I am executing script using Custom Script extension in VM. Below is my script.
Myscript.sh
echo "$$ $JAVA_HOME $$" >> output.log
echo `env` >> output.log
output.log
$$ $$
PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin PWD=/var/lib/waagent/custom-script/download/1 LANG=en_US.UTF-8 SHLVL=4 _=/usr/bin/env
When I ran the above script, I don't see JAVA_HOME is set. But I see JAVA_HOME value when I logged into the machine.
Update:-
During installation of Oracle JDK, I set JAVA_HOME in /etc/profile.
script1.sh
javaHomeValue=$(cat /etc/profile | grep JAVA_HOME | awk -F= '{print $2}')
echo " -- $javaHomeValue -- " >> output.log
output.log
-- /usr/java/jdk1.8.0_172-amd64 --
I am able to get JAVA_HOME using the above script1.sh
Is there any reason, Why JAVA_HOME is not set in environment variables during execution of Custom script?
For your issue, if you want to set JAVA_HOME as environment variable in file /etc/profile, you can add code
export JAVA_HOME=/usr/java/jdk1.8.0_172-amd64
into /etc/profile. Then you can see the environment variable JAVA_HOME in a new terminal.