100

I have CentOS 5, but I don't know the steps to install Java SDK on Linux.

Where to download the RPM file and what can I do next to fully install that?

Then I need to install Tomcat.

Or is there any ready-made package for all?

nhahtdh
  • 52,949
  • 15
  • 113
  • 149
  • I suspect we need to start being clear when we mean Oracle Java, or can accept one of the open source alternatives. Sventek has a good answer - but it won't help if you have tools with maintainers that refuse to fix issues until reproduced on the Oracle Java/JDK tools. – Danny Staple Dec 19 '13 at 10:29

12 Answers12

165

The following command will return a list of all packages directly related to Java. They will be in the format of java-<version>.

$ yum search java | grep 'java-'

If there are no available packages, then you may need to download a new repository to search through. I suggest taking a look at Dag Wieers' repo. After downloading it, try the above command again.

You will see at least one version of Java packages available for download. Depending on when you read this, the lastest available version may be different.

java-1.7.0-openjdk.x86_64

The above package alone will only install JRE. To also install javac and JDK, the following command will do the trick:

$ yum install java-1.7.0-openjdk*

These packages will be installing (as well as their dependencies):

java-1.7.0-openjdk.x86_64
java-1.7.0-openjdk-accessibility.x86_64
java-1.7.0-openjdk-demo.x86_64
java-1.7.0-openjdk-devel.x86_64
java-1.7.0-openjdk-headless.x86_64
java-1.7.0-openjdk-javadoc.noarch
java-1.7.0-openjdk-src.x86_64
onebree
  • 1,785
  • 1
  • 15
  • 42
Sventek
  • 2,231
  • 2
  • 12
  • 8
34

@Sventeck, perfecto.

redhat docs are always a great source - good tutorial that explains how to install JDK via yum and then setting the path can be found here (have fun!) - Install OpenJDK and set $JAVA_HOME path

OpenJDK 6:

yum install java-1.6.0-openjdk-devel

OpenJDK 7:

yum install java-1.7.0-openjdk-devel

To list all available java openjdk-devel packages try:

yum list "java-*-openjdk-devel"
Elouan Keryell-Even
  • 828
  • 1
  • 11
  • 33
xpros
  • 1,657
  • 15
  • 15
  • 1
    Tested on CentOS 6.4, I confirm it is the "centos" way. The JAVA_HOME environment variable should be set on "/etc/alternatives/jre". – Emmanuel Keller Oct 01 '13 at 11:20
  • Gotta love me some centos! This variable can also be stored in /etc/environment which is where most system wide global variables should exist. `echo JAVA_HOME="path/to/JAVA_HOME" >> /etc/environment` – xpros Nov 01 '13 at 14:58
16

yum install java-1.8.0

and then:

alternatives --config java

and check:

java -version
Joe K
  • 17,254
  • 1
  • 31
  • 54
user831217
  • 694
  • 7
  • 17
13

On centos 7, I just do

sudo yum install java-sdk

I assume you have most common repo already. Centos just finds the correct SDK with the -devel sufix.

fangmobile
  • 799
  • 7
  • 22
10

An alternative answer is,

sudo yum list \*java-1\* | grep open 

than select one from list and install that

for example,

sudo yum install java-1.7.0-openjdk.x86_64
iceberg
  • 1,831
  • 1
  • 18
  • 24
6

Here is a detailed information on setting up Java and its paths on CentOS6.

Below steps are for the installation of latest Java version 8:

  1. Download java rpm package from Oracle site. (jdk-8-linux-x64.rpm)
  2. Install from the rpm. (rpm -Uvh jdk-8-linux-x64.rpm)
  3. Open /etc/profile, and set the java paths, save it.
  4. Check the java installation path, and java version, with the commands: which java, java -version

Now you can test the installation with a sample java program

shasi kanth
  • 6,503
  • 22
  • 101
  • 151
3

Since Oracle inserted some md5hash in their download links, one cannot automatically assemble a download link for command line.

So I tinkered some nasty bash command line to get the latest jdk download link, download it and directly install via rpm. For all who are interested:

