Getting tomcat's installation directory using

2019-06-04 20:12发布

问题:

I want to get tomcat's installation directory in my computer using java. I tried using :

System.getProperty("catalina.base");

and

System.getProperty("catalina.home");

But both methods return null as the answer. I tried it with System.getProperty("java.home"); and it correctly returns the java path. Any ideas as to what the problem is? Thanks

回答1:

Try installing this JSP and passing various values for the "property" parameter:

<%
  String propertyName = request.getParameter("property");
  Object propertyValue;
  String typeString;
  if(null == propertyName)
    propertyValue = null;
  else
    propertyValue = System.getProperty(propertyName);

  if(null == propertyValue)
    typeString = "null";
  else
    typeString = propertyValue.getClass().getName();
%>
The system property <code><%= propertyName %></code> has the value:
<code><%= propertyValue %></code> (<%= typeString %>).

Maybe you can find a pattern to which property values return null.



回答2:

I also encountered the same error as you did. but then I realized that the tomcat server was not running. After starting the server, I still failed to get the tomcat path.
Then I realized that I was trying to get the path from the main method in a JSF project while tomcat was still running.
So i finally got the tomcat path by using: System.out.println(System.getProperty("catalina.base")); in a @PostConstruct method of one of my @ViewScoped beans in a JSF project. And it successfully displayed C:\Documents and Settings\S!LENT W@RRIOR\Application Data\NetBeans\7.2.1\apache-tomcat-7.0.27.0_base in the console in NetBeans.

So In order to get Tomcat's installation directory, following points should be kept in mind:

  • Tomcat Server is running
  • You're not trying to get path from main method

hope this helps



回答3:

we are using cucumber for testing and in cucumber cases, we are accessing project's individual method. in project, we are reading configuration for xml and the location is in config folder of Apache tomcat. Now when i run project,System.out.println(System.getProperty("catalina.base")); it's provide the proper path however when i call project's method from cucumber then it's returning null