Java requires two environment variables be set for optimal operation. For UNIX operating systems one typically modifies a startup file like ~/.bash_login to set and export shell variables while Mac users typically set and export environment variables in .bash_profile. For Windows, go to Start -> Control Panel -> System -> Advanced -> Environment Variables and set JAVA_HOME via the GUI (samples below).
JAVA_HOME
Set the JAVA_HOME environment variable to point to the base directory of your Java installation.
Linux/Unix/: export JAVA_HOME=/usr/java/java-current
Mac: export JAVA_HOME=/Library/Java/Home
Windows: JAVA_HOME=C:\j2sdk1.5.0_xx ("xx" = the maintenance/revision number, e.g. "j2sdk1.5.0_13")
If your machine has already set the variable JRE_HOME or if you want to use a particular JRE if you have more than one JRE installed on your machine then you'll want to set the JRE_HOME variable as well. JRE_HOME is what Apache Tomcat uses when it starts up, but it defaults to use JAVA_HOME if JRE_HOME is not set. In most cases, setting JAVA_HOME should cover both cases sufficiently.
PATH
Next, add Java's /bin directory to the PATH environment variable:
Linux/Unix/Mac: export PATH=$PATH:$JAVA_HOME/bin/
Windows: append the string ;C:\jdk1.5.0_xx\bin to the end of the system variable named Path ("xx" = the maintenance/revision number, e.g. "j2sdk1.5.0_13")
You should check that you have set your environment variables correctly. For both Windows XP and Unix/Linux/Mac operating systems start a new shell and run the set command to see your environment variables.
$ set [excerpt] JAVA_HOME=/Library/Java/Home ... PATH=/Library/Java/Home/bin [etc] ....