Josue
Josue

Reputation: 83

Is there a way to determine the Java EE version at runtime?

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

Upvotes: 8

Views: 13767

Answers (3)

Stephen C
Stephen C

Reputation: 718798

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.

Upvotes: 6

Paul Vargas
Paul Vargas

Reputation: 42020

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
} ...

Upvotes: 2

David Blevins
David Blevins

Reputation: 19368

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.

Upvotes: 6

Related Questions