Installing Java JDK on Centos

By default, RedHat Linux AS and AS4 servers don’t come with a JDK (Java Development Kit) installed on them. Depending on your install, if you run the java command you may get some sort of error message or a file not found message.

  1. First we need to download a JDK. I am downloading JDK 6u2 from http://java.sun.com/javase/downloads/index.jsp. Go to that page and click the Download button. On the new page that loads, we will download the Linux RPM in self-extracting file.
  2. Once you have downloaded your file, and have it placed somewhere on your Linux box (I put mine in /tmp), we will extract and install it. The name of the file I downloaded was jdk-6u2-linux-i586-rpm.bin.
  3. Login to your Linux box as the root user, and change directory to where you placed the jdk file that you downloaded.
  4. Make sure the file you downloaded is executable.
    chmod 755 jdk-6u2-linux-i586-rpm.bin
    
  5. Now run the file.

    ./jdk-6u2-linux-i586-rpm.bin
    
  6. A license agreement will appear on your screen displayed through the more command. Simply press the enter key until you get to the end, or just press the q key to skip right to the end.

  7. At the end of the agreement, type yes to agree to it and continue with the install.
  8. The JDK will now unpack and install itself. Your screen output should look similar to the following:
    Unpacking...
    Checksumming...
    Extracting...
    UnZipSFX 5.50 of 17 February 2002, by Info-ZIP (Zip-Bugs@lists.wku.edu).
      inflating: jdk-6u2-linux-i586.rpm  
    Preparing...                ########################################### [100%]
       1:jdk                       ########################################### [100%]
    Unpacking JAR files...
            rt.jar...
            jsse.jar...
            charsets.jar...
            tools.jar...
            localedata.jar...
            plugin.jar...
            javaws.jar...
            deploy.jar...
     
    Done.
    
  9. The install routine installs the files into the directory /usr/java/jdk1.6.0_02. It also creates two links in that directory named default and latest. It’s about time Sun has done this. I have been manually doing something like this since the JDK 1.2 days. These links make it easy to migrate to a new JVM, as you would reference the link in your shell scripts and not the actual JVM directory itself.

  10. Now we want our system to automatically put the JDK binary files into our PATH when we login for all users. To do this, edit the /etc/profile file and place the following two lines at the bottom of your file:
    JAVA_HOME=/usr/java/default; export JAVA_HOME
    PATH=$JAVA_HOME/bin:$PATH; export PATH
    
  11. Now if you logout of your system and then log back in, you should be able to run the java command to print its version. Run the following command:

    java -version
    

    And your output should look like:

    java version "1.6.0_02"
    Java(TM) SE Runtime Environment (build 1.6.0_02-b06)
    Java HotSpot(TM) Client VM (build 1.6.0_02-b06, mixed mode, sharing)
    
  12. Congratulations, your JDK is installed!

Now we can install tomcat

