Search code examples
javajakarta-eeapplication-server

Determine that application is running under application server


Some code may be reused in various environments including Java EE Application server. Sometimes it is nice to know whether the code is running under application server and which application server is it.

I prefer to do it by checking some system property typical for the application server. For example it may be

  • jboss.server.name for JBoss
  • catalina.base for Tomcat

Does somebody know appropriate property name for other servers? Weblogic, Websphere, Oracle IAS, others?

It is very easy to check if you have the specific application server installed. Just add line System.getProperties() to any JSP, Servlet, EJB and print the result.

I can do it myself but it will take a lot of time to install server and make it working.

I have read this discussion: How to determine type of Application Server an application is running on?

But I prefer to use system property. It is easier and absolutely portable solution. The code does not depend on any other API like Servlet, EJBContext or JMX.


Solution

  • This is not a 'standard' way but what I did was to try to load a Class of the AppServer.

    For WAS:

    try{  
    Class cl = Thread.getContextClassLoader().loadClass("com.ibm.websphere.runtime.ServerName");
    
    // found
    
    }  
    // not Found  
    catch(Throwable)
    {
    
    }
    
    // For Tomcat: "org.apache.catalina.xxx"
    

    Etc.

    Let me know what you think