5

How do I get the Tomcat/Catalina version number in JAVA?

I've seen lots of how to do it via command line etc. but that's not code I can use, I also cannot use catalina.path as the version number has been stripped from the path.

Please also note I want to use the version in code, so the various JSP solutions that I've looked at also do not work for me.

Thanks

Sean
  • 271
  • 1
  • 8
  • 20
  • http://www.bestdesigns.co.in/blog/check-jsp-tomcat-version – MihaiC Dec 02 '14 at 08:30
  • and here is your answer :http://www.indianwebportal.com/check-jsp-tomcat-version – ppuskar Dec 02 '14 at 08:36
  • Apologies but doesn't actually answer my question...how do I do it in Java. I do not want to access a page. I need this in code because other code segments are dependent on whether version 6 or version 7 is being used. – Sean Dec 02 '14 at 08:43

2 Answers2

16

From a JSP

In a jsp file you can print out the version like this:

Tomcat Version : <%= application.getServerInfo() %>

Output:

Tomcat Version : Apache Tomcat/8.0.14

Outside of JSP (Any Java Code)

If you want it outside of a JSP (e.g. in a Servlet or listener or w/e) take a look at the org.apache.catalina.util.ServerInfo class, it has some nice static methods:

System.out.println(ServerInfo.getServerBuilt());
System.out.println(ServerInfo.getServerInfo());
System.out.println(ServerInfo.getServerNumber());

Output:

Sep 24 2014 09:01:51
Apache Tomcat/8.0.14
8.0.14.0

So what you really want is ServerInfo.getServerNumber().

Note:

The ServerInfo class also has a main() method so it can be run as a standalone application too:

java -cp lib/catalina.jar org.apache.catalina.util.ServerInfo

Example output:

Server version: Apache Tomcat/8.0.14
Server built:   Sep 24 2014 09:01:51
Server number:  8.0.14.0
OS Name:        Windows 7
OS Version:     6.1
Architecture:   x86
JVM Version:    1.7.0_55-b13
JVM Vendor:     Oracle Corporation
icza
  • 389,944
  • 63
  • 907
  • 827
  • he can just run java -cp catalina.jar org.apache.catalina.util.ServerInfo – MihaiC Dec 02 '14 at 08:40
  • @MihaiC mr. Sean said's that "I've seen lots of how to do it via command line etc. but that's not code I can use," so he need something that he can use. e.g. for verifying the server something like that :) – Secondo Dec 02 '14 at 08:43
  • @Secondo Sean said just now : "Apologies but doesn't actually answer my question...how do I do it in Java. I do not want to access a page. I need this in code because other code segments are dependent on whether version 6 or version 7 is being used." – MihaiC Dec 02 '14 at 08:56
  • @MihaiC I just read that comment, sorry dude. – Secondo Dec 02 '14 at 08:58
  • 1
    Also found the official javadoc for [ServerInfo](http://tomcat.apache.org/tomcat-8.0-doc/api/org/apache/catalina/util/ServerInfo.html). – icza Dec 02 '14 at 09:00
1

Try to put this in your JSP <%= application.getServerInfo() %> Look at this

Community
  • 1
  • 1
Secondo
  • 451
  • 4
  • 9