0

I am trying to load a properties file.
I have the properties file in the same directory as the class that I am trying to load it.
Example:

package com.classes.internal;   
public class ClassA {  

    private static String PFILE = "config.properties";  


    private static void methodA(){  
    //do stuff  
        Properties properties = null;  
        try{  
            properties = new Properties();  
            properties.load(new FileInputStream(PFILE));  
            //properties.load(new ClassA().getClass().getResourceAsStream(PFILE)); --> Does not work either   

        }catch(Exception e){  
            e.printStackTrace();  
        }  


    }   

Also config.properties file is in com\classes\internal dir

But I get a FileNotFoundException or java.lang.NullPointerException (if using the commented out line instead of the first)

What am I doing wrong here? What am I missunderstanding?

Jim
  • 18,826
  • 34
  • 135
  • 254

6 Answers6

2

The file needs to be in the directory you're executing from, not the directory where the class file is.

So if you have the directory structure

Project/com/classes/internal

and you run the command

Project$ java com.classes.internal.SomeClass

Your JVM will look for the "config.properties" file in the "Project" directory.

FrankieTheKneeMan
  • 6,645
  • 2
  • 26
  • 37
  • I am not sure I understand this.But then what is the meaning of `getClass().getResourceAsStream` etc? – Jim Apr 06 '12 at 07:35
  • `getClass().getResourceAsStream` is looking for the file in the folder where the source file for the current class is stored. Try doing `System.out.println(new ClassA().getClass().getResourceAsStream("config.properties"));` by placing the `config.properties` file in the folder as stated before. – mtk Apr 13 '12 at 16:09
0

I think your program work directory not in com\classes\internal. Try to pass this relative path:

com\classes\internal\config.properties

or absolute path. You can obtain current working directory like this

String currentDir = new File(".").getAbsolutePath();
ice
  • 777
  • 1
  • 4
  • 19
0
properties.load(this.getClass().getResourceAsStream(
    "/com/classes/internal/" + PFILE));
Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
  • There is no `this`.I am in a `static` method – Jim Apr 06 '12 at 07:33
  • ..and what happened when you used the earlier form, with the altered location string? Note that I'm not in the mood for playing '20 questions' - it pays to use a little initiative when offered answers. – Andrew Thompson Apr 06 '12 at 14:44
0

The line below will load file relative to working directory (in FrankieTheKneeMan example, the "Project" folder. If you are running your code in Tomcat, it is quite likely from $Tomcat/bin/):

properties.load(new FileInputStream(PFILE));

And for the line below, it is loading file relative to your classpath. So "config.properties" refer to directory that contain your "com" folder, instead of "com/classes/internal/".

properties.load(new ClassA().getClass().getResourceAsStream(PFILE));

So you need to decide on whether to load from classpath, or from working directory.

ThiamTeck
  • 385
  • 1
  • 9
0

Example. I'm using eclipse project structure, I hope you get the picture anyway:

//File in Project/src/com/classes/internal/config.properties
InputStream in = ClassA.class.getResourceAsStream("config.properties");
Properties p = new Properties();
try {
   p.load(in);
} catch(IOException e) {
   e.printStackTrace();
}

In case you decide to put your file outside package:

//File in Project/src/config.properties
InputStream in = ClassA.class.getClassLoader().getResourceAsStream("config.properties");

Note that getResourceAsStream file has to be included in your classpath.

viktor
  • 1,277
  • 10
  • 16
0

Create a config package in the source folder directly and place the config.properties file in it. On building the application, it will go to /WEB-INF/classes/config/config.properties

You can then use the following code to access the properties file -

    Properties prop = new Properties();
    String propFileName = "/config/config.properties";

    InputStream input = null;

    input = ClassA.class.getResourceAsStream(propFileName);
    prop.load(input);
Ajay Kelkar
  • 144
  • 1
  • 5
  • 20