1

I'm developing a web application(this is my first time) and pretty confused about using property files. I don't know where to put the property files.

The problem is that i have put it under the WEB_INF folder. And when i test run it as a Java Application to check whether Database connections are working according to the properties in the property file it is working without any problem.

But when i run it in a Server as a Web Application it fails to load the properties file saying it could not find the file in the path specified. I tried using every possible path i could give and changing the file directories within the whole project. But I kept getting the same error.

Then i changed my class again from scratch thinking there's some kind of a bug withing my code where i load the properties file. And it seems that it could not find the file either when deployed as a Web App. But my test application works fine. Where do i put this file and how do i use it. I have read @BalusC's answer in this thread https://stackoverflow.com/a/2161583/2999358 but i have no idea why this happens. Can someone help me on this?

I'm using Tomcat 8, Eclipse IDE and building on JSF framework.

Class where i load my properties file

public class ConfigCache {
private static final File FILE = new File("./WebContent/WEB-INF/conf/config.properties");
private static final Properties PROPERTIES = new Properties();

public static final String JDBC_DRIVER = ConfigCache.getProperty("db.driverName");
public static final String DATABASE_URL = ConfigCache.getProperty("db.url");
public static final String DATABASE_USERNAME = ConfigCache.getProperty("db.user");
public static final String DATABASE_PASSWORD = ConfigCache.getProperty("db.pass");

public ConfigCache() {
}

public static String getProperty(String key) {
    if (PROPERTIES.isEmpty()) {
        loadProperties();
    }
    Object value;
    return (value = PROPERTIES.get(key)) == null ? "" : value.toString();
}

private static void loadProperties() {
    if (!FILE.exists()) {
        throw new IllegalArgumentException("The 'config.properties' has not been found.");
    }
    try {
        FileInputStream fis = null;
        try {
            fis = new FileInputStream(FILE);
            PROPERTIES.load(fis);
        } finally {
            try {
                if (fis != null) {
                    fis.close();
                }
            } catch (IOException exp) {
                System.out.println("IOException @" + ConfigCache.class + " @ loadProperties() : " + exp);
            }
        }
    } catch (Exception exp) {
        System.out.println("Exception @" + ConfigCache.class + " @ loadProperties() : " + exp);
    }
}

}

Folder Structure

Project Folder Structure

Cœur
  • 32,421
  • 21
  • 173
  • 232
k9yosh
  • 781
  • 1
  • 10
  • 27
  • 1
    I recommend to stop tagging Java EE related questions with [java]. As you see, you only attracted nitwits who didn't even bother to read the question you linked there. – BalusC Jun 12 '15 at 06:24
  • Sorry it was a mistake. Won't happen again. :) – k9yosh Jun 12 '15 at 06:38

3 Answers3

-1

You can explode (unzip) your war/ear file and see the contents or folder structure of it and find why your code doesnt work. The reason is that the folder WebContent doesnt exist in your ear/war , but does exist only when run via eclipse. This is the reason why its always better to follow the solution provided in the link posted so that you can retrieve the porperty files from classpath. The below code fetches your property file in eclipse but not in the server.

private static final File FILE = new File("./WebContent/WEB-INF/conf/config.properties");

Contents of WAR file (from JournelDev), it contains WEB-INF directory but there would be no WebContent directory above it

enter image description here

Kalyan Chavali
  • 1,220
  • 7
  • 24
  • That's not correct. Everything in WEB-INF is part of the war. It's the way the property file is access, not the location that is wrong. – BetaRide Jun 12 '15 at 06:05
  • I never said WEB-INF wouldnt be in the war. I said the directory WebContent wouldnt be in the WAR. Please read the complete answer before you downvote. – Kalyan Chavali Jun 12 '15 at 06:07
  • @KalyanChavali I get what you are saying. I have tried it with my previous implementation. And i tried with this too. It doesn't work. – k9yosh Jun 12 '15 at 06:36
-1

Try With this. put the property in src folder.

Sanjay
  • 2,265
  • 1
  • 11
  • 26
-1

Your file is in the WEB-INF directory. This means it's part of the war and reachable as part of the class path. That's perfectly ok, since it makes it portable and independant of the web container installation (e.g. Tomcat).

You can load any file in the class path as a resource:

getClass().getResourceAsStream("/conf/config.properties")

This means you can write your code like this:

private static void loadProperties() {
        InputStream is = getClass().getResourceAsStream("/conf/config.properties");
        PROPERTIES.load(fis);
}

(Error handling omitted)

BetaRide
  • 14,756
  • 26
  • 84
  • 153