Configuring OpenJDK on Linux
Linux configuration profiles differ from the Windows ones, because those systems are working with resources as well as with hardware in a slightly different fashion. Here, we will briefly explain those differences and a way to overcome them. Moreover, different Linux distributions, as always, have different ways to deal with the configuration. We will try to pass through the most noticeable ones.
Getting ready
To follow this recipe, you will need an OpenJDK instance installed on a Linux system. The deb, rpm, or ebuild distributions will suite us really well, although we will see a method for generic Linux configuration also.
Also, we will need the bash startup files to be installed properly.
Tip
In most Linux distributions, the generic way to configure anything that needs root access is not recommended, and the results of such an approach tend to vanish with each update. Usually, there are distribution-recommended how-to's where the problem solution is described.
How to do it...
First let's check whether your bash startup files are installed. The simplest possible way is to configure your OpenJDK using them. They are system-wide and easy to use, though there are drawbacks in their usage, such as update conflicts:
- Type the following line in your terminal:
cat /etc/profile
If the file exists and contains some kind of shell script, then your bash startup file's setup is probably correct. If not, please set it up by following your distribution instructions.
- Then add the
/etc/profile.d/openjdk.shfile. - In order to configure different things, write the following:
To set JAVA_HOME JAVA_HOME=<youJDK installation directory> export JAVA_HOME To append JAVA_HOME to PATH pathappend $JAVA_HOME/bin PATH To adjust CLASSPATH directory AUTO_CLASSPATH_DIR=<classpath dir> pathprepend . CLASSPATH for dir in `find ${AUTO_CLASSPATH_DIR} -type d 2>/dev/null`; do pathappend $dir CLASSPATH done for jar in `find ${AUTO_CLASSPATH_DIR} -name "*.jar" 2>/dev/null`; do pathappend $jar CLASSPATH done export CLASSPATHTip
The
CLASSPATHenv variable should be avoided as much as possible. It is generally used by legacy Java applications mostly configured for JDK 1.2 and below. Use-classpathwithjavaandjavaccommands instead.
The preceding code is quite simple—it just appends all JAR files to the classpath.
How it works…
This script is called during shell initialization, so whenever you perform shell initialization, these variables will be exported. The variables are thus system-wide, so be careful while playing with them, as they can cause your Java to fail permanently if you make some errors in this file.
There's more...
On Linux, you can see the directory structure of the installed OpenJDK using the tree command.
To do so, install the tree package (use your distribution's documentation if possible) and type:
tree -L 1 <path-to-openjdk> -lah
You will see something like the following:
/usr/lib/jvm/java-7-openjdk-amd64 ├── [ 22] ASSEMBLY_EXCEPTION -> jre/ASSEMBLY_EXCEPTION ├── [4.0K] bin ├── [ 41] docs -> ../../../share/doc/openjdk-7-jre-headless ├── [4.0K] include ├── [4.0K] jre ├── [4.0K] lib ├── [4.0K] man ├── [ 20] src.zip -> ../openjdk-7/src.zip └── [ 22] THIRD_PARTY_README -> jre/THIRD_PARTY_README
This is the first-level directory structure in which:
ASSEMBLY_EXCEPTIONis about licensing, and so isTHIRD_PARTY_README.- The
docsfolder is for various OpenJDK documentation (changelog, copyrights, authors, and so on). - The
includedirectory is to include paths (forJNI, for example). - The
jredirectory is where the Java Runtime is placed. - The
libdirectory is where various OpenJDK libraries are placed (such as Jigsaw or CORBA support; mainly, it consists of all OpenJDK code). - The
mancommand is a manual pages entry for OpenJDK. It contains OpenJDK classes,javadocs, and other manual entries. It may be extremely useful in the highly improbable event of Internet connection loss.