I have created a Bash script with the following line:
export JAVA_HOME="/Library/Java/JavaVirtualMachines/jdk1.7.0_75.jdk/Contents/Home" 2> bash_errors.txt
I have also made the file executable
chmod 777 bash_test
When I run the script and check
java -version
java version "1.8.0_65"
Java(TM) SE Runtime Environment (build 1.8.0_65-b17)
Java HotSpot(TM) 64-Bit Server VM (build 25.65-b01, mixed mode)
However if I run directly in Bash
export JAVA_HOME="/Library/Java/JavaVirtualMachines/jdk1.7.0_75.jdk/Contents/Home"
And then check
java -version
java version "1.7.0_75"
Java(TM) SE Runtime Environment (build 1.7.0_75-b13)
Java HotSpot(TM) 64-Bit Server VM (build 24.75-b04, mixed mode)
The path changes. Furthermore, if I change the script line to
sudo export JAVA_HOME="/Library/Java/JavaVirtualMachines/jdk1.7.0_75.jdk/Contents/Home" 2> bash_errors.txt
I get the following in the bash_errors.txt
file
sudo: export: command not found
So I know the script is executing, but JAVA_HOME doesn't change when executed from script.
What am I not understanding? How do I change the JAVA_HOME environment variable from a script?
My goal is to permanently change JAVA_HOME from 8 to 7 - I am eventually going to add .bash_profile to my default directory.
Edit
Trying to change files mentioned in linked reference are read only, even using sudo vim
: