1

I have a class,in which ther is a func,which opens a properties file. When i write main in the same class & call that function,i am able to open the properties file n read. but, when i am tying to call the same func in my servlet by creating instance to that class, i get file not found exception.

This is the function, which i have written in my class to read properties file. And both my class and servlet are in src folder. I am using eclipse IDE.

Code:

private void readPropertiesFileAndFieldListData() {
        String PROP_FILE = "./src/fields.properties";
        try {
            FileReader reader = new FileReader(PROP_FILE);
            BufferedReader br = new BufferedReader(reader);
            ArrayList<String> field = new ArrayList<String>();
            while ((str = br.readLine()) != null) {
                if (!str.startsWith("#") && str.trim().length() > 0) {
                    // System.out.println(str);
                    field.add(str);
                    count++;
                }
            }
}
Sathish D
  • 4,854
  • 31
  • 44
  • How do you run the servlet? What servlet container? How do you deploy the code to the servlet contaner? You reference the PROP_FILE via "./source.." which means that is is relativ to your current working directory. This will most likely not work in a servlet context. – bert Oct 17 '11 at 09:05

2 Answers2

4

You're relying on the current working directory of the disk file system path. The current working directory is dependent on how the application is started and is not controllable from inside your application. Relying on it is a very bad idea.

The normal practice is to put that file in the classpath or to add its path to the classpath. Your file is apparently already in the classpath (you placed it in the src folder), so you don't need to change anything else. You should should just get it from the classpath by the class loader. That's more portable than a disk file system path can ever be.

ClassLoader classLoader = Thread.currentThread().getContextClassLoader();
InputStream input = classLoader.getResourceAsStream("fields.properties");
// ...

See also:


Unrelated to the concrete problem, you're basically reinventing the java.util.Properties class. Don't do that. Use the following construct:

ClassLoader classLoader = Thread.currentThread().getContextClassLoader();
InputStream input = classLoader.getResourceAsStream("fields.properties");
Properties properties = new Properties();
properties.load(input);
// ...
Community
  • 1
  • 1
BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
1

PLease write a small test, for printing the file path of "PROP_FILE" to the log or console. it seems, that you relativ path is incorrect.

Your relative path starting point can change, depending on where your *.exe file is started.

Your test should print

File tFile = new File(PROP_FILE);
// print tFile.getAbsolutePath()

Its better to get a special class by calling

SomeClass.class.getResource(name)

Eclipse RCP from the Bundle

Bundle.getResource(ResourcePathString)

EDIT:

Please check, whether the resource is part of your *.jar. It could be, that you missed to add it to the build.properties file. Check whether the file is existing, before you read the properties file.

Markus Lausberg
  • 12,177
  • 6
  • 40
  • 66
  • Thanks for the reply Markus. I printed the absolute path. Its printing the correct path. The file open operation works in the main() of the class, which is in src folder. But, not able to do the same operation from outside the class, i.e from servlet, which is also in src folder. – Sathish D Oct 17 '11 at 09:24