Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

How do I Locate an application properties file

0 views
Skip to first unread message

MattC

unread,
Sep 16, 2005, 5:24:48 PM9/16/05
to
I am working in a Struts web based J2EE environment. My application is
packaged as a web app.

I have application level configuration information that needs to be
kept in a properties file. My problem is how to programmatically
determine the absolute pathname of that properties file.

It seems to me I have two basic options.

1.) I can create the properties file and put it in a "known" location.
For instance the WEB-INF\classes directory. In order to do this I will
need to determine the absolute path name to the WEB-INF directory. For
instance it might be C:\myproject\public_html\WEB-INF. How can I
determine the absolute pathname of the WEB-INF directory?

2.) I can store a relative pathname of the properties file in the
web.xml file. This is a more flexible implementation and so it is
preferred over option #1. In order to do this I need to determine the
physical location of the WEB-INF directory as in option #1. In addition
I need to gain access to the web.xml file.

Can someone tell me how to access the web.xml file from a Struts
application? In other words, I don't have access to the servlet.init()
method and its associated ServletConfig object.

Thanks,
Matt

Malte

unread,
Sep 16, 2005, 6:03:09 PM9/16/05
to

Below is an outlined example. The idea is to use the webapp.xml file.
Put in a context-param tag, then in your Java code, use the servlet
context to retrieve the information. If you, like I did for the example,
stuff your properties file into /WEB-INF/data, getRealPath() will find
it for you nicely. What you don't want to do is hardcode c:\ or
something ugly. What if someone deploys your application to a real OS
when it would become something line /opt/mywebapps ?

<web-app>

<context-param>
<param-name>DATA_DIR</param-name>
<param-value>/WEB-INF/data</param-value>
</context-param>
...
</web-app>

private File getDataDir() throws ServletException {
ServletContext application = getServletContext();
String dataDir = application.getInitParameter(DATA_DIR_PARAM);

if (dataDir == null || dataDir.length() == 0)
error(FILE_COMP_ID, MISSING_DATA_DIR_PARAM_ERROR,
FacesMessage.SEVERITY_FATAL);

String realDir = application.getRealPath(dataDir);
if (realDir == null)
error(FILE_COMP_ID, CANNOT_ACCESS_DATA_DIR_ERROR,
FacesMessage.SEVERITY_FATAL);

return new File(realDir);
}

Roedy Green

unread,
Sep 16, 2005, 9:37:50 PM9/16/05
to
On 16 Sep 2005 14:24:48 -0700, "MattC" <matthew....@lmco.com>
wrote or quoted :

>I have application level configuration information that needs to be
>kept in a properties file. My problem is how to programmatically
>determine the absolute pathname of that properties file.

Frustrating eh? especially if you want this to work muliplatform.

Look into the Preferences API for getting around that problem.
The other way is to use JAWS.

see http://mindprod.com/jgloss/preferences.html
http://mindprod.com/jgloss/avawebstart.html

--
Canadian Mind Products, Roedy Green.
http://mindprod.com Again taking new Java programming contracts.

Raymond DeCampo

unread,
Sep 17, 2005, 6:20:23 PM9/17/05
to
MattC wrote:
> I am working in a Struts web based J2EE environment. My application is
> packaged as a web app.
>
> I have application level configuration information that needs to be
> kept in a properties file. My problem is how to programmatically
> determine the absolute pathname of that properties file.

Why do you need the absolute pathname? The simplest solution is to put
the .properties file in the classpath and load it via ResourceBundle.
If you do not want a ResourceBundle you can use
ClassLoader.getResource() to get the URL.

>
> It seems to me I have two basic options.
>
> 1.) I can create the properties file and put it in a "known" location.
> For instance the WEB-INF\classes directory. In order to do this I will
> need to determine the absolute path name to the WEB-INF directory. For
> instance it might be C:\myproject\public_html\WEB-INF. How can I
> determine the absolute pathname of the WEB-INF directory?

ServletContext or ServletConfig has a getRealPath() method that can help
you. However, there is no real need to do it this way.

>
> 2.) I can store a relative pathname of the properties file in the
> web.xml file. This is a more flexible implementation and so it is
> preferred over option #1. In order to do this I need to determine the
> physical location of the WEB-INF directory as in option #1. In addition
> I need to gain access to the web.xml file.
>
> Can someone tell me how to access the web.xml file from a Struts
> application? In other words, I don't have access to the servlet.init()
> method and its associated ServletConfig object.
>

Look into just putting it in the classpath. It's the easiest way and it
works in every environment (web container, EJB, applet, etc.).


HTH,
Ray

--
XML is the programmer's duct tape.

Roedy Green

unread,
Sep 17, 2005, 7:46:25 PM9/17/05
to
On Sat, 17 Sep 2005 22:20:23 GMT, Raymond DeCampo
<nos...@twcny.rr.com> wrote or quoted :

>Why do you need the absolute pathname? The simplest solution is to put
>the .properties file in the classpath and load it via ResourceBundle.
>If you do not want a ResourceBundle you can use
>ClassLoader.getResource() to get the URL.

If it is a read-only properties file, you can put it in the jar. The
problem only comes when you have to create a properties file out of
thin air and it is not clear where a safe place to put it is.

0 new messages