Running Tomcat from the JBuilder IDE without specifying Tomcat's full path.

By: Alexey Solofnenko

Abstract: Everybody is talking about zero administration. The article describes how to make Web projects location-insensitive so programmers can run and debug Tomcat apps from any path from JBuilder. By Alexey N. Solofnenko.

Everything should be made as simple as possible, but not simpler. --Albert Einstein

Life is complex enough; when we get a chance to make it simpler, we should not hesitate. On some recent projects, I found that doing a build was not a trivial problem! Some projects required special drive mappings, or environment variables, or third-party libraries. So in the first project I managed I decided that a programmer should be able to compile and run the sources immediately after they are checked out.

(Well, almost...we still needed JBuilder and a C++ compiler installed.)

The problem was that absolute paths were required to compile and run our projects.

We dealt with libraries by putting them into CVS. JBuilder helped us by providing local (project) library definitions. But that wasn't a sufficient solution for configuring Tomcat.

With some trial and error, we found a solution. Here is an example how to start Tomcat without knowing where it is installed. The class works with any version of JBuilder greater than 3.5 and it should work with other IDEs too.

First you need to define Tomcat library. I hope you know how to do it. With Tomcat 4.0.1 I added the following jars:

  • bin/bootstrap.jar
  • common/lib/*
  • server/lib/*
  • lib/*

Next, configure your project to put the generated class files into PATH/webapps/*youapp*/WEB-INF/classes.

Copy Tomcat.java into your source directory and change it to have your Web app's directory correct. Here is Tomcat.java:

Created with JBuilder

package com.inventigo.misc;

import java.net.*;
import java.io.*;
import java.util.*;

/*
 * This class helps start Tomcat without specifying the full path to Tomcat's
 * configuration directory when it is started from JBuilder.
 */

public class Tomcat {
  // classpath suffix: /webapps/app/WEB-INF/classes
  private final static String PATH_SUFFIX=File.separator+"webapps"+File.separatorChar+"app"+File.separatorChar+"WEB-INF"+File.separatorChar+"classes";
    public static void main(String[] args) {
    if (System.getProperty("catalina.home")==null) {
      String classpath=System.getProperty("java.class.path");
      StringTokenizer tokenizer=new StringTokenizer(classpath, File.pathSeparator);
      String home=null;
      while (!(home=tokenizer.nextToken()).endsWith(PATH_SUFFIX)) { // looking for a suffix
        if (!tokenizer.hasMoreTokens()) {
          System.err.println("!! Cannot find a classpath directory ending with '"+PATH_SUFFIX+"'");
          return;
        }
      }

      home=home.substring(0, home.length()-PATH_SUFFIX.length());
      System.out.println("catalina.home='"+home+''');
      System.setProperty("catalina.home", home);
    }
    org.apache.catalina.startup.Bootstrap.main(args);
  }
}

Set Tomcat.java as your main class. Upon execution, the class will try to calculate the Tomcat home directory, set the catalina.home property, and run Tomcat.

Good Luck!

By Alexey N. Solofnenko.

Server Response from: SC3