Douglas Jarquin

Java and Tomcat on Ubuntu Hardy

2008 August 4th
Comments

I just spent my entire morning trying to figure out how to install Tomcat on one of my servers. I needed it to demo a business intelligence application, and boy oh boy was it a pain. There is a lot of talk out there about this particular install, but nothing specifically for Ubuntu 8.04 Hardy.

What follows is a quick guide on how to setup Java 1.6.0 and Tomcat 5.5 on a “new” Ubuntu 8.04 Hardy Heron server. We will be using apt-get to install everything, since I hate having to remember to update compiled code.

First things first

First make sure Ubuntu is all up to date:

sudo apt-get update && sudo apt-get upgrade

Java

It was a little hidden, but Ubuntu does have a very detailed how-to for installing Java here. Okay, so it is not very detailed at all. I missed it a few times, but the install steps are there.

sudo apt-get install openjdk-6-jre openjdk-6-jdk

OpenJDK is an open-source Java SDK implementation with support for Ubuntu 8.04 Hardy. Tomcat needs both the JRE and the JDK so make sure to install both if you have not already.

Check on Java

Just to make sure we are all good, run this command to verify that Java installed correctly:

java -version

The output should look something like this:

java version "1.6.0"
OpenJDK Runtime Environment (build 1.6.0-b09)
OpenJDK 64-Bit Server VM (build 1.6.0-b09, mixed mode)

Tomcat

Tomcat was the hard part. There really is not much on the topic, since all Java Web developers are moving to Rails ;), but here is what I put together. There is a package available for Tomcat 5.5, and once installed successfully it will be running on port 8180 of your server. Let’s get to it.

sudo apt-get install apache2 tomcat5.5 tomcat5.5-webapps tomcat5.5-admin

Okay, I kind of let you down here, because the install will fail. Bare with me, it is all part of my plan. If you look carefully at the output you will see why Tomcat’s install failed so miserably.

Setting up tomcat5.5 (5.5.25-5ubuntu1) ...
 * no JDK found - please set JAVA_HOME

Don’t you just love helpful error messages. And I welcome you to the part of the installation that took me hours to figure out. What is required for Tomcat to finish its installation is a simple environment variable. A single line of code that will tell Tomcat where to find our Java install. I tried putting this in the /root/.bashrc, .bash_login, .bash_profile, and ~/.bashrc with no success. I kept thinking there was something wrong with my syntax, but nothing worked. Luckily, I ran into this blog post when doing a Google search for the terms “No JDK found”.

As it turns out, Tomcat has its own place to set this environment variable. Jackpot! Oh, this is why we needed to let the install fail, to create this file for us.

sudo nano /etc/default/tomcat5.5

Then look for this line

#JAVA_HOME=/usr/lib/jvm/java-6-sun

And make it look like this

JAVA_HOME=/usr/lib/jvm/java-6-openjdk

Check the newly set JAVA_HOME with:

echo $JAVA_HOME

We did two things. First we uncommented the line, so Tomcat can parse the environment variable set here. Next, we corrected the path to our Java installation. You can check the Java path yourself by typing sudo update-alternatives --config java. Running which java or whereis java will only display symlinks.

So now we run our Tomcat installation command again. Run the whole command again just to make sure all of the packages get installed. If they were installed successfully they will be skipped, so no harm can come from doing this.

sudo apt-get install apache2 tomcat5.5 tomcat5.5-webapps tomcat5.5-admin

Java and Tomcat are now successfully installed! Visit http://localhost:8180 to see how all of my hard work has paid off. Just in case, here are a few useful Tomcat commands:

# Check Tomcat's status
sudo /etc/init.d/tomcat5.5 status

# Start Tomcat
sudo /etc/init.d/tomcat5.5 start

# Stop Tomcat
sudo /etc/init.d/tomcat5.5 stop

References

Filed under Tutorials
10 comments to...
“Java and Tomcat on Ubuntu Hardy”
ManuelLopez

Really useful. Had same problem! Thanks!


poof65

Nice.
Thanks for sharing this tip.


Greg H

I want to thank you so much for this tutorial. I knew there was a problem with $JAVA_HOME but wasnt sure how to configure the packages to install correctly. Thanks again!


Vikas Mohan

I have gone through your post.I have setup a php development environment in ubuntu 8.04 after a long struggle.so i don’t want to mess it up.As i want to brush up java,i want to install tomcat from tar file.i have done this in fedora core by putting all tomcat folder in /opt and setting environment variables like JAVA_HOME,CATALINA_HOME in .bash_profile but in ubuntu i have no .bash_profile in my home dir.how can i get through.


Vikas

Setting Up Tomcat5.0 tar package in Ubuntu 8.04 LTS Hardy Heron

I have already installed java6 with sudo apt-get install.

I have used the term folder and directory both carry same meaning and do not be confused.

In my above post,i just said that i have installed tomcat.xxx.tar.gz on fedora.And since i have installed LAMP with much trouble so i just don’t want to mess up with tomcat.So i prefer to install from tar package(i don’t know what it is called like binary,precompiled etc..)But the tar package contain the following folders, bin,common,conf,logs,server,shared,temp,webapps,LICENCES,NOTICE and so on.

Ubuntu does not have .bash_profile but it has “.profile” which is in /etc/skel/.profile.In this file there is a line that helped me
to setup tomcat

# ~/.profile: executed by the command interpreter for login shells.
# This file is not read by bash(1), if ~/.bash_profile or ~/.bash_login
# exists.

