8

Is there programmatic way to determine which version of the java ee environment is available?

user207421
  • 305,947
  • 44
  • 307
  • 483
Josue
  • 83
  • 1
  • 1
  • 4
  • 2
    You tagged Tomcat7. Just wanted to say, Tomcat7 isn't exactly a Java EE application server. It's "just" a JSP/Servlet container. Glassfish, JBoss AS and TomEE are examples of fullfledged Java EE application servers (JSP/Servlet/JSF/JPA/EJB/etc/etc). – BalusC Jan 20 '13 at 02:00
  • Understood. Mostly been using TomEE, but need to figure out is the right environment will be available during installation. – Josue Jan 20 '13 at 22:21

3 Answers3

6

There's no standard way to do that. The closest you could do is use reflection/ClassLoader and check for specific API classes/methods that were introduced in a given Java EE version.

Off the top of my head:

  • Java EE 6 / EJB 3.1 added javax.ejb.Singleton
  • Java EE 5 / EJB 3.0 added javax.ejb.Stateless
  • J2EE 1.4 / EJB 2.1 added javax.ejb.TimerService
  • J2EE 1.3 / EJB 2.0 added javax.ejb.MessageDrivenBean

Before that it's J2EE 1.2 / EJB 1.1

Though, note, if this is for Tomcat (judging by the tag), the best way is to just check System.getProperty("tomcat.version"). You should be able to imply the servlet version based on the Tomcat version.

David Blevins
  • 19,178
  • 3
  • 53
  • 68
6

I don't know if there is a way to get the Java EE version number, but you can get hold of the Servlet API version number and the JSP version number:

  • You can get the servlet api version in an implementation independent way from a ServletContext object. Look for the getMajorVersion() and getMinorVersion() methods.

  • You can get the JSP version as follows:

    JspFactory.getDefaultFactory().getEngineInfo().getSpecificationVersion()

And there are no doubt platform (i.e. appserver) specific ways to find or infer various version numbers.


It is worth noting that "Java EE version" is a rubbery concept if you take into account what appserver vendors might do; e.g. cherry-picking the Java EE technologies that they support, and possibly cherry-picking versions. For instance Tomcat does not support all of Java EE - EJB support is missing.

Stephen C
  • 698,415
  • 94
  • 811
  • 1,216
  • 2
    Note that `` in `web.xml` has influence in `ServletContext#getMajorVersion()` and `getMinorVersion()`. Theoretically, this can be set to 2.5 while the container itself supports 3.0. The container is then running in a fallback modus. This can if necessary be combined with checking if a Servlet 3.0 specific class (e.g. `@WebServlet` annotation) is in classpath or not, depending on why exactly the OP needs this information. – BalusC Jan 20 '13 at 02:01
  • Note cherry-picking is not allowed on any certified Java EE server. This was the main reason for creating [TomEE](http://tomee.apache.org) as plain Tomcat excludes [quite a bit more than EJB](http://stackoverflow.com/a/9199893/190816). – David Blevins Jan 20 '13 at 17:47
  • ServletContext has **four** relevant methods: `get[Effective]{Major|Minor}Version()'. See the JavaDoc for details. – Per Lindberg Apr 01 '19 at 12:16
2

If you are using Tomcat, you can get the Servlet API version of runtime using org.apache.catalina.core.Constants class. e.g.

if (Constants.MINOR_VERSION == 2 && Constants.MINOR_VERSION == 5) {
    // Servlet 2.5
} else if(Constants.MINOR_VERSION == 3 && Constants.MINOR_VERSION == 0) {
    // Servlet 3.0
} ...
Paul Vargas
  • 41,222
  • 15
  • 102
  • 148