Get installation path of web application from class file

Can anybody tell me how I can get the path on disk to the root of my web application from a class file. Im running under Tomcat 5.5 and I have 3 copies of the same webapp under 3 differenet directories:

C:\xxx\dev\yyy

C:\xxx\test\yyy

C:\xxx\prod\yyy

Based on which directory the web-app, I need to do a lookup on a properties file to pull the right jdbc connection to connect to the appropriate database. So it would be similar to using getServletContext().getRealPath("/") from a .jsp file.

Thanks,

Jason

[548 byte] By [jplusma] at [2007-11-26 22:58:40]
# 1
Is this sufficient?System.getProperty("user.dir");
BalusCa at 2007-7-10 12:24:44 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 2

I tried that but unfortanetly it returns the path to the Tomcat installation :( and the webapps are under a different directory with their appbase specified in Tomcat's server.xml file. So I guess if I could somehow pull each webapps appbase definition from Tomcat's server.xml, that would work too.

Thanks,

Jason

Message was edited by:

jplusm

Message was edited by:

jplusm

jplusma at 2007-7-10 12:24:44 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 3
It would be better to just put the properties file in the application's classpath (for example in the WEB-INF/classes directory) and read it like this:InputStream is = this.getClass().getResourceAsStream("/jdbc.properties");
DrClapa at 2007-7-10 12:24:44 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 4
Well its not that I can't access the properties file its i need to know what resource to pull from it based on where the application is installed.Thanks,Jason
jplusma at 2007-7-10 12:24:44 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 5

Sorry, obviously I'm not understanding the word "pull" there. I just gave you code so you could read from the properties file that's inside the application where the code is running. Don't know about pulling it, you'll have to explain what that means.

Edit: Okay, it looks like maybe you don't know the name of the resource file. Is that it?

Message was edited by:

DrClap

DrClapa at 2007-7-10 12:24:44 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 6

Sorry for not clarifying. I have a properties file with name/value pairs. I know where this properties file is and I can do a lookup in it. In this file, I have jdbc connection names which point to corresponding jdbc resources in my context.xml file.

jdbcDevDatabase=jdbc/tfmDev

jdbcTestDatabase=jdbc/tfmTest

jdbcProdDatabase=jdbc/tfmProd

These connections are for dev, test and prod databases. Now based on where the web-app is installed on disk, i.e-dev dir, test dir, prod dir, I want to retrieve the corresponding connection. The problem is I don't know how to get a handle to the web apps real path on disk without access to the request object or using getServletContext().getRealPath("/") which are not available to me in a class file.

Thanks,

Jason

jplusma at 2007-7-10 12:24:44 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...