I tried with .profile but it didn’t worked for me.But the next line above says like this
This file is not read by bash(1), if ~/.bash_profile or ~/.bash_login exists.

I had a backup copy of .bash_profile from fedora.so i just copied into my home directory and set up Environment variables.

BELIEVE ME it’s VERY EASY to set up Tomcat(Just Read and Follow this) To setup tomcat ,you have to set up the following environment variable.

PATH
Append java path to your home directory if it doesn’t exixts.
open a terminal and type “locate javac”
and look for an entry that ends with “javac”

For me this was the path
/usr/lib/jvm/java-6-sun-1.6.0.06/bin/

JAVA_HOME
This path is the location of your java main directory

JAVA_HOME=/usr/lib/jvm/java-6-sun-1.6.0.06

(exclude bin directory from above path)

Now setting up CATALINA_HOME for tomcat
Get the tomcat tar package and extract it anywhere you want.In my case i put it inside
/opt/tomcat5.0
so
CATALINA_HOME=/opt/tomcat5.0

Now we know the environment variables and it’s path.It’s time to put all this in .bash_profile.You can use the following .bash_profile i have given below.just copy(inside two horizontal line) and save into your home directory as .bash_profile
—————————————————————————–
# .bash_profile

# Get the aliases and functions
if [ -f ~/.bashrc ]; then
. ~/.bashrc
fi

# User specific environment and startup programs

PATH=$PATH:$HOME/bin:/usr/lib/jvm/java-6-sun-1.6.0.06/bin/
JAVA_HOME=/usr/lib/jvm/java-6-sun-1.6.0.06
CATALINA_HOME=/opt/tomcat5.0
CLASSPATH=/usr/share/java-1.5.0/jre/lib/ext/mysql-connector-java-3.1.11-bin.jar

export PATH JAVA_HOME CATALINA_HOME CLASSPATH
unset USERNAME

echo “Sourcing done..”
—————————————————————————–
In case you use mysql jdbc driver,set it’s path in CLASSPATH
environment variable.Just download driver and show it’s location to the CLASSPATH variable.Refer the above file and set up your own Environment path.After this you have to source it.open terminal and enter the following command

source .bash_profile press key

You will see like this.if doesn’t!! setup path correctly.

Sourcing done..

Now start tomcat from the terminal with this command
/opt/tomcat5.0/bin/startup.sh

root@-desktop:~# /opt/tomcat5.0/bin/startup.sh
Using CATALINA_BASE: /opt/tomcat5.0
Using CATALINA_HOME: /opt/tomcat5.0
Using CATALINA_TMPDIR: /opt/tomcat5.0/temp
Using JRE_HOME: /usr/lib/jvm/java-6-sun-1.6.0.06
root@-desktop:~#

After this open the follwoing link in firefox or web browser

http://localhost:8080

YOU HAVE DONE TOMCAT IS UP AND RUNNING!!

for stopping tomcat
/opt/tomcat5.0/bin/startup.sh

root@-desktop:~# /opt/tomcat5.0/bin/startup.sh
Using CATALINA_BASE: /opt/tomcat5.0
Using CATALINA_HOME: /opt/tomcat5.0
Using CATALINA_TMPDIR: /opt/tomcat5.0/temp
Using JRE_HOME: /usr/lib/jvm/java-6-sun-1.6.0.06
root@-desktop:~#

Now follow this ONLY if you want to set up a development directory outside tomcat.In this example tomcat is located in /opt/tomcat5

Now i want a development directory in my home directory which is currently root

I am creating a development directory by name “test”

You have to create the following structure for “test”

Inside test create a folder called WEB-INF
test->WEB-INF

Inside WEB-INF create two folders called classes and lib
WEB-INF->classes
WEB-INF->lib
save the following file as web.xml inside WEB-INF Directory

web.xml
—————————————————————————-

Welcome to Tomcat

Welcome to Tomcat

—————————————————————————-
Now tell tomcat the location of your new development directory called “test” through a context file which will be named same as development directory” test.xml” and save it in the following location as “test.xml”

/opt/tomcat5.0/conf/Catalina/localhost/

test.xml
—————————————————————————–

—————————————————————————–
Now you have to re load tomcat either refresh web browser or stop tomcat and start it again.In the side pane(left) you will see a hyper link by name “Tomcat Manager” click on it and when prompted give username and password both as “tomcat” In the application panel you will see listing for the development directory we have just created that is “/test”.

So We Have DONE!!


Curran

Thanks for the guide! I found out that the whole JAVA_HOME error thing can be avoided by executing
sudo update-java-alternatives -s java-6-openjdk
right after installing openjdk.

The following is all I needed for a fully functioning Tomcat:

sudo apt-get install openjdk-6-jre openjdk-6-jdk -y
sudo update-java-alternatives -s java-6-openjdkc
sudo apt-get install tomcat5.5 -y


Curran

Oops, sorry about the typo, it should be

sudo apt-get install openjdk-6-jre openjdk-6-jdk -y
sudo update-java-alternatives -s java-6-openjdk
sudo apt-get install tomcat5.5 -y


Efrain Avila

Thanks a lot for sharing it!!!!!!!!!!!!!!!!!!


hongshu

Thank you for your explanation. But there is still a problem about my computer!


fire legna

thanks for sharing :)




(required)



(required) (Won't be displayed)


Your Comment:

Photostream
Foot ModelingHappy TimesOfficial Baby PictureEthan's First Yawn