0

I need to read in config.properties for some configuration. I am open to any way of doing it, with requirements.

I want it outside my war file so that it can be changed easily, and does not require a rebuild.

I have searched everywhere but cannot find how to do it, I'm sure this is java 101, but I cant figure it.

I have tried classloader but that seems to only load resources from inside the war, and I cannot find how to get the location I loaded the war into my server from to read it from there. I also cannot find a way to pass in an argument via command line parameter's as its a soap endpoint, which I can access anywhere in my code?

I saw this Where to place and how to read configuration resource files in servlet based application? and i want to use the file system approach but I don't want a hard coded path for the config file.

I just want something simple and easy, I know there is something but I just cannot find it.

Community
  • 1
  • 1
f1wade
  • 2,877
  • 6
  • 27
  • 43
  • What's wrong with putting in classpath outside the project as hinted in the duplciate? – BalusC Sep 01 '16 at 07:29
  • @BalusC it doesnt work. I added to windows my computer environment PATH but it wont load it. im not sure how to add the folder to java's classpath in weblogic? – f1wade Sep 01 '16 at 09:05

2 Answers2

0

I want it outside my war file so that it can be changed easily, and does not require a rebuild.

May not be how you want it, but just to answer this, we have a similar scenario where our web applications are deployed in web servers located in locations /apps/servers/webserver-1, /apps/servers/webserver-2. And we have properties placed in some other locations like /apps/my-web-app1/app.properties and /apps/my-web-app2/app.properties.

Now good thing about this structure is that if I need to update any property, I just do the edit in the relative property and restart my web server. A downside of this is that I have to pass in paths to these properties file as system arguments to my web-server startup scripts (in my case, these are the catalina.sh files, yes I am using tomcats).

So my catalina.sh has line somewhere lying around something like

export JAVA_ARGS `-Dpath.to.properties.of.my-web-app2=/apps/my-web-app2/app.properties` .... 

To read these properties, I have a Property Utility function that gets called by the StartupServlets of each application. The purpose of this function is to simply open up this file by reading the system property path.to.properties.of.my-web-app2 and puts these properties in something like a cache (a HashMap in my case) from where I can access them easily throughout the application.

Saif Asif
  • 5,516
  • 3
  • 31
  • 48
  • thats sounds like what I want, but i'm still unclear as to how to get it to work, what code do i use in java? and how to I configure weblogic / netbeans with the additional folder to look in? – f1wade Sep 01 '16 at 09:03
0

use this this is helpful your question . i think. In one method abcd

public static String abcd(String one) {
    properties = new Properties();
            properties.load(<classNmae>.class.getClassLoader().getResourceAsStream("AppResources.properties"));
return properties.getProperty(one);

call this code

String fileLocation = abcd("internal property file");

            Properties properties = new Properties();
        FileInputStream fis = new FileInputStream(fileLocation );
        properties.load(fis);
        fis.close();

        acd = (String)properties.get("acd");

Note: AppResources.properties have external file location D:/aaa.properties file then in second method you read properties of external file

suresh manda
  • 659
  • 1
  • 8
  • 25
  • that works but make the location of my external properties file effectively hard coded as I need to recompile and build the WAR to change it. – f1wade Sep 01 '16 at 15:38