Tuesday, 12 January 2010

Using Tomcat 6 on Xubuntu

Currently I'm doing some Java education.  In order to learn and understand some of the mechanics, I'm using a plain text editor instead of a sophisticated IDE and compiling on the command line.

I'm running in a VM, so I'm using Tomcat on Xubuntu (because it's reasonably lightweight) and Geany as my text editor.  This isn't as plain as using Mousepad (the default text editor in Xubuntu) but I'm only using it for syntax highlighting and outlining at the moment.  Once the compiling is done then testing is done running java manually from the command line and deployment is done by copying files manually into the Tomcat webapps directory.

It's all a bit different from my usual working environment, which is IBM Rational Application Developer (based on Eclipse) - auto-completing class names, automatically sorting out my imports, creating WAR files for me, and deploying to WebSphere Portal.  It took me a little while to figure out where things need to be deployed, and I'm not sure I'd ever run a Java program from the command line before!

So here's a reminder to myself:

  • web apps are deployed in /var/lib/tomcat6/webapps
  • compiling is done from the root development directory, like this:
~/webapps/MyWebApp$ javac -classpath classes:. -d classes src/com/example/model/TestModel.java

where the classpath is a colon-separated list of the parent directories of classes (i.e. "classes" has the directory structure com/example/etc underneath it) and the -d switch controls where the compiled output goes.
  • running a Java program (say, a test harness): 
~/webapps/MyWebApp$ java -cp src:classes com.example.model.test.TestModel param

where, again, the classpath (-cp) is the parent directory of necessary directory structures, and the class to be run is specified by its fully qualified name, not its path.

  • If you use the Tomcat web application manager to deploy a WAR file via the UI, the context root for the application is <war-name>/<root-mapping>. So if the mapping in web.xml is, say "/myservlet" and you WAR is called MyServlet.war, the correct place for the app is /MyServlet/myservlet. (This caught me out and cost me about an hour one time ...)


It might all seem simple stuff to you but when you're used to the tool doing it for you, it's easy to forget.  Took me a couple of goes to get it right!

No comments:

Post a Comment