There are JDK versions available from the base CentOS repositories. Depending on your version of CentOS, and the JDK you want to install, the following as root should give you what you want:
OpenJDK Runtime Environment (Java SE 6)
yum install java-1.6.0-openjdk
OpenJDK Runtime Environment (Java SE 7)
yum install java-1.7.0-openjdk
OpenJDK Development Environment (Java SE 7)
yum install java-1.7.0-openjdk-devel
OpenJDK Development Environment (Java SE 6)
yum install java-1.6.0-openjdk-devel
Update for Java 8
In CentOS 6.6 or later, Java 8 is available. Similar to 6 and 7 above, the packages are as follows:
OpenJDK Runtime Environment (Java SE 8)
yum install java-1.8.0-openjdk
OpenJDK Development Environment (Java SE 8)
yum install java-1.8.0-openjdk-devel
There's also a 'headless' JRE package that is the same as the above JRE, except it doesn't contain audio/video support. This can be used for a slightly more minimal installation:
OpenJDK Runtime Environment - Headless (Java SE 8)
yum install java-1.8.0-openjdk-headless
Answer from grdryn on Stack Overflowjava - How to install Jdk in centos - Stack Overflow
rpm - OpenJDK 17 for CentOS 7 - Unix & Linux Stack Exchange
java - How to install jdk7 on CentOS? - Stack Overflow
Installing Java to Another Location on Centos 7
Videos
There are JDK versions available from the base CentOS repositories. Depending on your version of CentOS, and the JDK you want to install, the following as root should give you what you want:
OpenJDK Runtime Environment (Java SE 6)
yum install java-1.6.0-openjdk
OpenJDK Runtime Environment (Java SE 7)
yum install java-1.7.0-openjdk
OpenJDK Development Environment (Java SE 7)
yum install java-1.7.0-openjdk-devel
OpenJDK Development Environment (Java SE 6)
yum install java-1.6.0-openjdk-devel
Update for Java 8
In CentOS 6.6 or later, Java 8 is available. Similar to 6 and 7 above, the packages are as follows:
OpenJDK Runtime Environment (Java SE 8)
yum install java-1.8.0-openjdk
OpenJDK Development Environment (Java SE 8)
yum install java-1.8.0-openjdk-devel
There's also a 'headless' JRE package that is the same as the above JRE, except it doesn't contain audio/video support. This can be used for a slightly more minimal installation:
OpenJDK Runtime Environment - Headless (Java SE 8)
yum install java-1.8.0-openjdk-headless
I advise you to use the same JDK as you may use with Windows: the Oracle one.
http://www.oracle.com/technetwork/java/javase/downloads/index.html
Go to the Java SE 7u67 section and click on JDK7 Download button on the right.
On the new page select the option "(ยค) Accept License Agreement"
Then click on jdk-7u67-linux-x64.rpm
On your CentOS, as root, run:
$ rpm -Uvh jdk-7u67-linux-x64.rpm
$ alternatives --install /usr/bin/java java /usr/java/latest/bin/java 2
You may already have a Java 5 installed on your box... before installing the downloaded rpm remove previous Java by running this command yum remove java
You might simply build this yourself. If it worked before, chances are the build for Fedora works. You will need a Fedora 34+ host to cross-build this.
# Don't build as root. Only need root privileges to install `fedpkg`,
sudo dnf install -y fedpkg
# Now get the package description
git clone https://src.fedoraproject.org/rpms/java-17-openjdk
# Use fedpkg to kick off a mock build
cd java-17-openjdk
fedpkg mockbuild --root centos-7-x86_64
This will take a whileยน; and in the end, you should be getting a line that tells you where the RPMs you've just built were put.
ยน mock sets up a chroot, in which it installs a CentOS 7 base system, then EPEL, which you'll almost certainly will need, I think, then the tools necessary to build any RPM, then the build-time dependencies of the java-17-openjdk package. Then it will download all the necessary source code, hand off to rpm-build to do the build, which includes applying all patches, then compiling OpenJDK, then, running the OpenJDK installation, collecting the installed files, compressing them into valid RPMs.
Use Temurin. You'll need the Adoptium yum repo configuration from https://adoptium.net/installation/linux/
You should only be editing your PATH if you have to install a personal copy of Java. If you are going that route then you need to make sure that the bin directory of your preferred version of Java is at the beginning of your PATH, like so:
export JAVA_PATH=$JAVA_HOME
export PATH=$JAVA_HOME/bin:$PATH
export PATH=$JRE_HOME/bin:$PATH
But the better way to handle this is to install Java and use the alternatives command. If you can get by with OpenJDK you could just try:
yum install java-1.7.0-openjdk
yum install java-1.7.0-openjdk-devel
As OpenJDK is installed it will run the alternatives command to correctly configure everything. If you want to use the Oracle JDK then you can use either the RPM or the tar.gz, the RPM installed in /usr/java while the tar.gz should probably be extracted to that directory or another non-user specific directory like /opt/java or something. Personally I'd recommend the RPMs so install would be something like:
yum localinstall jdk-8u5-linux-x64.rpm
yum localinstall jdk-8u5-linux-x64-demos.rpm
After install comes the alternatives command however most people and docs get lazy (look at the CentOS wiki page that the other answer points to for an example of what I mean by lazy). You really should use the alternatives command to setup not only java and javac but also all the other commands that come with Java AND the man pages for those commands. You should run like:
jdk=/usr/java/jdk1.8.0_05
jre=$jdk/jre
alternatives \
--install /usr/bin/java java $jre/bin/java 180005 \
--slave /usr/lib/jvm/jre jre $jre \
--slave /usr/bin/keytool keytool $jre/bin/keytool \
--slave /usr/bin/orbd orbd $jre/bin/orbd \
--slave /usr/bin/pack200 pack200 $jre/bin/pack200 \
--slave /usr/bin/rmid rmid $jre/bin/rmid \
--slave /usr/bin/rmiregistry rmiregistry $jre/bin/rmiregistry \
--slave /usr/bin/servertool servertool $jre/bin/servertool \
--slave /usr/bin/tnameserv tnameserv $jre/bin/tnameserv \
--slave /usr/bin/unpack200 unpack200 $jre/bin/unpack200 \
--slave /usr/bin/javaws javaws $jre/bin/javaws \
--slave /usr/bin/jjs jjs $jre/bin/jjs \
--slave /usr/share/man/man1/java.1 java.1 \
$jdk/man/man1/java.1 \
--slave /usr/share/man/man1/keytool.1 keytool.1 \
$jdk/man/man1/keytool.1 \
--slave /usr/share/man/man1/orbd.1 orbd.1 \
$jdk/man/man1/ordb.1 \
--slave /usr/share/man/man1/pack200.1 pack200.1 \
$jdk/man/man1/pack200.1 \
--slave /usr/share/man/man1/rmid.1 rmid.1 \
$jdk/man/man1/rmid.1 \
--slave /usr/share/man/man1/rmiregistry.1 rmiregistry.1 \
$jdk/man/man1/rmiregistry.1 \
--slave /usr/share/man/man1/servertool.1 servertool.1 \
$jdk/man/man1/servertool.1 \
--slave /usr/share/man/man1/tnameserv.1 tnameserv.1 \
$jdk/man/man1/tnameserv.1 \
--slave /usr/share/man/man1/unpack200.1 unpack200.1 \
$jdk/man/man1/unpack200.1 \
--slave /usr/share/man/man1/javaws.1 javaws.1 \
$jdk/man/man1/javaws.1 \
--slave /usr/share/man/man1/jjs.1 jjs.1 \
$jdk/man/man1/jjs.1
alternatives \
--install /usr/lib/jvm/jre-1.8.0 \
jre_1.8.0 $jre 180005 \
alternatives \
--install /usr/bin/javac javac $jdk/bin/javac 180005 \
--slave /usr/lib/jvm/java java_sdk $jdk \
--slave /usr/bin/appletviewer appletviewer $jdk/bin/appletviewer \
--slave /usr/bin/extcheck extcheck $jdk/bin/extcheck \
--slave /usr/bin/idlj idlj $jdk/bin/idlj \
--slave /usr/bin/jar jar $jdk/bin/jar \
--slave /usr/bin/jarsigner jarsigner $jdk/bin/jarsigner \
--slave /usr/bin/javadoc javadoc $jdk/bin/javadoc \
--slave /usr/bin/javafxpackager javafxpackager $jdk/bin/javafxpackager \
--slave /usr/bin/javah javah $jdk/bin/javah \
--slave /usr/bin/javap javap $jdk/bin/javap \
--slave /usr/bin/jcmd jcmd $jdk/bin/jcmd \
--slave /usr/bin/jconsole jconsole $jdk/bin/jconsole \
--slave /usr/bin/jdb jdb $jdk/bin/jdb \
--slave /usr/bin/jdeps jdeps $jdk/bin/jdeps \
--slave /usr/bin/jhat jhat $jdk/bin/jhat \
--slave /usr/bin/jinfo jinfo $jdk/bin/jinfo \
--slave /usr/bin/jmap jmap $jdk/bin/jmap \
--slave /usr/bin/jmc jmc $jdk/bin/jmc \
--slave /usr/bin/jps jps $jdk/bin/jps \
--slave /usr/bin/jrunscript jrunscript $jdk/bin/jrunscript \
--slave /usr/bin/jsadebugd jsadebugd $jdk/bin/jsadebugd \
--slave /usr/bin/jstack jstack $jdk/bin/jstack \
--slave /usr/bin/jstat jstat $jdk/bin/jstat \
--slave /usr/bin/jstatd jstatd $jdk/bin/jstatd \
--slave /usr/bin/jvisualvm jvisualvm $jdk/bin/jvisualvm \
--slave /usr/bin/native2ascii native2ascii $jdk/bin/native2ascii \
--slave /usr/bin/policytool policytool $jdk/bin/policytool \
--slave /usr/bin/rmic rmic $jdk/bin/rmic \
--slave /usr/bin/schemagen schemagen $jdk/bin/schemagen \
--slave /usr/bin/serialver serialver $jdk/bin/serialver \
--slave /usr/bin/wsgen wsgen $jdk/bin/wsgen \
--slave /usr/bin/wsimport wsimport $jdk/bin/wsimport \
--slave /usr/bin/xjc xjc $jdk/bin/xjc \
--slave /usr/share/man/man1/appletviewer.1 appletviewer.1 \
$jdk/man/man1/appletviewer.1 \
--slave /usr/share/man/man1/extcheck.1 extcheck.1 \
$jdk/man/man1/extcheck.1 \
--slave /usr/share/man/man1/idlj.1 idlj.1 \
$jdk/man/man1/idlj.1 \
--slave /usr/share/man/man1/jar.1 jar.1 \
$jdk/man/man1/jar.1 \
--slave /usr/share/man/man1/jarsigner.1 jarsigner.1 \
$jdk/man/man1/jarsigner.1 \
--slave /usr/share/man/man1/javac.1 javac.1 \
$jdk/man/man1/javac.1 \
--slave /usr/share/man/man1/javadoc.1 javadoc.1 \
$jdk/man/man1/javadoc.1 \
--slave /usr/share/man/man1/javafxpackager.1 javafxpackager.1 \
$jdk/man/man1/javafxpackager.1 \
--slave /usr/share/man/man1/javah.1 javah.1 \
$jdk/man/man1/javah.1 \
--slave /usr/share/man/man1/javap.1 javap.1 \
$jdk/man/man1/javap.1 \
--slave /usr/share/man/man1/jcmd.1 jcmd.1 \
$jdk/man/man1/jcmd.1 \
--slave /usr/share/man/man1/jconsole.1 jconsole.1 \
$jdk/man/man1/jconsole.1 \
--slave /usr/share/man/man1/jdb.1 jdb.1 \
$jdk/man/man1/jdb.1 \
--slave /usr/share/man/man1/jdeps.1 jdeps.1 \
$jdk/man/man1/jdeps.1 \
--slave /usr/share/man/man1/jhat.1 jhat.1 \
$jdk/man/man1/jhat.1 \
--slave /usr/share/man/man1/jinfo.1 jinfo.1 \
$jdk/man/man1/jinfo.1 \
--slave /usr/share/man/man1/jmap.1 jmap.1 \
$jdk/man/man1/jmap.1 \
--slave /usr/share/man/man1/jmc.1 jmc.1 \
$jdk/man/man1/jmc.1 \
--slave /usr/share/man/man1/jps.1 jps.1 \
$jdk/man/man1/jps.1 \
--slave /usr/share/man/man1/jrunscript.1 jrunscript.1 \
$jdk/man/man1/jrunscript.1 \
--slave /usr/share/man/man1/jsadebugd.1 jsadebugd.1 \
$jdk/man/man1/jsadebugd.1 \
--slave /usr/share/man/man1/jstack.1 jstack.1 \
$jdk/man/man1/jstack.1 \
--slave /usr/share/man/man1/jstat.1 jstat.1 \
$jdk/man/man1/jstat.1 \
--slave /usr/share/man/man1/jstatd.1 jstatd.1 \
$jdk/man/man1/jstatd.1 \
--slave /usr/share/man/man1/jvisualvm.1 jvisualvm.1 \
$jdk/man/man1/jvisualvm.1 \
--slave /usr/share/man/man1/native2ascii.1 native2ascii.1 \
$jdk/man/man1/native2ascii.1 \
--slave /usr/share/man/man1/policytool.1 policytool.1 \
$jdk/man/man1/policytool.1 \
--slave /usr/share/man/man1/rmic.1 rmic.1 \
$jdk/man/man1/rmic.1 \
--slave /usr/share/man/man1/schemagen.1 schemagen.1 \
$jdk/man/man1/schemagen.1 \
--slave /usr/share/man/man1/serialver.1 serialver.1 \
$jdk/man/man1/serialver.1 \
--slave /usr/share/man/man1/wsgen.1 wsgen.1 \
$jdk/man/man1/wsgen.1 \
--slave /usr/share/man/man1/wsimport.1 wsimport.1 \
$jdk/man/man1/wsimport.1 \
--slave /usr/share/man/man1/xjc.1 xjc.1 \
$jdk/man/man1/xjc.1
alternatives \
--install /usr/lib/jvm/java-1.8.0 \
java_sdk_1.8.0 $jdk 180005 \
That is my example for JDK 8 but if you swap out the jdk= line for your path that will come close but there are a couple of new commands that come with 8 and one that was dropped from 8 so some slight adjustments would be needed if you really wanted to install 7. Also note the 1800005 number. With the alternatives command the higher number is the default selection and OpenJDK 7u55 uses 170055. If you pick a number lower than the number used by whatever OpenJDK you have installed you'll need to run alternatives --config java and alternatives --config javac.
After running the alternatives commands you want to update you man page keyword database by running makewhatis.
Then you might want to install the Java plugin for your Firefox:
ln -s /usr/java/jdk1.8.0_05/jre/lib/amd64/libnpjp2.so /usr/lib64/mozilla/plugins/
Then you'd want to add the Java Control Panel to your System->Preferences menu and setup JARs to be double-clicked on:
/bin/cp -fr /usr/java/jdk1.8.0_05/jre/lib/desktop/* /usr/share/
/bin/cp -fr /usr/java/jdk1.8.0_05/jre/lib/desktop/icons/hicolor/* /usr/share/icons/System
/usr/bin/gtk-update-icon-cache -f /usr/share/icons/System
/usr/bin/update-desktop-database
If it wasn't clear, you'd do all this as root. If you don't have root then just use the tar.gz version and fix your PATH. I wrote a little bit more detail at http://blog.osdev.org/java/2014/04/22/jdk-alternatives.html
You probably has link in /usr/bin/java to your another java installation directory.
Check there about alternatives in CentOS
Hello,
I installed java before and i am running java applications on Centos, it is located at /usr/bin/java. For some other reason i want to install it to another location, let's say /myDirectory/java I downloaded java jdk as tar file and extract it to /myDirectory this directory then i set JAVA_HOME accordingly.
The problem is i can't start the applications anymore, the log says :
ERROR: JAVA_HOME is set to an invalid directory: /myDirectory/jdk1.8.0_181/bin/java
Please set the JAVA_HOME variable in your environment to match the location of your Java installation.
Is it because i had a prior installation? How do i achive this? Any help would be aprreciated, thanks.
yum search jdk
Results:
java-1.8.0-openjdk.x86_64 : OpenJDK Runtime Environment
java-1.8.0-openjdk-devel.x86_64 : OpenJDK Development Environment
So you could likely run:
yum install java-1.8.0-openjdk.x86_64 java-1.8.0-openjdk-devel.x86_64
If that doesn't work, try a yum clean all and repeat.
Edit: You could also search the FTP repo link you posted...

i figured
bash$ grep openjdk /etc/yum.conf
exclude=*.i?86 *openjdk* java*
and can't believe spent a long time at everything else. works now after removing the exclusion. very naive!