Friday, January 21, 2011

Setting Up Tomcat and Eclipse in Ubuntu Tutorial

This had me scratching my head for several days, but I finally put all the pieces together. If you want to get around to building some JSP stuff in Tomcat using Eclipse on Ubuntu (or some other Linux) you might run into some problems like I did and just complete a simple Hello World tutorial. Here's the problems and the solutions.

Problem: You don't have an option to create a new "Dynamic Web Project"
Cause: You don't have the Web Tools Platform installed.
Solution: Download WTP.  If you already have Eclipse you can update installation here.

Problem: You are getting any slurry of errors about permissions or can't write or can't find or can't configure errors. They are all varied and confusing and difficult to google. So you probably won't find this page by looking for them.
Cause: You are using the default Tomcat installation from apt. This saves you from worrying about the installation, but it isn't setup the way Eclipse wants you to.
Solution: Run the commands found here. I've archived them incase the forum ever dies or changes drastically.

sudo apt-get install tomcat6
cd /usr/share/tomcat6
sudo ln -s /var/lib/tomcat6/conf conf
sudo ln -s /etc/tomcat6/policy.d/03catalina.policy conf/catalina.policy
sudo ln -s /var/log/tomcat6 log
sudo chmod -R 777 /usr/share/tomcat6/conf

Problem: You get an error about port 8005 and 8080 already in use.
Cause: Tomcat is already running on those ports so they are of course in use.
Solution: Tell Eclipse to start it's Tomcat on a different port. Use the directions found here. I've archived the text incase the page ever dies or changes drastically.

Click on servers tab in eclipse and then double click on the server listed there
Then when the config page opens change the port by clicking on the numbers and typing over them

Problem: You are getting weird Java errors about objects not found or classes or something.
Cause: The Hello World you are attempting to run is way over your head.
Solution: Try something simpler to make sure your Tomcat and Eclipse setup is working properly.  The example from IBM is old but simple.  I'm not going to archive it for you.

Tuesday, January 4, 2011

Compiling Class Files to Java Files for Tomcat

I'm going through some tutorials and teaching myself some JSP Struts stuff from scratch.  I decided to go the classic route and just use whatever Debian Lenny includes in apt so I don't have to worry about new exciting features, just old reliable stuff.

One of the first things you'll need to do with JSP is compile your Class Files to a Java File so it can be properly loaded.  You can't run a JAVA file in Tomcat.  It just won't work.

I figured since I used apt to install all the goodies it would work perfectly so I tried to compile the first example java file from the tutorial I was following.

So I ran:

I got an error about servlet stuff not found.

import javax.servlet.http.*;
The import javax.servlet cannot be resolved

So I tracked down the servlet stuff jar file and ran:
javac -classpath /usr/share/tomcat5.5/common/lib/servlet-api.jar

It fixed the servlet not found error, but now I got an error about org.apache stuff not found.

import org.apache.struts.action.*;
The import org.apache cannot be resolved

So I tracked down the struts stuff jar file and tried:
javac -classpath /usr/share/tomcat5.5/common/lib/servlet-api.jar:/usr/share/struts1.2/struts.jar

Hooray! It Worked.

I'm not a sucker though, I don't want to have to add all that every time I want to compile a java file relating to Tomcat or Struts though.  So in Debian I just go track down and vim my /etc/profile file and add the text:

export CLASSPATH=.:/usr/share/tomcat5.5/common/lib/servlet-api.jar:/usr/share/struts1.2/struts.jar

(Right before the "umask 022")

Now when you log out and log back in, you can run:
javac and it'll work perfectly!

Monday, January 3, 2011

Tomcat and Struts and Debian

I decided I need to learn some JSP to get back in the JAVA game.
Just for stability and old school cool, I'm using Debian Lenny and the Tomcat that it installs.
You can apt-get install tomcat5.5 and tomcat5.5-admin and tomcat5.5-webapps to get everything you need.
It installs struts 1.2 by default so you don't have to go looking.

The Tomcat Default folder is /usr/share/tomcat5.5/