Java is a programming language developed by engineers of Sun Microsystems. The language is object-oriented and designed to run on multiple platforms without the need of recompiling code for each platform. Although Java can be compiled as a native program, much of Java's popularity can be attributed to its portability, along with other features such as garbage collection. To make platform independence possible the Java compiler compiles the Java code to an intermediate representation called "Java bytecode" that runs on a JRE (Java Runtime Environment) and not directly on the operating system.
In order to run Java bytecode, one needs to have a JRE (Java Runtime Environment) installed. A JRE provides core libraries, a platform dependent Java Virtual Machine, plugins for browsers, among other things. A JDK (Java Development Kit) adds programming tools, such as a bytecode compiler and a debugger.
For existing installs, regardless of if you have installed anything Java
before, make sure you have followed the
New installs should require no further preparation.
Gentoo provides numerous Runtime Environments (JREs) and Development Kits (JDKs). Among the current choices, we have:
| Vendor | JDK | JRE |
|---|---|---|
To install your profile's default JDK, you can run
In recent events, Sun has relicensed their JDK and JRE under a more Linux distro friendly license. As a result, Sun releases Java 1.5 and onwards are freely downloadable, without any further hassle.
Some of the JDKs and JREs require you to jump through a few hoops before installing. Simply emerge the packages as you normally would. The ebuilds will then instruct you where to go and what to download.
You should download the indicated file(s) into
Gentoo has the ability to have multiple JDKs and JREs installed without causing conflicts.
Using the
Running the command
# java-config --list-available-vms The following VMs are available for generation-2: 1) Blackdown JDK 1.4.2.03 [blackdown-jdk-1.4.2](Build Only) 2) Blackdown JRE 1.4.2.03 [blackdown-jre-1.4.2](Build Only) 3) IcedTea6-bin 1.4.1 [icedtea6-bin] 4) Sun JDK 1.5.0.20 [sun-jdk-1.5](Build Only) *) Sun JDK 1.6.0.16 [sun-jdk-1.6]
The
(By handle (preferred)) # java-config --set-system-vm blackdown-jdk-1.4 Now using blackdown-jdk-1.4 as your generation-2 system JVM WARNING: blackdown-jdk-1.4 is marked as a build-only JVM. Using this vm is not recommended.(By number) # java-config --set-system-vm 5 Now using sun-jdk-1.6 as your generation-2 system JVM
As a regular user, you can use
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 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 your system or user VM is strongly discouraged as these VMs
will then be used when running the
While merging Java packages, the VM can and will be switched as necessary.
Because of the wide variety of available VMs, we do not have the resources to
test and verify every package works on all of them. So to ensure that every
packages merges smoothly, we have defined a list of
The merge time VM switching is also needed when, for example, your system-vm is set a 1.4 VM and the package you are merging requires a 1.5 VM. While merging it will use the preferred 1.5 VM, leaving your system-vm choice intact.
Of course, Gentoo is all about choice, so you can override these defaults in
(I always want it to use a sun-jdk, ie sun-jdk-1.4 for 1.4, sun-jdk-1.5 for 1.5, etc) *=sun-jdk
(Always use sun-jdk-1.5 wherever possible, except for when a 1.4 or 1.3 VM is explicitly required) *=sun-jdk-1.5
# For 1.3 I prefer sun-jdk 1.4 but when it is not available, use ibm-jdk-bin, # For 1.4, use blackdown-jdk, and for 1.5, use sun-jdk 1.3=sun-jdk-1.4 ibm-jdk-bin 1.4=blackdown-jdk 1.5=sun-jdk
The standard Java compiler used for building is javac, which comes with each
JDK. In addition to configuring the VM used at build time, it is also possible
configure which compiler is used. Essentially, you define a list your
preference for which compiler to use in
# If the ebuild supports it # it will check the COMPILERS var front to back and # use the first compiler that is installed COMPILERS="ecj-X.Y jikes javac"
Some compilers don't support all possible -target and -source arguments. Therefore, each compiler in the list is checked to see if it can support the desired -source/-target. javac will work in all cases, so if no other suitable compiler is found, it will be used instead.
More details about each compiler are provided below:
| Name | Handle | Package | Description |
|---|---|---|---|
First, you will want to list available Java libraries installed on your system that might want to be put in your CLASSPATH. Here is an example of output:
# 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 (
# java-config --set-system-classpath log4j,xerces-2
You will have to update your environment by logging out, then in again or
sourcing
For users,
if [[ -f "${HOME}/.gentoo/java-env-classpath" ]]; then
source ${HOME}/.gentoo/java-env-classpath
fi
If you really want a system wide or user default classpath you can add something like the following to your shell's profile. But we would advise against it.
# export CLASSPATH="${CLASSPATH}:$(java-config --classpath log4j,xerces-2)"
You can install a Java plugin for your web browser by emerging a Java VM with
the
Portage will allow you to install multiple versions of Java plugins, though only one will be used by your browser. You can check the list of available plugins by running:
# eselect java-nsplugin list [1] sun-jre-bin-1.6 [2] icedtea6-bin
In this example,
# eselect java-nsplugin set sun-jre-bin-1.6
Verify that the correct plugin was selected:
# eselect java-nsplugin list [1] sun-jre-bin-1.6 current [2] icedtea6-bin
Java.com also provides a link to
If you are running a mixed 64-bit and 32-bit multilib system (for example, on AMD64), you can use 64-bit and 32-bit Java plugins. Unless you have a pressing need to run 32-bit Java applications, we recommend using native 64-bit plugins on 64-bit web browsers.
There are several native 64-bit browser plugins available. The default JDK/JRE
pair,
# echo "dev-java/sun-jre-bin nsplugin" >> /etc/portage/package.use # emerge sun-jre-bin
To use a 32-bit plugin on a 32-bit browser, you will need to emerge
# echo "app-emulation/emul-linux-x86-java nsplugin" >> /etc/portage/package.use # emerge emul-linux-x86-java
Next, check which plugins are available:
# eselect java-nsplugin list Available 32-bit Java browser plugins [1] emul-linux-x86-java-1.5 [2] emul-linux-x86-java-1.6 Available 64-bit Java browser plugins [1] icedtea6-bin [2] sun-jre-bin-1.6
Now select the right plugin for your browsers:
(Choose the plugins for 32-bit and 64-bit browsers) # eselect java-nsplugin set 32bit emul-linux-x86-java-1.6 # eselect java-nsplugin set 64bit sun-jre-bin-1.6
Verify the correct plugin was selected:
# eselect java-nsplugin list Available 32-bit Java browser plugins [1] emul-linux-x86-java-1.5 [2] emul-linux-x86-java-1.6 current Available 64-bit Java browser plugins [1] icedtea6-bin [2] sun-jre-bin-1.6 current
For more information regarding USE flags, refer to the