When you install Java with default apt install
command on Ubuntu or CentOS Linux OS then it will install Java under default folder /usr/bin/java
.
First thing first. How to install or upgrade JVM in Linux OS?
Use below linux command to install latest Java in Ubuntu, CentOS:
root@crunchify:~# apt install openjdk-9-jre-headless Reading package lists... Done Building dependency tree Reading state information... Done openjdk-9-jre-headless is already the newest version (9~b161-1). 0 upgraded, 0 newly installed, 0 to remove and 0 not upgraded.
As you see above, I already have latest JDK 9 installed
and then there is no need to download any extra package here. For new re-imaged host it will completely install new Java/JDK binary for you.
After that – just use command which java
to find out CLASSPATH variable. In my case it’s /usr/bin/java
.
BEFORE:
root@crunchify:/usr/bin# which java /usr/bin/java
This is the default Java version available for you to use. With simple command java -version
you will see which JDK it referred to.
root@crunchify:/usr/bin# java -version openjdk version "1.8.0_121" OpenJDK Runtime Environment (build 1.8.0_121-8u121-b13-4-b13) OpenJDK 64-Bit Server VM (build 25.121-b13, mixed mode)
This is an ideal situation for most of the cases if you are the only one working on project. But it’s not the case for most of the companies out there 🙂
Usually, in production, companies ship Java with their project binary. During runtime they setup CLASSPATH and PATH
respectively to use preferred Java version.
Want to change default JVM version in Ubuntu Linux OS? Or CentOS Linux?
Let’s get started on changing default Java to JDK 9
root@crunchify:/tmp/crunchify# java -version openjdk version "1.8.0_121" OpenJDK Runtime Environment (build 1.8.0_121-8u121-b13-4-b13) OpenJDK 64-Bit Server VM (build 25.121-b13, mixed mode) root@crunchify:/tmp/crunchify# vi ~/.bash_profile // Put these 2 lines in it - change java location accordingly ==> export JAVA_HOME=/opt/java/jdk-9 ==> export PATH=$JAVA_HOME/bin:$PATH root@crunchify:/tmp/crunchify# source ~/.bash_profile root@crunchify:/tmp/crunchify# java -version java version "9" Java(TM) SE Runtime Environment (build 9+181) Java HotSpot(TM) 64-Bit Server VM (build 9+181, mixed mode) root@crunchify:/tmp/crunchify#
.bash_profile file content:
export JAVA_HOME=/opt/java/jdk-9 export PATH=$JAVA_HOME/bin:$PATH
AFTER:
Now run the same command which java to see updated java location.
root@crunchify:/tmp/crunchify# which java /opt/java/jdk-9/bin/java
As you see above, in order to reload all properties you need to run command source ~/.bash_profile
. Now onwards your Java will always be set using .bash_profile
file settings. Similar way you could also install Maven on Mac OS X.
Hope you get complete idea on how to change default Java version on Linux.