Comment installer Java sur CentOS et Fedora

Introduction

Java est une plate-forme de logiciel populaire qui vous permet d’exécuter des applets et applications Java. Ce tutoriel va vous montrer comment installer Java sur CentOS, Fedora, RHEL et.

L’installation des versions suivantes de Java sont couverts :

OpenJDK 7
OpenJDK 6
Java Oracle 8
Oracle Java 7

N’hésitez pas à passer à votre section désirée en utilisant le bouton contenu dans la barre latérale !
Conditions préalables

Avant de commencer ce guide, vous devriez avoir un utilisateur régulier, non root avec sudo privilèges configurés sur les deux serveurs–, il s’agit de l’utilisateur que vous devez vous connecter à vos serveurs comme. Vous pouvez apprendre comment configurer un compte d’utilisateur normal en suivant étapes 1 à 4 dans notre guide de configuration initiale du serveur Ubuntu 14.04.
Variations de Java

Il existe trois éditions différentes de la plate-forme Java : Standard Edition (SE), Enterprise Edition (EE) et Micro Edition (ME). Ce tutoriel se concentre sur Java SE (Java Platform, Standard Edition).

Il y a deux ensembles de Java SE qui peuvent être installées : l’environnement d’exécution Java (JRE) et le Java Development Kit (JDK). JRE est une implémentation de la Machine virtuelle Java (JVM), qui vous permet d’exécuter des applications Java compilées et les applets. JDK comprend JRE et autres logiciels requis pour l’écriture, développer et compiler des applets et applications Java.

Il y a aussi deux implémentations différentes de Java : OpenJDK et Oracle Java. Les deux implémentations sont basées essentiellement sur le même code mais OpenJDK, l’implémentation de référence de Java, est entièrement open source, alors qu’Oracle Java contient du code propriétaire. La plupart des applications Java fonctionne parfaitement avec soit, mais vous devez utiliser quelle que soit la mise en œuvre votre logiciel réclame.

Vous pouvez installer différentes versions et versions de Java sur un système unique, mais la plupart des gens ne doivent une seule installation. Dans cet esprit, essayez d’installer uniquement la version de Java dont vous avez besoin pour lancer ou développer vos applications.
OpenJDK 7

Cette section vous montrera comment installer les préconstruits OpenJDK 7 JRE et packages JDK en utilisant le gestionnaire de paquets yum, qui est similaire à apt-get pour Ubuntu/Debian. OpenJDK 7 est la dernière version de OpenJDK.
Installer OpenJDK 7 JRE

Pour installer OpenJDK 7 JRE à l’aide d’yum, exécutez cette commande :

To install OpenJDK 7 JRE using yum, run this command:

sudo yum install java-1.7.0-openjdk

At the confirmation prompt, enter y then RETURN to continue with the installation.

Congratulations! You have installed OpenJDK 7 JRE.
Install OpenJDK 7 JDK

To install OpenJDK 7 JDK using yum, run this command:

sudo yum install java-1.7.0-openjdk-devel

At the confirmation prompt, enter y then RETURN to continue with the installation.

Congratulations! You have installed OpenJDK 7 JDK.
OpenJDK 6

This section will show you how to install the prebuilt OpenJDK 6 JRE and JDK packages using the yum package manager.
Install OpenJDK 6

To install OpenJDK 6 JRE using yum, run this command:

sudo yum install java-1.6.0-openjdk

At the confirmation prompt, enter y then RETURN to continue with the installation.

Congratulations! You have installed OpenJDK 6 JRE.
Install OpenJDK 6 JDK

To install OpenJDK 6 JDK using yum, run this command:

sudo yum install java-1.6.0-openjdk-devel

At the confirmation prompt, enter y then RETURN to continue with the installation.

Congratulations! You have installed OpenJDK 6 JDK.
Install Oracle Java 8

This section of the guide will show you how to install Oracle Java 8 update 25 JRE and JDK (64-bit), the latest release of these packages at the time of this writing.

Note: You must accept the Oracle Binary Code License Agreement for Java SE, which is one of the included steps, before installing Oracle Java.
Install Oracle Java 8 JRE

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

Change to the /opt directory and download the Oracle Java 8 JRE .tar.gz archive with these command:

cd /opt
sudo 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/8u25-b17/jre-8u25-linux-x64.tar.gz”

Extract the archive that you just downloaded with this command (if you downloaded a different release, substitute the filename here):

sudo tar xvf jre-8u25-linux-x64.tar.gz

This will produce a directory based on the release that you downloaded, e.g. jre1.8.0_25. Change the ownership of the extracted files to root:

sudo chown -R root: jre1.8.0_25

The JRE executable files, e.g. java among others, are now installed at /opt/jre1.8.0_25/bin which is not in your PATH variable, so the commands can only be used if you reference their locations. To remedy this, you can either add this directory to your PATH variable or use the alternatives command to add symbolic links to individual executable files to the /usr/bin directory. We will show you how to use the alternatives command to manage your Java executables.

Use this alternatives command to add a symbolic link, in the /usr/bin directory, to the java command:

sudo alternatives –install /usr/bin/java java /opt/jre1.8.0_25/bin/java 1

Feel free to use the alternatives command in a similar fashion to manage any of the other Java executable files.

You may delete the archive file that you downloaded earlier:

sudo rm /opt/jre-8u25-linux-x64.tar.gz

Congratulations! You have installed Oracle Java 8 JRE.
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 .tar.gz package. Substitute the copied download link in place of the highlighted part of the wget command.

