Generating an executable for Tomcat

In this tutorial, we are going to generate an executable for Tomcat. Tomcat is an open-source reference servlet container developed by the Apache Group. It is widely used and is chosen as an example to show how easy it is to generate a executable for a non-trivial Java application.

Since Tomcat is rather large and is constantly being improved, we chose not to bundle it with NativeJ. Instead, you can download the latest source/binary builds from http://jakarta.apache.org/tomcat/index.html. This example applies to Tomcat 4.1.x, though it can be easily adopted for other versions of Tomcat.

The downloaded binary distribution should be in the form tomcat-4.1.x.zip. Unzip it into the examples\ subdirectory. The base directory is jakata-tomcat-4.1.x\. For simplicity, rename it to tomcat\. When you are done, the directory structure should look like this:

C:\
  Program Files\
    NativeJ\
      executables\
        tomcat\

Now let's test if Tomcat is working properly.

> cd C:\Program Files\NativeJ\executables\tomcat
> set JAVA_HOME=C:\JDK
> set CATALINA_HOME=C:\Program Files\NativeJ\executables\tomcat
> bin\startup.bat

If all goes well, a separate command window will pop up:

You should now be able to access the Tomcat homepage at http://localhost:8080/.

Use bin/shutdown.bat to stop Tomcat.

Now, launch NativeJ, and populate the new project with the following parameters:

Name Value Remarks
Main
Application type
Console
The executable generated will run as a console program.
Allow as service
Yes
The executable generated will run as a Win32 service.
Executable icon
tomcat.ico
The application icon.
Executable filename
tomcat.exe
The filename of the generated executable.
Java Runtime
Classpath
bin;
server\lib;
common\lib;
%JAVA_HOME%\lib\tools.jar
This contains required .jar files. In this case, only the directories containing the required .jar files are specified. The executable is smarted enough to dynamically generate the classpath at runtime which includes all the .jar files in these directories.
Application
Main application class
org.apache.catalina.startup.Bootstrap
The name of the Java class we wish to run. This class should have a main() method.
Service
Service name
Tomcat
The service name which will appear in Services when installed.
Install command
‑install
Command used to install the service. By default, this is ‑install.
Uninstall command
‑uninstall
Command used to uninstall the service. By default, this is ‑uninstall.
Start argument(s)
start
The argument(s) to pass to the main() method when starting the program.
Stop argument(s)
stop
The argument(s) to pass to the main() method when stopping the program.

Now, click on File... Generate... to generate the executable. If everything goes well, you will receive a notification that tomcat.exe has been generated.

The Tomcat executable can function both as a console program, as well as a Win32 service.

To start Tomcat from the command line, type:

C:\Program Files\NativeJ\executables\tomcat> tomcat start

To stop Tomcat from the command line, type:

C:\Program Files\NativeJ\executables\tomcat> tomcat stop

To install Tomcat as a service, type:

C:\Program Files\NativeJ\executables> tomcat ‑install
Service installed: Tomcat

To uninstall the Tomcat service, type:

C:\Program Files\NativeJ\executables> tomcat ‑uninstall
Service uninstalled: Tomcat

Note that to execute JSP scripts, you must either have the JDK installed on the target system, or you need to copy the \jdk\lib\tools.jar file to lib\ext in the JRE installation directory. That's because by default the JRE does not contain the compiler class com.sun.tools.javac.Main. But executing JSP scripts require the Java compiler class to be present, so you will need to copy the tools.jar file to the JRE lib\ext subdirectory, which is reserved for extension libraries.