File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Servlets and the fly likes accessing a .properties file Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of OCA/OCP Java SE 7 Programmer I & II Study Guide this week in the OCPJP forum!
JavaRanch » Java Forums » Java » Servlets
Bookmark "accessing a .properties file " Watch "accessing a .properties file " New topic
Author

accessing a .properties file

Nikki Freeman
Greenhorn

Joined: Nov 03, 2005
Posts: 10
I have an action class\servlet that references a .properties file. I am running the app on a websphere server off of my local pc.

The only way I can get the server to see the properties file is to give it a full path ie:
System.setProperty("OSS_CLIENT_CONFIGURATION","C:\\Documents and Settings\\IBM\\rationalsdp6.0\\APworkspace\\APElapsedTime\\WebContent\\WEB-INF\\source\\com\\corp\\elapsedtimeosds\\resources\\OSSclient.properties");

I tried this but got a could not find error:
System.setProperty("OSS_CLIENT_CONFIGURATION","..\\WebContent\\WEB-INF\\source\\com\\corp\\elapsedtimeosds\\resources\\OSSclient.properties");

Obviously I can't use a local path to the properties file when I deploy the app. Any suggestions on how to setup a properties file so that a servlet can read it off of the server?
Nikki Freeman
Greenhorn

Joined: Nov 03, 2005
Posts: 10
actually looking around on the forums, I found this:

http://jaitechwriteups.blogspot.com/2007/01/how-to-read-properties-file-in-web.html

I haven't tried it yet, but I don't see why this would not work?
Ben Souther
Sheriff

Joined: Dec 11, 2004
Posts: 13410

Tomcat's documentation lists a couple ways:
http://wiki.apache.org/tomcat/HowTo#head-45c3314139cb900ddd43dde2ff671532e6e844bc

I'm not familiar enough with Websphere to know if there would be a problem with either of these techniques.


Java API J2EE API Servlet Spec JSP Spec How to ask a question... Simple Servlet Examples jsonf
Nikki Freeman
Greenhorn

Joined: Nov 03, 2005
Posts: 10
I thought that would have worked. The problem is I need the properties inside the properties file set as a system property. This code doesn't work - NullPointException :



Thanks for the response
David O'Meara
Rancher

Joined: Mar 06, 2001
Posts: 13459

Are you going to tell us where the NPE is being thrown?

I'm guessing this needs a leading slash: config/cargo.properties
Nikki Freeman
Greenhorn

Joined: Nov 03, 2005
Posts: 10
Thanks David,

Looks like the properties are being loaded. I need to set the properties in the file as system properties. Here's what I have done so far:

Ben Souther
Sheriff

Joined: Dec 11, 2004
Posts: 13410

Is it still a NullPointerException or is it throwing a SecurityException?
David O'Meara
Rancher

Joined: Mar 06, 2001
Posts: 13459

Firstly, if you get a reference to the System properties and update them, then the System properties should be updated. You shouldn't need to write them back.

Starting from first principles though, should you be doing this via a properties file, or should you be setting the System properties on the command line using the JVM -D arguments?

Thirdly, you could read the file from into a new properties instance, then iterate the values and use System.setProperty(...)
Nikki Freeman
Greenhorn

Joined: Nov 03, 2005
Posts: 10
Thanks David. As always, you are correct on all of those points.

The last problem I am having is, I have an object that needs a properties file setup as a System property. For example,
System.setProperty("CLIENT_CONFIGURATION","C:\\OSSclient.properties");
This works fine, but obviously isn't portable. I have this properties file in the WebContent directory but I get a file not found error when the object referecnces the property. I tried something like this:
System.setProperty("CLIENT_CONFIGURATION","OSSclient.properties");
but that didn't work. Any ideas?

Also, I am running this off of a WebSphere server, but I don't think that should matter too much.
David O'Meara
Rancher

Joined: Mar 06, 2001
Posts: 13459

The first is portable if you add -DCLIENT_CONFIGURATION=c:\\OSSclient.properties to the command line starting WebSphere rather than including it in the code
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: accessing a .properties file