Terry : Set Java Environment Variables

Debian GNU/Linux and Ubuntu

Install Sun Java JDK, JRE and plugin

sudo apt-get install sun-java6-jdk sun-java6-fonts sun-java6-plugin

Sun Java and Oracle Java have been removed from Ubuntu official repositories.

Use oab-java.sh script from GitHub to build Debian/Ubuntu .deb packages and set up a local repository, install from there, this is high recommended.

Set the default JDK at system level

sudo update-alternatives --config java

If you want to choose which JDK to use for apps installed via Debian deb package, set the default JVM in /etc/jvm.

For example, in Ubuntu Intrepid Ibex 8.10:

/usr/lib/jvm/java-6-sun-1.6.0.10

Of course the traditional way still works. Edit ~/.bash_profile, ~/.bashrc or /etc/profile.

Session Wide

~/.bash_profile
~/.profile
~/.bashrc

Difference

Login / Non-interactive Shell & Login / Interactive Shell

Bash executes the following files in order

  1. /etc/profile [^sysconfdir]
  2. ~/.bash_profile
  3. ~/.bash_login
  4. ~/.profile

Non-login / Interactive Shell (for example gnome-terminal)

Bash executes the following in order

  1. Global bashrc (defined as SYS_BASHRC when compiling, by default it is /etc/bash.bashrc)
    ~/.bashrc [^exception_bashrc]

System Wide

/etc/profile
/etc/bash.bashrc
/etc/environment

Bash startup files

Bash Startup Files

Icon

Invoked as an interactive login shell, or with --login

When Bash is invoked as an interactive login shell, or as a non-interactive shell with the --login option, it first reads and executes commands from the file /etc/profile, if that file exists. After reading that file, it looks for ~/.bash_profile, ~/.bash_login, and ~/.profile, in that order, and reads and executes commands from the first one that exists and is readable. The --noprofile option may be used when the shell is started to inhibit this behavior.
When a login shell exits, Bash reads and executes commands from the file ~/.bash_logout, if it exists.

Invoked as an interactive non-login shell

When an interactive shell that is not a login shell is started, Bash reads and executes commands from ~/.bashrc, if that file exists. This may be inhibited by using the --norc option. The --rcfile file option will force Bash to read and execute commands from file instead of ~/.bashrc.

So, typically, your ~/.bash_profile contains the line

if [ -f ~/.bashrc ]; then
    . ~/.bashrc;
fi

after (or before) any login-specific initializations.

Gist https://gist.github.com/1564928

Red Hat Enterprise Linux, Oracle Enterprise Linux and CentOS

Use the rpm.bin download from Sun, it's a binary file which extracts the rpm packages.

By default, JDK 1.6.0_16 will be installed under /usr/java /jdk1.6.0_16. There are 2 soft links under /usr/java, latest and defaut, which makes it easier to set up the environment variable.

Use the path below to avoid changing the JDK path every time you upgrade JDK.

JAVA_HOME=/usr/java/latest

Traditional way of setting Java environment variables

Single user's .bash_profile or .bashrc which is loaded whenever a Terminal is opened.

vi ~/.bash_profile

Add the snippet:

export JAVA_HOME=/usr/java/jdk1.5.0
export CLASSPATH=.:$JAVA_HOME/lib/tools.jar:$JAVA_HOME/lib/dt.jar
export PATH=$JAVA_HOME/bin:$PATH

Or write it in a script file like setenv.sh, chmod u+x setenv.sh and then source it.

source /path/setenv.sh

Test using java -version

terry@tux:~$ java -version
java version "1.6.0_15"
Java(TM) SE Runtime Environment (build 1.6.0_15-b03)
Java HotSpot(TM) Server VM (build 14.1-b02, mixed mode)

2. system wide profile, /etc/profile

Add

JAVA_HOME=/usr/java/latest
PATH=$JAVA_HOME/bin:$PATH
CLASSPATH=.:$JAVA_HOME/lib/tools.jar:$JAVA_HOME/lib/dt.jar
export PATH JAVA_HOME CLASSPATH

Save, log out and login again to see if it works. It it doesn't, try source /etc/profile.

3 write a script, script under /etc/profile.d will be loaded when starting up.
nano /etc/profile.d/java.sh

#Set JDK for all users
JAVA_HOME=/usr/java/latest
CLASSPATH=.:$JAVA_HOME/lib/tools.jar:$JAVA_HOME/lib/dt.jar
PATH=$JAVA_HOME/bin:$PATH
export PATH JAVA_HOME CLASSPATH

Set permissions

chmod 755 /etc/profile.d/java.sh (755 -rwxr-xr-x)

Reference

Bash Startup Files

Google Docs note

Environment Variables

Java Installation