James Hutchinson
James Hutchinson

Reputation: 881

Reading properties file from server location

How do I read a file from a location on my server?

I can successfully read from /WEB_INF/classes using:

InputStream systemParamInputStream = getClass().getClassLoader().getResourceAsStream("ldap.properties");

However, I would like to have this file somewhere on the server, so that it can be configured by the support folks when the application goes live in production.

My liberty profile server.xml lives here: C:\eclipse\runtime\usr\servers\tmpServer. This would be fine, as indeed would any other location around there.

Upvotes: 1

Views: 5766

Answers (3)

xverges
xverges

Reputation: 4718

This thread shows different approaches for doing it.

The one I have successfully tried is to specify a folder as a library in server.xml, so that it will be available in the classpath:

<library id="configResources">
    <folder="${server.config.dir}/config" />
</library>

<application location="foo.war">
    <classloader privateLibraryRef="configResources" />
</application>

Two minor warnings regarding this approach:

  1. Requires 8.5.5
  2. May result in a server.xml XML validation error in Eclipse due to the use of the folder element (cvc-complex-type.2.4.a: Invalid content was found starting with element 'folder'. One of '{fileset}' is expected.). I gave a try to use a fileset instead but could not get it to work.

Upvotes: 1

Cristian Sevescu
Cristian Sevescu

Reputation: 1489

"WEB-INF/classes" is part of your classpath.
You can continue to read files as you do, by adding another folder to the classpath of the server. In that folder you would have the configuration files.

But you can also access the file directly like this:

InputStream systemParamInputStream=new FileInputStream(filePath);

The trick would be to find a clean way of configuring filePath of the configuration file as hardcoding is not nice. This is an option:

new FileInputStream(System.getProperty("filePath","C:\\eclipse\\runtime\\usr\\servers\\tmpServer"));

And you would send filePath as program parameter -DfilePath=c:\\

Upvotes: 1

Leonidas K
Leonidas K

Reputation: 491

I'm using java.util.Properties:

Properties props = new Properties();
props.load(new FileReader("path to your file"));

Upvotes: 1

Related Questions