From Gentoo Wiki
Jump to: navigation, search
This page is a translated version of the page Java and the translation is 38% complete.

Other languages:
Deutsch • ‎English • ‎español • ‎français • ‎日本語 • ‎한국어 • ‎português do Brasil • ‎русский • ‎中文(中国大陆)‎

Java is a programming language, originally developed by Sun Microsystems, which uses a runtime to allow running Java-developed applications on various platforms. It is a popular choice for developers who want to create cross-platform applications.

Qu'est-ce que Java ?

Vue d'ensemble

Java est un langage de programmation qui a été développé par les ingénieurs de Sun Microsystems. C'est un langage orienté objet et conçu pour être exécuté sur des plateformes multiples sans avoir à recompiler le code pour chacune des plateformes. Bien que Java puisse être compilé comme un programme natif, la popularité de Java est essentiellement attribuable à sa portabilité, et à d'autres fonctionnalités comme la fonction éboueur. Pour rendre possible l'indépendance de la plateforme, le compilateur Java compile le code en une représentation intermédiaire appelée Java bytecode qui tourne sur un environnement d'exécution Java ou JRE et pas directement sur le système d'exploitation.

Afin d'exécuter le Java bytecode, il faut avoir un environnement d'exécution Java (Java Run Time Environment ou JRE) installé. Le JRE procure, parmi d'autres, les bibliothèques du cœur, une machine virtuelle Java dépendante de la plateforme et des modules d'extension pour les navigateurs. Un kit de développement Java (Java Development Kit ou JDK) ajoute des outils de programmation comme un compilateur de bytecote et un débogueur.

Installer une machine virtuelle

Les choix possibles

Gentoo procure de nombreux environnement d'exécution Java (JREs) et kits de développement Java (JDKs). Les choix courants comprennent :

Vendeur JDK JRE
IcedTea Open Java SE (anciennement icedtea6-bin) dev-java/icedtea et dev-java/icedtea-bin
Oracle Java dev-java/oracle-jdk-bin dev-java/oracle-jre-bin
GCJ IcedTea GNU GCC Java (actuellement IcedTea-7) dev-java/icedtea sys-devel/gcc USE="gcj"

Installer un JRE/JDK

Pour installer le "JDK" par défaut du profile, exécuter :

root #emerge --ask virtual/jdk

Pour installer le "JRE" par défaut du profile, exécuter :

root #emerge --ask virtual/jre

Quelques JDKs et JREs, y compris les paquets d'Oracle, nécessitent que l'utilisateur accepte des Conditions Générales d'Utilisation (CGU, en anglais End User License Agreement ou EULA). Si cette licence (telle que dlj-1.1) n'est pas listée dans la variable ACCEPT_LICENSE dans /etc/portage/make.conf, l'installation du JDK/JRE échouera. Pour savoir comment ajouter des licences acceptables à make.conf, reportez-vous au chapitre Licences du manuel Portage.

Pour éviter le désagrément d'une licence trop restrictive, installez plutôt icedtea-bin, qui est une mise en œuvre libre de Java du projet OpenJDK.

Soyez conscient qu'un JDK comprend aussi un JRE, c'est pourquoi il n'est pas nécessaire d'installer un JRE si un JDK a déjà été installé.

Installing IcedTea GCJ Virtual Machine

gcj is deprecated and is removed in GCC versions 7.0 and greater. It is unavailable from GCC 6.0 on Gentoo. Consider this section deprecated.

The IcedTea virtual machine is provided in source code and requires compilation by a Java bytecode compiler. This bootstrapping can be executed by the GNU Compiler for Java (GCJ). This compiler is available when GCC is built with the gcj USE flag.

When GCC is rebuilt with this USE flag set, the dev-java/icedtea package can be installed. Because of a Portage bug, users need to install dev-java/gcj-jdk and dev-java/ant-core explicitly first if they are not already present.

root #emerge --ask --oneshot dev-java/gcj-jdk dev-java/ant-core
root #emerge --ask dev-java/icedtea:7

Installer une machine virtuelle en limitant les recherches

Quelques JDKs et JREs vous obligent à passer par quelques étapes supplémentaires pour leur configuration. Installez (avec emerge) les paquets comme vous le feriez normalement. Si des étapes supplémentaires sont nécessaires, les ebuilds vous indiqueront où aller et quoi télécharger.

Download the indicated file(s) into /usr/portage/distfiles Once the files are in the right directories, rerun the emerge command, at which point the JRE/JDK will be begin to install.

Setting up a headless JRE

Sometimes there is no need for a full JRE with all the capabilities of java. Using java on a server often does not require any GUI, graphical, sound or even printer related features. To install a simplified (sometimes also referred to as headless) JRE, a few USE flags need to be changed for the selected JRE flavor.

FILE /etc/portage/package.useRequired USE flag changes
dev-java/icedtea headless-awt -alsa -cups
dev-java/icedtea-bin headless-awt -alsa -cups
dev-java/oracle-jre-bin headless-awt -alsa -cups

Depending on the current Gentoo profile, this might already be the case. As usual, the USE flag settings that are applicable to a particular package can be checked by running emerge in pretend mode:

user $emerge --pretend --verbose virtual/jre

Configurer la machine virtuelle Java

Vue d'ensemble

Gentoo a la capacité à avoir plusieurs JDKs et JREs installés sans que cela ne crée de conflit.

Using the java-config tool with root privileges, a system-wide default java virtual machine (VM) can be set. Users can also use java-config to custom set their personal VM on a user-by-user basis.

