Java User Guide

This guide will introduce you to Java and explain how to use Java with Gentoo Linux.

Overview
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.

Existing installs
For existing installs, regardless of if you have installed anything Java before, make sure you have followed the Java Upgrade Guide.

New installs
New installs should require no further preparation.

The choices
Gentoo provides numerous Runtime Environments (JREs) and Development Kits (JDKs). Among the current choices, we have:

The default for Java 1.4 is the Blackdown JRE/JDK pair, as it is freely ("free as in beer") available without any registration fuss.

JREs and JDKs from Sun, IBM, and BEA are generally faster, but getting them is a bit more work, as you are required to read and accept their license before downloading (IBM additionally requires you to register).

Installing a JRE/JDKs
To install your profile's default JDK, you can run emerge virtual/jdk. Or to install your profile's default JRE, you can emerge virtual/jre.

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.

Setting a default virtual machine
Running the command java-config --list-available-vms will give you a list of all JREs and JDKs installed on your system. Here is an example of output:

The  indicates this is the current active vm (system-vm or user-vm when set). The name in the brackets ([]) is the handle or ID for that particular VM. You use the handle or the number to java-config --set-system-vm. Here is an example of how to set the system VM.

Setting the System VM:

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

Preferred VM
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 default/supported VMs per arch. You can find them in /usr/share/java-config-2/config/jdk-defaults.conf. When you are merging a Java package, and it detects one of the VM in that file is installed, it will automatically use that VM, instead of the system-vm.

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 /etc/java-config-2/build/jdk.conf and have complete control over which VM will get used. See the following examples.

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.:

Always use sun-jdk-1.5 wherever possible, except for when a 1.4 or 1.3 VM is explicitly required:

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:

Compilers
The standard Java compiler used for building is, 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:

Some compilers don't support all possible  and   arguments. Therefore, each compiler in the list is checked to see if it can support the desired /. 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:

Setting a default CLASSPATH
{{Warning|The options explained in this section should be considered deprecated and will most likely be removed in the future. We strongly recommend against using these, because your Java projects or application should ideally manage their own classpaths. If you choose to specify a default CLASSPATH, some applications may behave unexpectedly, because classes they weren't expecting would 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, 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:

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

You will have to update your environment by logging out, then in again or sourcing /etc/profile.

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

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.

Setting classpath:

Installing a plugin
You can install a Java plugin for your web browser by emerging a Java VM with the nsplugin USE flag set.

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:

In this example, sun-jre-bin is selected for the browser plugin. Select a plugin:

Verify that the correct plugin was selected:

Java.com also provides a link to verify your installed plugin. Additionally, if you are using a Mozilla-based browser, you can verify your Java plugin by typing about:plugins into the address bar.

Plugins on multilib systems
If you are running a mixed 64-bit and 32-bit multilib system (for example, on AMD64), you can only use 32-bit Java plugins.

To use a 32-bit plugin, you will need to emerge emul-linux-x86-java with the nsplugin USE flag enabled:

Next, check which plugins are available:

Although you must select a 32-bit browser (such as mozilla-firefox-bin) to use with your 32-bit plugin, the 64-bit version of konqueror uses your Java VM directly, so it's possible to use the 64-bit version of blackdown with this browser; no further configuration is necessary.

Now select the right plugin for your 32-bit browser:

Verify the correct plugin was selected:

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

The flags

 * The java flag adds support for Java in a variety of programs
 * The nsplugin flag adds support for Mozilla-like browsers (including Firefox). You will need this for viewing Java applets in your Mozilla-like browser.
 * 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 you are using.
 * The jce flag adds support for the Java Cryptography Engine
 * For Java packages, the doc flag will build API documentation using javadoc.

Additional resources

 * java-config man page
 * java-config --help

Online resources

 * The Java Project Page
 * The gentoo-java, gentoo-user, and gentoo-dev mailing list archives
 * #gentoo and #gentoo-java on IRC
 * Wikipedia's entry for Java
 * If you have suggestions or questions regarding this document, please email the Gentoo Java team: java@gentoo.org