Change to the /opt directory and download the Oracle Java 8 JDK .tar.gz archive with these command:

cd /opt
sudo 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/8u25-b17/jdk-8u25-linux-x64.tar.gz”

Extract the archive that you just downloaded with this command (if you downloaded a different release, substitute the filename here):

sudo tar xvf jdk-8u25-linux-x64.tar.gz

This will produce a directory based on the release that you downloaded, e.g. jdk1.8.0_25. Change the ownership of the extracted files to root:

sudo chown -R root: jdk1.8.0_25

The JDK executable files, e.g. java, javac, and jar, are now installed at /opt/jdk1.8.0_25/bin which is not in your PATH variable, so the commands can only be used if you reference their locations. To remedy this, you can either add this directory to your PATH variable or use the alternatives command to add symbolic links to individual executable files to the /usr/bin directory. We will show you how to use the alternatives command to manage your Java executables.

Use these alternatives commands to add symbolic links, in the /usr/bin directory, to the java, javac, and jar commands:

sudo alternatives –install /usr/bin/java java /opt/jdk1.8.0_25/bin/java 1
sudo alternatives –install /usr/bin/javac javac /opt/jdk1.8.0_25/bin/javac 1
sudo alternatives –install /usr/bin/jar jar /opt/jdk1.8.0_25/bin/jar 1

Feel free to use the alternatives command in a similar fashion to manage any of the other Java executable files.

You may delete the archive file that you downloaded earlier:

sudo rm /opt/jdk-8u25-linux-x64.tar.gz

Congratulations! You have installed Oracle Java 8 JDK.
Install Oracle Java 7

This section of the guide will show you how to install Oracle Java 7 update 71 JRE and JDK (64-bit).

Note: You must accept the Oracle Binary Code License Agreement for Java SE, which is one of the included steps, before installing Oracle Java.
Install Oracle Java 7 JRE

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

Change to the /opt directory and download the Oracle Java 7 JRE .tar.gz archive with these command:

cd /opt
sudo 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/7u71-b14/jre-7u71-linux-x64.tar.gz”

Extract the archive that you just downloaded with this command (if you downloaded a different release, substitute the filename here):

sudo tar xvf jre-7u71-linux-x64.tar.gz

This will produce a directory based on the release that you downloaded, e.g. jre1.7.0_71. Change the ownership of the extracted files to root:

sudo chown -R root: jre1.7.0_71

The JDK executable files, e.g. java, javac, and jar, are now installed at /opt/jre1.7.0_71/bin which is not in your PATH variable, so the commands can only be used if you reference their locations. To remedy this, you can either add this directory to your PATH variable or use the alternatives command to add symbolic links to individual executable files to the /usr/bin directory. We will show you how to use the alternatives command to manage your Java executables.

Use this alternatives command to add a symbolic link, in the /usr/bin directory, to the java command:

sudo alternatives –install /usr/bin/java java /opt/jre1.7.0_71/bin/java 1

Feel free to use the alternatives command in a similar fashion to manage any of the other Java executable files.

You may delete the archive file that you downloaded earlier:

sudo rm /opt/jre-7u71-linux-x64.tar.gz

Congratulations! You have installed Oracle Java 7 JRE.
Install Oracle Java 7 JDK

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

Change to the /opt directory and download the Oracle Java 7 JDK .tar.gz archive with these command:

cd /opt
sudo 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/7u71-b14/jdk-7u71-linux-x64.tar.gz”

Extract the archive that you just downloaded with this command (if you downloaded a different release, substitute the filename here):

sudo tar xvf jdk-7u71-linux-x64.tar.gz

This will produce a directory based on the release that you downloaded, e.g. jdk1.7.0_71. Change the ownership of the extracted files to root:

sudo chown -R root: jdk1.7.0_71

The JDK executable files, e.g. java, javac, and jar, are now installed at /opt/jdk1.7.0_71/bin which is not in your PATH variable, so the commands can only be used if you reference their locations. To remedy this, you can either add this directory to your PATH variable or use the alternatives command to add symbolic links to individual executable files to the /usr/bin directory. We will show you how to use the alternatives command to manage your Java executables.

Use these alternatives commands to add symbolic links, in the /usr/bin directory, to the java, javac, and jar commands:

sudo alternatives –install /usr/bin/java java /opt/jdk1.7.0_71/bin/java 1
sudo alternatives –install /usr/bin/javac javac /opt/jdk1.7.0_71/bin/javac 1
sudo alternatives –install /usr/bin/jar jar /opt/jdk1.7.0_71/bin/jar 1

Feel free to use the alternatives command in a similar fashion to manage any of the other Java executable files.

You may delete the archive file that you downloaded earlier:

sudo rm /opt/jdk-7u71-linux-x64.tar.gz

Congratulations! You have installed Oracle Java 7 JDK.
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/lib/jvm/java-1.7.0-openjdk-1.7.0.71-2.5.3.1.el7_0.x86_64/jre/bin/java
2 /opt/jdk1.8.0_25/bin/java
3 /opt/jre1.8.0_25/bin/java
+ 4 /opt/jre1.7.0_71/bin/java
5 /opt/jdk1.7.0_71/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 /opt/jdk1.8.0_25 (i.e. java executable is located at /opt/jdk1.8.0_25/bin/java), you could set your JAVA_HOME environment variable in a bash shell or script like so:

export JAVA_HOME=/opt/jdk1.8.0_25

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=/opt/jdk1.8.0_25 >> /etc/environment”

Leave a Reply

Your email address will not be published.