Let’s start with this program: http://crunchify.com/how-to-run-java-program-automatically-on-tomcat-startup/
We will use the same Java Program and modify it to give you Tomcat Directory Path in Java and lot more.
package com.crunchify.tutorials; import javax.servlet.*; import javax.servlet.http.HttpServlet; /** * @author Crunchify.com */ @SuppressWarnings("serial") public class CrunchifyServletExample extends HttpServlet { public void init() throws ServletException { System.out.println("----------"); System.out.println("---------- CrunchifyExample Servlet Initialized successfully ----------"); System.out.println("----------"); System.out.println("\nApp Deployed Directory path: " + this.getServletContext().getRealPath("/")); System.out.println("getContextPath(): " + this.getServletContext().getContextPath()); System.out.println("Apache Tomcat Server: " + this.getServletContext().getServerInfo()); System.out.println("Servlet API version: " + this.getServletContext().getMajorVersion() + "." +this.getServletContext().getMinorVersion()); System.out.println("Tomcat Project Name: " + this.getServletContext().getServletContextName()); } }
Output:
---------- ---------- CrunchifyExample Servlet Initialized successfully ---------- ---------- App Deployed Directory path: /Users/<username>/Documents/eclipsewp/.metadata/.plugins/org.eclipse.wst.server.core/tmp0/wtpwebapps/CrunchifyTutorials/ getContextPath(): /CrunchifyTutorials Apache Tomcat Server: Apache Tomcat/7.0.40 Servlet API version: 3.0 Tomcat Project Name: CrunchifyTutorials
The post How to Get Tomcat Directory Path in Java appeared first on Crunchify.