I'm not sure I'm aware of what I'm doing. Any comments or suggestions will be appreciated.
$ cat java7
#!/bin/sh
alternative=java-7-oracle
sudo update-java-alternatives -s $alternative
export JAVA_HOME=/usr/lib/jvm/$alternative
$ cat java8
#!/bin/sh
alternative=java-8-oracle
sudo update-java-alternatives -s $alternative
export JAVA_HOME=/usr/lib/jvm/$alternative
$ . java7;javac -version;echo $JAVA_HOME
javac 1.7.x_yy
/usr/lib/jvm/java-7-oracle
$ . java8;javac -version;echo $JAVA_HOME
javac 1.8.x_yy
/usr/lib/jvm/java-8-oracle
UPDATE
I'm updating for myself and anyone who needs.
First I added following line to my ~/.bashrc
.
#http://serverfault.com/a/276221/113357
export JAVA_HOME=$(readlink -f /usr/bin/java | sed "s:bin/java::")
And my new ~/bin/java_home
scripts.
#!/bin/bash
if [ $# -ne 1 ]
then
echo please select a jname.
update-java-alternatives -l
return;
fi
jname=$1
sudo update-java-alternatives -s $jname
source ~/.bashrc
And when I want to change the JDK, I do.
$ source java_home java-7-oracle
$ source java_home java-8-oracle
The JAVA_HOME
variable changes even in any new terminals. Previous java7
and java8
changed like this.
$ echo java7
#!/bin/sh
source java_home java-7-oracle
$ echo java8
#!/bin/sh
source java_home java-8-oracle
$
sh
andsource
.export
command to the first script.