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?

3
  • Where is properties file relative to your classpath ? Commented Apr 6, 2012 at 7:08
  • It is in the same directory as ClassA Commented Apr 6, 2012 at 7:11
  • Similar question: stackoverflow.com/questions/7688710/… Commented Apr 6, 2012 at 7:12

6 Answers 6

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.

Sign up to request clarification or add additional context in comments.

2 Comments

I am not sure I understand this.But then what is the meaning of getClass().getResourceAsStream etc?
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.
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();

Comments

0
properties.load(this.getClass().getResourceAsStream(
    "/com/classes/internal/" + PFILE));

2 Comments

There is no this.I am in a static method
..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.
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.

Comments

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.

Comments

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);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.