Java LAMP How-To - Part 4

Tomcat and the Java JDK
 
I neglected to tell you why we were installing Java in the first place because my reasons are not likely to be yours. However, our Java application environment had a few specific requirements. In particular the Java5 JDK, also referred to as JDK 1.5. This file is available at the following URL:
 
 
Sun will probably change the navigation to the file but the heading over the link which generated the actual download said I was getting the Linux Platform - J2SE(TM) Development Kit 5.0 Update 8. There is an RPM version and a non-RPM version - pick your favorite. I would try to use the RPM version since programs like YUM can keep this up to date for you. (Be sure to choose the file format that is appropriate for your Operating System.) I selected the self-extracting RPM version (about 46MB) because I did not want to worry about keeping the JDK up to date. (In fact, I am going to do another complete installation How-To for these same versions of software using RPM's to see if I get the same results).
 
TECHSNIP:  I took the time to visit the Sun Java website and read the topics in the "New to Java Canter" and the tutorials in JDK. I discovered that they recommed to use the RPM version for RedHat, and I also learned that the installation procedures places a new service script file called jexec in /etc/init.d, which allows any user to directly execute standalone JAR files. The self extracting executable installs into the /usr/java directory. The Linux (32 bit) installation instructions I used are at http://java.sun.com/j2se/1.5.0/install-linux.html#install-rpm
 

 
Installing the Java JDK 
 
It really does not matter where you save this file because it will extract and install in the proper location and register itself, which is very convenient.I dropped this file in the /tmp folder. You need to make this an executable file.
 
chmod +x jdk-1_5_0_08-linux-i586-rpm.bin
 
... then run the file.
 
./jdk-1_5_0_08-linux-i586-rpm.bin
 
Thats about all you need to do for the JDK.
 

 
Installing the Java Server - Tomcat
 
Tomcat is not the only Java Server application out there, but at the time I needed to make a decision it was between Tomcat or Resin, and I flipped a coin. Yeah, real scientific. So maybe you will have the time to do better research for your choice? The Tomcat project is available at Apache.org, and this is the link I used to get the version I needed - Tomcat 5.5: http://tomcat.apache.org/download-55.cgi. Once again I opted for the binary "Core" version, since my software vendor recommended it, and they also recommended not using a development version.
 
You can retrieve this file in tar.gz form with the following command:
 
 
Once you have this file uncompress it and change to the directory that was created.
 
tar -zxf apache-tomcat-5.5.17.tar.gz
 
This should have unpacked the binaries into /usr/local/apache-tomcat-5.5.17. The next thing we do is to set up a symbolic link to Tomcat as follows:
 
ln -s apache-tomcat-5.5.17/ tomcat -f
 
What remains to complete the installtion is to set up environment variables for the Java JDK and Tomcat, JAVA_HOME and CATALINA_HOME respectively. I also set the JAVA_HOME varaibel in the path.
 
JAVA_HOME=/usr/java/jdk1.5.0_8
 
CATALINA_HOME=/usr/local/tomcat
 
export PATH=$JAVA_HOME/bin:$PATH
 
 
TECHSNIP:  You should read the release notes here: http://mirrors.combose.com/apache/tomcat/tomcat-5/v5.5.17/RELEASE-NOTES.
 
The file $CATALINA_HOME/bin/startup.sh can be used for starting Tomcat automatically at boot time from /etc/init.d. Chek this file to make sure that the classname is Bootstrap, and not BootstrapService.
 
 

 
Starting Tomcat as a Service
 
If you want Tomcat to start automatically on boot then you need to set it up as a service. To do this you need to copy the code below and save it as a file called tomcat in the folder /etc/init.d:
 

# This is the init script for starting up the
# Jakarta Tomcat server
#
# chkconfig: 345 91 10
# description: Starts and stops the Tomcat daemon.
#

# Source function library.
. /etc/rc.d/init.d/functions

# Get config.
. /etc/sysconfig/network

# Check that networking is up.
[ "${NETWORKING}" = "no" ] && exit 0

tomcat=/usr/local/tomcat
startup=$tomcat/bin/startup.sh
shutdown=$tomcat/bin/shutdown.sh
export JAVA_HOME=/usr/java/jdk1.5.0_08
export CATALINA_HOME=/usr/local/tomcat

start(){
echo -n $"Starting Tomcat service: "
$startup
RETVAL=$?
echo
}

stop(){
action $"Stopping Tomcat service: " $shutdown
RETVAL=$?
echo
}

status(){
numproc=`ps -ef | grep catalina | grep -v "grep catalina" | wc -l`
if [ $numproc -gt 0 ]; then
echo "Tomcat is running..."
else
echo "Tomcat is stopped..."
fi
}

restart(){
stop
sleep 5
start
}


# See how we were called.
case "$1" in
start)
start
;;
stop)
stop
;;
status)
status
;;
restart)
restart
;;
*)
echo $"Usage: $0 {start|stop|status|restart}"
exit 1
esac

exit 0

 
You may have to change the value for JAVA_HOME if your JDK is in a different location than mine. You then need to make the file executable with the command:
 
chmod +x /etc/init.d/tomcat
 
And finally you need to set it to start at boot with the command:
 
chkconfig --add tomcat
 
You now have a working Tomcat install that will start it self at boot time. You can also interact with it using the service command to start, stop, restart and see the status of the service at any time.