Get Tomcat Version in Java: Programmatic Methods with Code Examples
I was working on a monitoring feature for our web application and needed to log the Tomcat server version at startup. This would help our support team quickly identify which Tomcat version a customer is running when they report issues. I expected this to be straightforward, but I discovered there are two different approaches depending on your use case.
The Problem
I needed to programmatically get the Tomcat version from within a running Java application. The version information had to be accessible without shell access or command-line tools, since many of our deployments are in containerized environments where we can’t easily run terminal commands.
My first thought was to look for a system property, but Tomcat doesn’t expose its version that way. After some research, I found two practical approaches.
Method 1: Using ServerInfo Class (Tomcat-Specific)
The org.apache.catalina.util.ServerInfo class provides Tomcat version information directly. This is the cleanest approach when you know you’re running on Tomcat:
import org.apache.catalina.util.ServerInfo;
public class VersionCheck { public static void main(String[] args) { String serverInfo = ServerInfo.getServerInfo(); System.out.println(serverInfo); // Output: Apache Tomcat/10.1.52 }}However, I ran into a ClassNotFoundException when I first tried this. The issue was that catalina.jar wasn’t on my classpath. This class is part of Tomcat’s internal API, so you need to add the JAR to your project’s dependencies.
For Maven projects, you can add it as a system-scoped dependency:
<dependency> <groupId>org.apache.tomcat</groupId> <artifactId>catalina</artifactId> <version>10.1.52</version> <scope>system</scope> <systemPath>${env.CATALINA_HOME}/lib/catalina.jar</systemPath></dependency>The ServerInfo class also provides additional details beyond just the version string:
import org.apache.catalina.util.ServerInfo;
public class ServerInfoDetails { public static void main(String[] args) { System.out.println("Server info: " + ServerInfo.getServerInfo()); System.out.println("Server built: " + ServerInfo.getServerBuilt()); System.out.println("Server number: " + ServerInfo.getServerNumber()); }}Method 2: Using ServletContext (Container-Agnostic)
If you’re writing a servlet or have access to a ServletContext, there’s a more portable approach:
import jakarta.servlet.http.HttpServlet;import jakarta.servlet.http.HttpServletRequest;import jakarta.servlet.http.HttpServletResponse;import java.io.IOException;
public class ServerInfoServlet extends HttpServlet { @Override protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws IOException { String serverInfo = getServletContext().getServerInfo(); resp.setContentType("text/plain"); resp.getWriter().write("Server: " + serverInfo); // Output: Server: Apache Tomcat/10.1.52 }}This method works with any servlet container (Tomcat, Jetty, WildFly, etc.), not just Tomcat. The trade-off is that it only works within a servlet context—you can’t call it from a standalone main() method.
Handling Edge Cases
In production code, you should handle potential issues gracefully:
public String getTomcatVersion() { try { String serverInfo = getServletContext().getServerInfo(); if (serverInfo != null && serverInfo.contains("Tomcat")) { return serverInfo; } return "Unknown server: " + serverInfo; } catch (Exception e) { return "Unable to determine server version"; }}The getServerInfo() method returns different formats depending on the container. For Tomcat, it’s “Apache Tomcat/X.Y.Z”. For Jetty, it might be “jetty/X.Y.Z”. Always check for null and handle unexpected formats.
Command-Line Alternative
For quick checks during development or debugging, you can also run the ServerInfo class directly from the command line:
$ java -cp /opt/tomcat/lib/catalina.jar org.apache.catalina.util.ServerInfoServer version: Apache Tomcat/10.1.52Server built: Jan 23 2026 19:29:07 UTCServer number: 10.1.52.0This is useful when you have shell access to the server and want to quickly verify the Tomcat version without writing code.
Which Approach to Use?
| Scenario | Recommended Method |
|---|---|
| Servlet code, portable across containers | ServletContext.getServerInfo() |
| Standalone Java app running on Tomcat | ServerInfo.getServerInfo() |
| Quick command-line check | Run ServerInfo from terminal |
| Need detailed build info | ServerInfo.getServerBuilt() + getServerNumber() |
Summary
For most web applications, ServletContext.getServerInfo() is the better choice because it’s portable and works without additional dependencies. Use ServerInfo.getServerInfo() when you need Tomcat-specific details or when you’re working outside a servlet context.
Final Words + More Resources
My intention with this article was to help others share my knowledge and experience. If you want to contact me, you can contact by email: Email me
Here are also the most important links from this article along with some further resources that will help you in this scope:
Oh, and if you found these resources useful, don’t forget to support me by starring the repo on GitHub!
Comments