Skip to content

Instantly share code, notes, and snippets.

@skanjo
Last active June 21, 2018 18:35
Show Gist options
  • Star 5 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save skanjo/f07d2d873b3413a6c6bd to your computer and use it in GitHub Desktop.
Save skanjo/f07d2d873b3413a6c6bd to your computer and use it in GitHub Desktop.
Install Oracle JDK on CentOS

Install Oracle Java 8 JDK

Note: If you would like to install a different release of Oracle Java 8 JDK, go to the Oracle Java 8 JDK Downloads Page, accept the license agreement, and copy the download link of the appropriate Linux .rpm package. Substitute the copied download link in place of the highlighted part of the wget command.

Change to your home directory and download the Oracle Java 8 JDK RPM with these commands:

cd ~
wget --no-cookies --no-check-certificate --header "Cookie: gpw_e24=http%3A%2F%2Fwww.oracle.com%2F; oraclelicense=accept-securebackup-cookie" "http://download.oracle.com/otn-pub/java/jdk/8u72-b15/jdk-8u72-linux-x64.rpm"

Then install the RPM with this yum command (if you downloaded a different release, substitute the filename here):

sudo yum install jdk-8u72-linux-x64.rpm

Now Java should be installed at /usr/java/jdk1.8.0_72/jre/bin/java, and linked from /usr/bin/java.

You may delete the archive file that you downloaded earlier:

rm ~/jdk-8u72-linux-x64.rpm

Set Default Java

If you installed multiple versions of Java, you may want to set one as your default (i.e. the one that will run when a user runs the java command). Additionally, some applications require certain environment variables to be set to locate which installation of Java to use. This section will show you how to do this.

By the way, to check the version of your default Java, run this command:

java -version

Using Alternatives

The alternatives command, which manages default commands through symbolic links, can be used to select the default Java command.

To print the programs that provide the java command that are managed by alternatives, use this command:

sudo alternatives --config java

Here is an example of the output:

There are 5 programs which provide 'java'.

  Selection    Command
-----------------------------------------------
*+ 1           /usr/java/jdk1.8.0_72/jre/bin/java
   2           /usr/java/jdk1.7.0_79/jre/bin/java

Enter to keep the current selection[+], or type selection number: Simply enter the a selection number to choose which java executable should be used by default.

Using Environment Variables

Many Java applications use the JAVA_HOME or JRE_HOME environment variables to determine which java executable to use.

For example, if you installed Java to /usr/java/jdk1.8.0_72/jre/bin (i.e. java executable is located at /usr/java/jdk1.8.0_72/jre/bin/java), you could set your JAVA_HOME environment variable in a bash shell or script like so:

export JAVA_HOME=/usr/java/jdk1.8.0_72

If you want JAVA_HOME to be set for every user on the system by default, add the previous line to the /etc/environment file. An easy way to append it to the file is to run this command:

sudo sh -c "echo export JAVA_HOME=/usr/java/jdk1.8.0_72 >> /etc/environment"

References

source https://www.digitalocean.com/community/tutorials/how-to-install-java-on-centos-and-fedora
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment