Kapsh
Kapsh

Reputation: 22111

Access external config file from Java EE app

I have to access a config file which is in a directory on the app server file system, but it is not deployed as part of my Java EE app.

Would getResourceAsStream() help me in this case?

Upvotes: 1

Views: 1060

Answers (2)

user1470804
user1470804

Reputation: 21

In Real Time applications, the "External Config" files play very important role. Externalizing the Property files could ease your build and deployment process. As these are placed in an external location (not included in WAR file) any change to these would not require a new build and deployment.

In order to externilize the property files, you just need to place the property files in an external folder and make that folder accessible to JVM by putting that in Classpath as shown below. Assume that you place your propety files in "C:\conf"

  1. Add this Folder to Set ENV or server Start Up script like this.

    set "CLASSPATH=%CLASSPATH%%CATALINA_HOME%\bin\bootstrap.jar;C:\conf
    
  2. Or Add this right after Java Cmd as below.

    java -cp c:\conf yourClass
    
  3. Please your config file in WEB-INF/Classes folder you want read this using "getResourceAsStream"

    getClass().getClassLoader().getResource("Configuration.properties").getFile();
    

Upvotes: 1

skaffman
skaffman

Reputation: 403551

Would getResourceAsStream() help me in this case?

Not unless the config file is on the classpath, no. You're going to have use java.io to do what you need, unless you can convince the appserver to fetch it for you by some non-standard means.

This will take you outside of the JavaEE realm, and runs the risk of falling afoul of the appserver's security model.

Upvotes: 2

Related Questions