eselect can also be used to change the system and user VM. See eselect java-vm help.

Définir une machine virtuelle par défaut

Running the java-config command with the --list-available-vms option will output a list of all JREs and JDKs installed on the system. Here is an example of the output:

root #java-config --list-available-vms
The following VMs are available for generation-2:
1)      IcedTea JDK [icedtea-7]
*)      IcedTea JDK 3.0.1 [icedtea-8]
VMs marked as 'Build Only' may contain security vulnerabilities and/or be end of life (EOL). Gentoo recommends not setting these VMs as the system's or the user's VM. Please see Build Only VM for more information.

The * indicates this is the current active JVM (system-vm or user-vm when set). The name in the brackets ([]) is the handle or ID for that particular VM. The handle or the number to java-config --set-system-vm can be used to set the VM. The following text provides an example of how to set the system VM.

Définir la machine virtuelle système à l'aide de la poignée (solution préférée):

root #java-config --set-system-vm icedtea-7
Now using icedtea-7 as your generation-2 system JVM

Alternate method: select VM by number handle number:

root #java-config --set-system-vm 3
Now using icedtea-7 as your generation-2 system JVM

As a regular user, use java-config --set-user-vm.

source-ing the profile for existing shell sessions is not usually needed when changing the user or system VM. The only exception is that variables such as JAVA_HOME will still point to the old location after setting a user VM for the first time or revert back to the system VM.

Build only VM

Some virtual machines are flagged as build-only due to being EOL and/or containing security vulnerabilities. These virtual machines will not automatically be used by Gentoo for the running of applications using Gentoo launchers (run-java-tool script designed for switching VMs), but will still be available for use by Gentoo's build environment as some packages may require them for building. The setting of these virtual machines as either the system or user VM is strongly discouraged as these VMs will then be used when running the /usr/bin/{java,javac,..} executables, as well as used by any packages not using Gentoo's launcher scripts.

Configurer un CLASSPATH par défaut

The options explained in this section should be considered deprecated and will most likely be removed in the future. It is strongly recommended against using these, because Java projects or applications should ideally manage their own classpaths. When choosing to specify a default CLASSPATH, some applications may behave unexpectedly, because classes they were not expecting to be on the classpath.

java-config can also be used to set a system-wide default CLASSPATH, as well a user-specific default CLASSPATH.

First, list available Java libraries installed on the system to possibly put in the CLASSPATH variable. Here is an example of output:

root #java-config --list-available-packages
[xerces-2] The next generation of high performance, fully compliant XML parsers in the Apache Xerces family (/usr/share/xerces-2/package.env)
[junit] Simple framework to write repeatable tests (/usr/share/junit/package.env)
[bsh] BeanShell: A small embeddable Java source interpreter (/usr/share/bsh/package.env)
[bcel] The Byte Code Engineering Library: analyze, create, manipulate Java class files (/usr/share/bcel/package.env)
[log4j] A low-overhead robust logging package for Java (/usr/share/log4j/package.env)

Again, the names in brackets ([]) are the IDs to pass to java-config --set-system-classpath. Here is an example:

root #java-config --set-system-classpath log4j,xerces-2
The current directory (.) will not be part of the system classpath, as that should be added to the system's login profile.

Update the environment by logging out, then in again or by typing source /etc/profile.

For users, java-config --set-user-classpath will create ~/.gentoo/java-env-classpath, which should then source from the shell's profile.

CODE Sourcing user specific classpath
if [[ -f "${HOME}/.gentoo/java-env-classpath" ]]; then
       source ${HOME}/.gentoo/java-env-classpath

If desiring a system wide or user default classpath add something like the following to the shell's profile. This is advised against:

root #export CLASSPATH="${CLASSPATH}:$(java-config --classpath log4j,xerces-2)"

Java browser plugins

The Java browser plugin used to be managed via eselect java-nsplugin. This has now been removed in favor of a simpler out-of-the-box experience. For basic usage, simply emerge the chosen JVM with the nsplugin USE flag enabled. Note that Oracle's plugin is only available for amd64 and x86.

Also note that Chromium-based browsers no longer support NPAPI-based plugins since the code was removed in September 2015. The list of supporting browsers is shrinking but still includes Firefox as of version 46.

For more information, including JVM selection, Web Start, and multilib, see the README installed with icedtea-web.

Options de la variable USE à utiliser avec Java

Configurer les options de la variable USE

For more information regarding USE flags, refer to the USE flags chapter from the Gentoo Handbook.

USE flags

  • The java flag adds support for Java in a variety of programs;
  • The nsplugin flag adds support for Mozilla-like browsers (including Firefox). This is needed for viewing Java applets in a Mozilla-like browser;
  • The jce flag adds support for the Java Cryptography Engine;

Following USE flags go in JAVA_PKG_IUSE.

  • The source flag installs a zip of the source code of a package. This is traditionally used for IDEs to 'attach' source to the libraries that are being use;
  • For Java packages, the doc flag will build API documentation using javadoc.

See also

External resources

More information can be found offline:

  • man java-config
  • java-config --help

For suggestions or questions regarding this document, please email the Gentoo Java team:

This page is based on a document formerly found on our main website
The following people contributed to the original document: Joshua Nichols, Karl Trygve Kalleberg, Joshua Saddler (nightmorph)
They are listed here because wiki history does not allow for any external attribution. If you edit the wiki article, please do not add yourself here; your contributions are recorded on each article's associated history page.