1

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

mkab
  • 933
  • 4
  • 16
  • 31
  • Are you running under a SecurityManager? – Christopher Schultz May 22 '12 at 19:36
  • @ChristopherSchultz: ah yes. I configured conf/tomcat-users.xml. Is that the problem? Is it that it doesn't allow calls to System? – mkab May 22 '12 at 19:42
  • 1
    `catalina.base` and `catalina.home` must be set as an environment variable before you can access them. – devsnd May 22 '12 at 19:52
  • 1
    @twall: so if they aren't set there is no way to get tomcat's installation directory? – mkab May 22 '12 at 20:03
  • @mkab If you are under a SecurityManager (not just using tomcat-users.xml which is just user authentication) then the JVM prohibits access to certain things. IIRC, you'd get a SecurityException when trying to access a system property if that is not allowed instead of NULL. – Christopher Schultz May 23 '12 at 19:30
  • What exact version of Tomcat are you using? – Christopher Schultz May 23 '12 at 19:30
  • @ChristopherSchultz: Tomcat 7.0 (in Linux). Ok, then if it's that I don't really understand what you mean by SecurityManager. Though I don't think i'm under it since i'm getting NULL values. – mkab May 23 '12 at 21:18
  • Weird that you are getting null. When I launch Tomcat, the command-line (I can see it in 'ps') looks like this: "/usr/bin/java [....] -Dcatalina.base=/path/to/catalina/base -Dcatalina.home=/path/to/catalina/home [...] org.apache.catalina.startup.Bootstrap start". What does yours look like? (Or you can try "jinfo -sysprops [pid]"). – Christopher Schultz May 23 '12 at 21:52
  • @ChristopherSchultz: When i launch Tomcat, I have: `Using CATALINA_BASE: /usr/local/tomcat7 Using CATALINA_HOME: /usr/local/tomcat7 Using CATALINA_TMPDIR: /usr/local/tomcat7/temp Using JRE_HOME: /usr/local/jdk1.7.0_04/jre Using CLASSPATH: /usr/local/tomcat7/bin/bootstrap.jar:/usr/local/tomcat7/bin/tomcat-juli.jar` – mkab May 23 '12 at 21:59
  • @ChristopherSchultz: `ps` gives me: `/usr/local/jdk1.7.0_04/jre/bin/java -Djava.util.logging.config.file=/usr/local/tomcat7/conf/logging.properties -Djava.util.logging.manager=org.apache.juli.ClassLoaderLogManager -Djava.endorsed.dirs=/usr/local/tomcat7/endorsed -classpath /usr/local/tomcat7/bin/bootstrap.jar:/usr/local/tomcat7/bin/tomcat-juli.jar -Dcatalina.base=/usr/local/tomcat7 -Dcatalina.home=/usr/local/tomcat7 -Djava.io.tmpdir=/usr/local/tomcat7/temp org.apache.catalina.startup.Bootstrap start` – mkab May 23 '12 at 22:05
  • @ChristopherSchultz: `jinfo -sysprops` gives me: `and `jinfo -sysprops` gives me: `Error attaching to process: sun.jvm.hotspot.debugger.DebuggerException: Can't attach to the process` – mkab May 23 '12 at 22:11
  • If you get an error, you might not be running as the same user as the Tomcat process. Can you try that? Or, just use 'ps' and make sure you can see the whole command line (it's the 'w' option on GNU/Linux -- you may have to poke around the documentation for your platform). – Christopher Schultz May 24 '12 at 13:50
  • @ChristopherSchultz: yes you're right. I'm not running as the same user as the Tomcat process. `root` is running Tomcat. Do I have to change it to my `$USERNAME`? If so, how? – mkab May 24 '12 at 17:18
  • @ChristopherSchultz: `sudo jinfo -sysprops` gives me: `Exception in thread "main" java.lang.reflect.InvocationTargetException at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57) at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) at java.lang.reflect.Method.invoke(Method.java:601) at sun.tools.jinfo.JInfo.runTool(JInfo.java:97) at sun.tools.jinfo.JInfo.main(JInfo.java:71) Caused by: java.lang.RuntimeException: No type named "GrowableArray" etc...` – mkab May 24 '12 at 17:22
  • @mkab Hmm... maybe you are running a different JVM version between Tomcat and jinfo? Just try to look at a full process listing: you may have better luck. The initial command-line that launched the process should contain those system properties. – Christopher Schultz May 24 '12 at 21:15
  • @ChristopherSchultz: Actually jinfo wasn't installed in my pc and it suggested to apt-get install open-jdk-7 which i did. Stupid me. i didn't even think that it would be in the bin folder of my java folder. Ok i tried jinfo again and it gave me a whole lot of system properties (it's just the equivalent of `System.getProperties()` in java). And lo and behold `catalina.base` is defined in there. It's weird that `System.getProperty("catalina.base");` returns null. – mkab May 25 '12 at 09:23
  • _Nothing is null_, if you using the [Tomcat-API ServletContext::getRealPath](https://tomcat.apache.org/tomcat-5.5-doc/servletapi/javax/servlet/ServletContext.html#getRealPath) `String webServerPath = request.getSession().getServletContext().getRealPath( File.separator ) + File.separator + ".." /* webapps */ + File.separator + ".." /* myWebApp */ ; /* while running under windows */ String myCatalinaBase = webServerPath.replace( '\\', '/' );` – udoline Jul 09 '19 at 15:18

3 Answers3

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.

Christopher Schultz
  • 20,221
  • 9
  • 60
  • 77
  • 1
    Thanks for the code. I don't know much about jsp though. I've never used it. Nevertheless I tried to test the code and it gives be null values throughout for all the system properties I tried. So I changed the code a little bit. I removed the request.getParameter and everything that has connection with it and used System.getProperty directly. Well it sent me correct values for both `catalina.home` and `catalina.base`. It's funny why it returns null values in java. I'm using eclipse by the way. Might be the cause. – mkab May 26 '12 at 12:12
  • Oh! If you are using Eclipse, then that's probably the problem: Eclipse launches Tomcat in a different way from the standard scripts that Tomcat bundles with its distributions. It's possible that these environment variables are not set at all from Eclipse. If you launch Tomcat from the (provided) scripts, do you get the values you are expecting? – Christopher Schultz May 27 '12 at 15:27
  • No that's not what i meant. I'm not using Eclipse to launch Tomcat. I'm using eclipse just to program in java. I read that eclipse comes with it's own built-in jdk and doesn't use that of the system...so I thought that might be the problem. Anyway I launch Tomcat normally (startup.sh in the bin folder). I tried compiling the java file using the terminal `javac` then `java` but I still get null values. – mkab May 27 '12 at 18:06
  • The only thing I can think of is that your "java" code is not running within Tomcat even though you think it is. I don't know what your setup is, but its easy to get confused with all those moving parts. Maybe you can post your servlet code and I can take a look. – Christopher Schultz May 29 '12 at 14:37
  • What do you mean by my servlet code? Actually i'm writing a for my school project a GUI in java that would change some files in the `$CATALINA_HOME/webapps/mondrian/WEB-INF` directly. So far so good, the the user just enters his/her Tomcat installation directory in a JTextField and I continue from there. Now what I want to do is to make it detect this $CATALINA_HOME directly. – mkab May 31 '12 at 18:21
  • So you have a GUI app that runs entirely separately from Tomcat and you want it to auto-detect the catalina.base system property in another JVM? That's just not going to happen. If you have a system-side environment variable (say, CATALINA_BASE), then you can probably get that from the OS environment but there's no guarantee that the env var has been set for your GUI process. – Christopher Schultz Jun 03 '12 at 16:08
  • I see. I tried defining my variables in the `/etc/environment` file and using `System.getenv` in java I could get Tomcat's installation directory. So now I'm trying to do something general. For the system-side variable, what of if the person doesn't have CATALINA_BASE defined in his/her pc's environmental variables? How will I go about it? Should I document it in the user manual for example? – mkab Jun 04 '12 at 17:52
  • This has obviously strayed from your original question :) I think that you should require that the user of the GUI specify the value for Tomcat's installation directory in some way, whether that be on the command-line using CLI arguments, via a system property, or via an environment variable (or accept any of those with a clear order of preference) or even in a config file. If the setting is not found, show an error and refuse to continue. – Christopher Schultz Jun 05 '12 at 14:51
  • Yeah it has :). Actually this situation is my real question.Can't believe it took 12 days. But without it I wouldn't have discovered some stuffs that I discovered here (though i'd have discovered it some other way). I'll follow what you wrote. Thanks for your help! I appreciate. – mkab Jun 06 '12 at 12:35
1

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

S1LENT WARRIOR
  • 11,704
  • 4
  • 46
  • 60
0

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

mayank agrawal
  • 628
  • 5
  • 20