Reputation: 83
Is there programmatic way to determine which version of the java ee environment is available?
Upvotes: 8
Views: 13767
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
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
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:
javax.ejb.Singleton
javax.ejb.Stateless
javax.ejb.TimerService
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