14

I have created a dynamic web project within Eclipse. I created a properties file inside the src directory:

<project-root>/src/props.properties

I'm starting Tomcat via Eclipse in debug mode. Now I want to read the properties file from one of my POJOs, but I get a FileNotFoundException. The current path seems to be the Eclipse path.

I had a look into the web for solution, but none of them worked for me. Maybe I did something wrong. The code is like this:

File file = new File("props.properties");
FileReader reader = new FileReader(file);
properties.load(reader);

How should I acces the properties file? Where should it be located?

Thanks in advance.

Arjan Tijms
  • 37,782
  • 12
  • 108
  • 140
c0d3x
  • 691
  • 5
  • 12
  • 19

5 Answers5

23

If its a web application then the properties will get deployed to WEB-INF/classes and can be loaded using the class loader

InputStream in = {NameOfClassWhereThisisInvoked}.class.getResourceAsStream("/props.properties");
properties.load(in);
in.close();

Actully that should work regardless of whether it is a webapp or not.

I'm assuming src is defined as a source folder

hmehandi
  • 356
  • 4
  • 11
objects
  • 8,637
  • 4
  • 30
  • 38
  • 1
    Thanks all for answering. I forgot to mention that I'm starting tomcat via eclipse. The properties file is copied to: C:\workspace\.metadata\.plugins\org.eclipse.wst.server.core\tmp0\wtpwebapps\WebAppName\WEB-INF\classes. Now I have a copy of the props file in the project root and in src folder, but I am not able to acces it anyway. – c0d3x Mar 02 '10 at 10:18
  • Where is your pojo class? The above assumes they are both loaded by the same classloader – objects Mar 02 '10 at 10:56
  • 2
    This is the way to go. It's better to load resources via a classloader to avoid dependencies on local file system arrangement, current working directory etc. The resource location has to be on the classpath of course, so WEB-INF/classes is a good place. BTW if you omit the slash - getResourceAsStream("props.properties") - the resource is expected in the same package as the current class. – Adriaan Koster Mar 02 '10 at 11:23
3

There is another way of doing this as follows:

File file = new File(session.getServletContext().getRealPath("/") + "props.properties");

Instead of "props.properties" you could give any path relative to the "WebContent" folder of your eclipse web-application project.

sandeepkunkunuru
  • 6,150
  • 5
  • 33
  • 37
2

here is the correct way to load properties file from anywhere in the classpath

private Properties getPropertiesFromClasspath(String propFileName)
                                                                throws IOException
{
    Properties props = new Properties();
    InputStream inputStream =
    this.getClass().getClassLoader().getResourceAsStream(propFileName);

    if (inputStream == null)
    {
        throw new FileNotFoundException("property file '" + propFileName
      + "' not found in the classpath");
}

props.load(inputStream);
return props;
}

You can create a singleton class to load properties in memory on first time access and later use them via a static method. A complete example of this is available at http://bharatonjava.wordpress.com/2012/09/12/using-properties-file-in-java-application/

Bharat Sharma
  • 141
  • 1
  • 4
  • 2
    You should probably elaborate on why this is better / what the trade off is vs calling getResourceAsStream on the class rather than the classloader. – EdC Sep 16 '12 at 10:13
1

Kepp ur Properties file in src folder and the following code is enough to read the properties file

  **File file = new File("./src/config.properties");
  FileReader reader = new FileReader(file);
  prop.load(reader);
  System.out.println(prop.getProperty("directorypath"));**
0

Is your project set to build automatically? If so (or you're building manually), check whetherprops.properties appears in your project's output folder (in Eclipse this is usually "bin" but may be WebRoot or something else, depending on how your project is set up).

When a project builds, it should copy over config files as well as your compiled classes, JSPs etc. However, I believe that file readers, by default, use the JVM's home directory for their source. In Eclipse's case, this means that your props.properties file would have to be in the project root, i.e. not the "src" folder.

Ben
  • 7,548
  • 31
  • 45