Book Image

OpenJDK Cookbook

Book Image

OpenJDK Cookbook

Overview of this book

Table of Contents (20 chapters)
OpenJDK Cookbook
Credits
About the Authors
About the Reviewers
www.PacktPub.com
Preface
Index

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:

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

  2. Then add the /etc/profile.d/openjdk.sh file.

  3. 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 CLASSPATH

    Tip

    The CLASSPATH env 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 -classpath with java and javac commands 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_EXCEPTION is about licensing, and so is THIRD_PARTY_README.

  • The docs folder is for various OpenJDK documentation (changelog, copyrights, authors, and so on).

  • The include directory is to include paths (for JNI, for example).

  • The jre directory is where the Java Runtime is placed.

  • The lib directory is where various OpenJDK libraries are placed (such as Jigsaw or CORBA support; mainly, it consists of all OpenJDK code).

  • The man command 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.