wget -q http://www.oracle.com/technetwork/java/javase/downloads/index.html -O ./index.html && grep -Eoi ']+>' index.html | grep -Eoi '/technetwork/java/javase/downloads/jdk8-downloads-[0-9]+.html' | (head -n 1) | awk '{print "http://www.oracle.com"$1}' | xargs wget --no-cookies --header "Cookie: gpw_e24=xxx; oraclelicense=accept-securebackup-cookie;" -O index.html -q && grep -Eoi '"filepath":"[^"]+jdk-8u[0-9]+-linux-x64.rpm"' index.html | grep -Eoi 'http:[^"]+' | xargs wget --no-cookies --header "Cookie: gpw_e24=xxx; oraclelicense=accept-securebackup-cookie;" -q -O ./jdk8.rpm && sudo rpm -i ./jdk8.rpm

The bold part should be replaced by the package of your liking.

Benjen
  • 134
  • 1
  • 8
  • 1
    Righteous hack. – Aaron Altman Oct 04 '17 at 20:56
  • I've used a similar approach in a bash script to get the latest JRE and JDK download links for version 8 and 9. It can be found in [this thread as answer](https://stackoverflow.com/a/49780226/6771046). – U880D Apr 12 '18 at 05:16
2

If you want the Oracle JDK and are willing not to use yum/rpm, see this answer here:

Downloading Java JDK on Linux via wget is shown license page instead

As per that post, you can automate the download of the tarball using curl and specifying a cookie header.

Then you can put the tarball contents in the right place and add java to your PATH, for example:

curl -v -j -k -L -H "Cookie: oraclelicense=accept-securebackup-cookie" http://download.oracle.com/otn-pub/java/jdk/8u45-b14/jdk-8u45-linux-x64.tar.gz > jdk.tar.gz

tar xzvf jdk.tar.gz
sudo mkdir /usr/local/java
sudo mv jdk1.8.0_45 /usr/local/java/
sudo ln -s /usr/local/java/jdk1.8.0_45 /usr/local/java/jdk

sudo vi /etc/profile.d/java.sh
export PATH="$PATH:/usr/local/java/jdk/bin"
export JAVA_HOME=/usr/local/java/jdk

source /etc/profile.d/java.sh
Community
  • 1
  • 1
Anthony Hayward
  • 1,606
  • 18
  • 15
0

use the below commands to install oracle java8 through terminal

Step -1) Visit Oracle JDK download page, look for RPM version

Step -2) Download oracle java 8 using the below command wget --header "Cookie: oraclelicense=accept-securebackup-cookie" http://download.oracle.com/otn-pub/java/jdk/8u121-b13/e9e7ea248e2c4826b92b3f075a80e441/jdk-8u121-linux-x64.rpm

Step -3) Install the java8 using below command sudo yum localinstall jdk-8u121-linux-x64.rpm Now the JDK should be installed at /usr/java/jdk1.8.0_121 Step -4) Remove the downloaded .rpm file to utilize the space. rm jdk-8u121-linux-x64.rpm

Step -5) Verify the java by using command java -version

Step -6) If the CentOS has multiple JDK installed, you can use the alternatives command to set the default java sudo alternatives --config java

Step -7)Optional set JAVA_HOME Environment variables. copy the path of jdk install i.e /usr/java/jdk1.8.0_121 use below command to export java home export JAVA_HOME=/usr/java/jdk1.8.0_121 export PATH=$PATH:$JAVA_HOME

Dilip D
  • 524
  • 4
  • 23
0

I have written a shell script to install/uninstall java on centos. You can get it done by just run the shell. The core of this shell is :

1.download the jdk rpm(RedHat Package Manager) package.
2.install java using rpm.

You can see more detail here: https://github.com/daikaixian/WaterShell/tree/master/program_installer

Hope it works for you.

Dai Kaixian
  • 877
  • 2
  • 12
  • 21
0

enter image description here

This is what I did:

  1. First, I downloaded the .tar file for Java JDK and JRE from the Oracle site.

  2. Extract the .tar file into the opt folder.

  3. I faced an issue that despite setting my environment variables, JAVA_HOME and PATH for Java 9, it was still showing Java 8 as my runtime environment. Hence, I symlinked from the Java 9.0.4 directory to /user/bin using the ln command.

  4. I used java -version command to check which version of java is currently set as my default java runtime environment.

Bless
  • 4,071
  • 2
  • 35
  • 39
0

To install OpenJDK 8 JRE using yum with non root user, run this command:

sudo yum install java-1.8.0-openjdk

to verify java -version

Chris Catignani
  • 3,857
  • 6
  • 29
  • 40
muhammad shahan
  • 157
  • 1
  • 12