Running Tomcat from JBuilder IDE

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


Last updated: December 25th, 2001

This article was published by Borland. The editors changed the article a lot, they made the language better, but they deleted one important trick - placing runtime directory within repository structure.


The life is too complex, so it is our task to make it as simple as possible. One of the problems I encountered working on several projects - it was not easy to build them! Some projects required special drive mappings, environment variables, third-party libraries. So in the first project I managed I decided that a programmer should be able to compile and run the sources just after they are checked out. Almost... We still needed JBuilder and Visual C++ installed. The problem was the absolute paths required to compile/run our projects. We dealt with libraries by putting them into CVS. (See why it is a good idea) JBuilder helped us by providing local (project) library definitions. One of the challenges was configuring Tomcat.

So we imported the whole Tomcat into our repository and recreated its minimal required folder structure in runtime folder (also within the repository), where we placed Tomcat configuration, our web application configuration, images, pages, JSPs. The trick was that JBuilder took Tomcat Jars from one place, but Tomcat executed in another, prepared by us. That way we were able to configure several independent servers (we even used two Tomcat versions 3.3 and 4.0b).

Here is an example how to do that. The provided class is working with any JBuilder 3.5+ and it should work with other IDEs too.

Sample repository structure:
...
ProductDevelopment - development home. Put main makefile or ANT's build.xml here.
   Java - all JBuilder projects are here
      src - Java sources
   C++
   Runtime
      ThirdParty - Third party libraries, products (Tomcat, Xerces, ...)
      WebProduct1
         conf - Tomcat configuration
         webapps - Web Applications directory
...

Good Luck!


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);
  }
}


Tomcat.java
Created with JBuilder

© 2001 Alexey N. Solofnenko