Hi everyone, in this post I just want to write a reminder for me on how to set JAVA_HOME in linux ubuntu 18.04. I hope it will help you too, especially for you who new in linux.
Download JDK Version
For my case I have downloaded few JDK versions, it is important for me because I have some projects. There is certain project use JDK 7 and the other project use JDK 8. I downloaded the both version and extracted to /opt/java-jdk
muhammad@muhammad-laptop:~$ cd /opt/java-jdk/ muhammad@muhammad-laptop:/opt/java-jdk$ ls jdk1.7.0_80 jdk1.8.0_171 muhammad@muhammad-laptop:/opt/java-jdk$
Register Java & Javac to update-alternatives
We need to register java & javac command to update-alternatives for both versions. In this example Im gonna set JDK 8 as choice #1 and JDK 7 as choice #2
sudo update-alternatives --install "/usr/bin/java" "java" "/opt/java-jdk/jdk1.8.0_171/bin/java" 1 sudo update-alternatives --install "/usr/bin/javac" "javac" "/opt/java-jdk/jdk1.8.0_171/bin/javac" 1
sudo update-alternatives --install "/usr/bin/java" "java" "/opt/java-jdk/jdk1.7.0_80/bin/java" 2 sudo update-alternatives --install "/usr/bin/javac" "javac" "/opt/java-jdk/jdk1.7.0_80/bin/javac" 2
Easy to Change Java Version When Needed
After both jdk version has installed it is time to choose which version Im gonna use.
sudo update-alternatives --config java sudo update-alternatives --config javac
When Im gonna use JDK 8 I can enter number 1 from keyboard otherwise number 2 will be choosen when Im gonna use JDK 7. Do not forget to test by using command
muhammad@muhammad-laptop:~$ javac -version javac 1.8.0_171 muhammad@muhammad-laptop:~$ java -version java version "1.8.0_171" Java(TM) SE Runtime Environment (build 1.8.0_171-b11) Java HotSpot(TM) 64-Bit Server VM (build 25.171-b11, mixed mode) muhammad@muhammad-laptop:~$