0

I have created a java maven web application. Now my problem is that I have to access a properties file using resource bundle or Properties class. But this properties file is in webapp/resources folder i.e. in the same directory as my js and css files.

I have this utility method but I can write new ones if required:

public static ResourceBundle getBundle(String baseName, String absolutePath) {
    File file = new File(absolutePath);
    ResourceBundle bundle = null;
    try {
        URL[] urls = { file.toURI().toURL() };
        ClassLoader loader = Thread.currentThread().getContextClassLoader();
        bundle = ResourceBundle.getBundle(baseName, Locale.getDefault(), loader);
    } catch (MalformedURLException e) {
    }
    return bundle;
}

My project structure:

-project

 -------src

   --------main
           ---------java
           ---------resources
           ---------webapp
                    ----------resources
                              -----------app.properties
                              -----------script.js
ZanattMan
  • 746
  • 1
  • 7
  • 26
Popeye
  • 1,548
  • 3
  • 25
  • 39

3 Answers3

1

It seems that your properties file is relative to the web application root. In that case, you can use getResource() in ServletContext

NilsH
  • 13,705
  • 4
  • 41
  • 59
  • But I am not using it in any servlet. It is simple java class. – Popeye Mar 28 '13 at 11:27
  • But the class is used in a web application? Then you can pass the ServletContext as a parameter to the method. Or even better, use the ServletContext where you need the ResourceBundle, and pass the URL to the method. This obvisouly require you to change the signature of your utility method. – NilsH Mar 28 '13 at 12:21
  • I was trying to make it constant. Like this.. public static final ResourceBundle MIME_TYPES = SVNUtil.getMimeTypes(); – Popeye Mar 28 '13 at 12:26
  • and this constant is from other file..not servlet :( – Popeye Mar 28 '13 at 12:28
  • Well, in that case, I'd say either put it in the classpath, or change your strategy. – NilsH Mar 28 '13 at 12:46
0

You can issue a HttpUrlConnection to yourself (mydomain/myapp/resources/app.properties) but I'd say this is an ugly solution

The more common approach is to relocate you app.properties into your classpath (eg: src/main/resources). Then if you use Spring, you can use ClasspathResource. Or else you can read this post: How to really read text file from classpath in Java

Keep in mind putting app.properties there could mean all your user is able to read the app.properties

Community
  • 1
  • 1
gerrytan
  • 40,313
  • 9
  • 84
  • 99
  • Actually I am already reading some files which are on classpath... But this files needs to be here because my js uses this file by making a ajax call to it.. Also it does not contains any sensitive data.. – Popeye Mar 28 '13 at 09:25
0

I finally got it working by adding my webapp/resources folder to classpath. After adding it to classpath, we can simply do it as -

ResourceBundle bundle = ResourceBundle.getBundle(bundleName);
Popeye
  • 1,548
  • 3
  • 25
  • 39