By default, appbase is configured to point to TomEE's webapps folder:
<Host name="localhost" appBase="webapps" unpackWARs="true" autoDeploy="true">
I would like to find the location of "appBase" programmatically because it points to different places on our production server and our development boxes (and because it'd just be cleaner that way).
I've researched a few options, and the one that seems to work for others is calling ServletContext().getRealPath("/")
. For me this takes me to a temporary folder, $CATALINA_HOME/temp/[#-AppName]
, while by default I was looking for $CATALINA_HOME/webapps
.
I found the answer working off of what someone else wrote on this question.
From Tomcat's Host class you can find the appbase:
MBeanServer mBeanServer = MBeanServerFactory.findMBeanServer(null).get(0);
ObjectName name = new ObjectName("Catalina", "type", "Server");
Server server = (Server) mBeanServer.getAttribute(name, "managedResource");
Service service = server.findService("Catalina");
Engine engine = (Engine) service.getContainer();
Host host = (Host) engine.findChild(engine.getDefaultHost());
host.getAppBase(); //Got it.