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
- /etc/profile [^sysconfdir]
- ~/.bash_profile
- ~/.bash_login
- ~/.profile
Non-login / Interactive Shell (for example gnome-terminal)
Bash executes the following in order
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
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)