2

In order to load external JAR files that are not placed in WEB-INF/lib folder of my web application I have written a custom loader class by extending org.apache.catalina.loader.WebappLoader class.

What it does is on startup it scans the folder (in my case it is cataline_home\plugins\) and adds all the classes/jars in this folder to the classpath of my web application.

This all is working fine, it loads all the available classes and I can even execute them from my application; but for some reason these classes can't access the properties files within the JAR's.

Say I have JAR called pluginsms.jar, inside this JAR I have a properties file called sms.properties and I am using ResourceBundle resource = ResourceBundle.getBundle("sms"); to read this file, it works perfectly fine when I run it as standalone but when I try to load it in my web application (via the custom WebappLoader) it throws: Resource Not Found Exception.

Given below is the source for my WebappLoader:

public class PluginLoader extends WebappLoader {

    @Override
    public void setContainer(Container container) {
        StandardContext ctx = (StandardContext) container;

        try {
            File pluginFolder = new File(System.getProperty("catalina.home"),
                    "plugins");

            for (File file : pluginFolder.listFiles()) {
                if (file.isDirectory()) {
                    continue;
                }
                if (file.getName().endsWith(".jar")) {
                    addRepository(file.toURI().toString());
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }

        super.setContainer(container);
    }
}
Perception
  • 79,279
  • 19
  • 185
  • 195
Waqas
  • 6,812
  • 2
  • 33
  • 50
  • If I had to wager a guess I would say this is a class loader delegation problem. It is attempting to create the resource bundle in the system class loader which cannot see your plugin JAR's. – Perception Mar 13 '12 at 10:32
  • hmm if this is the case do you think I have to override the default class loader with my own? – Waqas Mar 13 '12 at 11:48
  • @Waqas 'Perception' is right I think, try to put sms.properties in your application/WEB-INF/classes. and verify it is working or not. or you can try to use the logic in the [selected answer of this question](http://stackoverflow.com/questions/2815404/load-properties-file-in-jar). – Asad Rasheed Mar 14 '12 at 05:27
  • @AsadRasheed yeah ur right, if i place them in classes folder it works – Waqas Mar 14 '12 at 06:16

1 Answers1

0

What my guess is that your property file resides in WEB-INF or Webcontent folder and you are accessing you property file using relative path. While your jars who want to use this property file can't see this path that's why you are getting this exception. I would suggest you to use static path for property file. I hope that can help you.

Tee
  • 1