The Basics – Download and Install The Software

  1. First make sure you have a Java Development Kit installed on your server. I have a how to do this at http://www.mr-webcam.com/linux-help/jdk.php.
  2. Download the Tomcat binary distribution from http://tomcat.apache.org/download-60.cgi. I selected the tar.gz option under the Core section.
  3. Once you have downloaded your file, place it somewhere on your Linux box (I put mine in /tmp/tomcat). The name of the file I downloaded was apache-tomcat-6.0.14.tar.gz.
  4. Login to your Linux box as the root user, and change directory to where you placed the Tomcat file that you downloaded.
  5. Make a directory to be the root of our Tomcat Installation.
    mkdir /usr/local/tomcat/
    
  6. Move the file we downloaded to the directory we created in the previous step.

    mv apache-tomcat-6.0.14.tar.gz /usr/local/tomcat/
    
  7. Change to the /usr/local/tomcat directory.

    cd /usr/local/tomcat
    
  8. Unzip and untar the tomcat file.

    tar -zxvf apache-tomcat-6.0.14.tar.gz
    
  9. You should now have a directory named /usr/local/tomcat/apache-tomcat-6.0.14. If you wish you can delete the install .tar.gz file named apache-tomcat-6.0.14.tar.gz.

    rm apache-tomcat-6.0.14.tar.gz
    
  10. Ensure you have a JAVA_HOME environment variable setup. If you don’t, when you go to startup Tomcat, you will get a error that says:

    Neither the JAVA_HOME nor the JRE_HOME environment variable is defined
    At least one of these environment variable is needed to run this program
    

    Since my JDK is installed in the directory /usr/java/default, I have the following set in my /etc/profile file to always set the JAVA_HOME variable and to put the Java binaries into my PATH:

    JAVA_HOME=/usr/java/default; export JAVA_HOME
    PATH=$JAVA_HOME/bin:$PATH; export PATH
    
  11. Change to your new Tomcat bin directory and start it up:

    cd /usr/local/tomcat/apache-tomcat-6.0.14/bin
    ./startup.sh
    

    Your output should look like:

    Using CATALINA_BASE:   /usr/local/tomcat/apache-tomcat-6.0.14
    Using CATALINA_HOME:   /usr/local/tomcat/apache-tomcat-6.0.14
    Using CATALINA_TMPDIR: /usr/local/tomcat/apache-tomcat-6.0.14/temp
    Using JRE_HOME:       /usr/java/default
    

    And doing a ps -aef | grep tomcat should show a Tomcat process running.

  12. Lastly, go to your client PC and open up a web browser. You should now be able to load up a page on port 8080 from the server you just installed Tomcat on. I loaded this URL in my web browser:
    http://192.168.1.50:8080
    

    And like magic, the default Tomcat page showed up in my web browser. The IP address or hostname will most likely be different than the one I specified above.

  13. If you want to shutdown your Tomcat service, the command to run is simply:
    ./shutdown.sh
    
  14. The last step I perform is to create a link named /usr/local/tomcat/current that links to my Tomcat install directory. This is so that I don’t have to hard code the specific installation directory in my scripts. When I upgrade to a new version of Tomcat, I don’t have to modify any of my scripts that reference the Tomcat installation directory. Instead I simply update the symbolic link. To do this run the following commands:

    cd /usr/local/tomcat
    ln -sf apache-tomcat-6.0.14 current 
    

    I use these symbolic links in the startup and shutdown scripts that I reference below.

  15. Congratulations, the basics are now done, and we have a working Tomcat installation to begin web development with! In later posts we will use this Tomcat installation to demonstrate some various web development techniques, retrieve and display data from your databases, and more. I also plan to show how to setup the Apache HTTP server to serve up your static content and forward requests for your dynamic content to the Tomcat installation. As a bonus, continue to read on for a system init script that will start Tomcat automcatically when your system boots!


Setup Tomcat to Start Automatically When the System Boots

In order to start Tomcat automatically when my server boots up, I setup a script in /etc/init.d. Follow the instructions below to do the same.

  1. First ensure you have the symbolic link named /usr/local/tomcat/current setup as a described above. If you don’t, then you’ll have to replace all occurrences of /usr/local/tomcat/current in the script below with your own TOMCAT_HOME location.
  2. Create the file /etc/init.d/tomcat
    vi /etc/init.d/tomcat
    

    …and place the following script in it:

    #!/bin/bash
    #
    # Startup script for Tomcat
    #
    # chkconfig: - 86 15
    # description: Tomcat is a JSP server.
    # processname: tomcat
    
    
    JAVA_HOME=/usr/java/default
    export JAVA_HOME
    tomcat_home=/usr/local/tomcat/current/bin
    start_tomcat=/usr/local/tomcat/current/bin/startup.sh
    stop_tomcat=/usr/local/tomcat/current/bin/shutdown.sh
    
    start() {
            echo -n "Starting tomcat: "
            cd $tomcat_home
            ${start_tomcat}
            echo "done."
    }       
    stop() {
            echo -n "Shutting down tomcat: "
            cd $tomcat_home
            ${stop_tomcat}
            echo "done."
    }
    
    # See how we were called
    case "$1" in
      start)
            start
            ;;
      stop)
            stop
            ;;
      restart)
            stop
            sleep 10
            start
            ;;
      *)    
            echo "Usage: $0 {start|stop|restart}"
    esac
    
    exit 0
    
  3. Make sure we set appropriate permissions on the new file:

    chmod 755 /etc/init.d/tomcat
    
  4. Now we use the chkconfig command to add the script to the list of system services.

    chkconfig --add tomcat
    
  5. Now we tell the system to start the tomcat service for run levels 3 and 5.

    chkconfig --level 35 tomcat on
    
  6. And lastly, we verify that this has indeed occurred. Run the following command:

    chkconfig --list tomcat
    

    And your output should look like the following:

    tomcat          0:off   1:off   2:off   3:on    4:off   5:on    6:off
    
  7. You should now be able to test your scripts. Running the commands below should start and stop your Tomcat service.

    /etc/init.d/tomcat start
    /etc/init.d/tomcat stop
    

    Furthermore, you should be able to reboot your system and ensure Tomcat starts automatically.