Silver Tabby Cat Lying on Brown Wooden Surface
Very loosely related image from to make this post look nicer in social media shares.

A while ago I mentioned that every Java project has to have a main method somewhere. I started thinking it would be interesting to dig into a larger project and show how even a whole servlet container starts with a main method, just like the smallest hello world program. So let’s poke at Tomcat!

Why tomcat? Because it’s open source, I’ve used it before, and honestly it was the first thing that came to mind :) Oh, and for anyone reading this who hasn’t spent a ton of time writing Java webapps, a servlet container is a kind of webserver that provides a nice little home for your servlets, which are little Java programs that take http requests and give back http responses.

Tomcat does indeed have a main method, it starts on line 457 in There’s even a helpful comment that says “Main method and entry point when starting Tomcat via the provided scripts.”

Which is all well and good, but that makes for a pretty boring blog post :) So let’s talk about how I hunted down that main method. I could have just searched for “static void main” in the Tomcat github repo, but a) I just tried that and it wasn’t actually super helpful, and b) what I really wanted to know was what happens when you start Tomcat.

It’s been a while since I used Tomcat so I looked at the docs first. In the readme on the github project there’s a link to RUNNING.txt, which looks promising.

So I skim through that (there’s a bunch of stuff about installing tomcat and setting up environment variables that I don’t care about right now, I just want to know what the startup script is called) and eventually find the section about starting tomcat. In there it says that the startup script for linux users like me is $CATALINA_HOME/bin/ (there’s a .bat file for windows users too if you’re curious).

Okay great what’s in (also thankyou github for having a file finder!) It turns out there’s not a ton in there and I don’t understand most of it, but at the end of that file is “exec $PRGDIR/$EXECUTABLE start $@“”, which I think I can assume executes a thing :) Also a little earlier in the file is

Given that, it looks like does some pre-setup setup and then actually starts the server (if there’s a 3rd level of startup scripts I’m going to be really sad). There’s a lot more going on in that I skimmed through, but I eventually started seeing a lot of eval exec \”$_RUNJAVA\”  [many more vars] org.apache.catalina.startup.Bootstrap $@ start. That $_RUNJAVA thing sounds like it might, you know, run Java :) That probably means that the startup class is org.apache.catalina.startup.Bootstrap, let’s go see if there’s anything interesting in there. And what do you know there’s a main method. It took some scrolling to get there but we made it eventually!

You may or may not care about how Tomcat (or any other given application) works, but what I hope is interesting is that everything an application does is understandable. Nothing the computer does is magic, it’s all knowable.