6

I'm developing a Dynamic Web Project in Eclipse. I created a .properties file for store database details (Username, Password etc.). I added it by right clicking on the project and New -> File . I used the Java util package Properties class. But it does not working. I can not retrieve any property from the file. Here is the code I used,

Properties prop = new Properties();

    try {

        prop.load(new FileInputStream("database.properties"));

        String db = prop.getProperty("database");
        String userName = prop.getProperty("dbuser");
        String password = prop.getProperty("dbpassword");

    } catch (IOException ex) {
        ex.printStackTrace();
    }

Is there something wrong or Is there any particular place where I should put properties file.

YakovL
  • 7,557
  • 12
  • 62
  • 102
Dilma
  • 625
  • 3
  • 10
  • 22
  • Seems that there is a problem trying to find the database.properties. Where is it located? is it in the same package that your class? – alicia Oct 28 '13 at 18:11

5 Answers5

6

What you did is correct, ie right clicking the project and new--file.You have to Put your properties where you start your jvm from. Please look into the attached image. The properties file is marked in red. Look if your properties file is also located something like this. Also add this in your code to find out where to put your file:

System.out.println(new File(".").getAbsolutePath());

For more details please follow this link- FileNotFoundException when using java properties file

enter image description here

Community
  • 1
  • 1
user2821894
  • 988
  • 2
  • 11
  • 20
  • 2
    yeah! I just found that the path I gave refers to the eclipse folder.I checked the image. There you using the absolute path for the .properties file. But when I host my project some where that's not going to work (Giving the absolute path). I just want to know is there any standard way keep .properties file and access it. Thank you! – Dilma Oct 28 '13 at 18:41
  • 1
    This is not the correct Way of using properties files. Using Absolute path is not recommended. If you file is in C:\\a.properties and you use full path and tomorrow you deploy you WAR in linux this won't work. See Nitind answer – Oliver Jun 24 '15 at 07:18
4

You should instead be using the more portable java.util.Properties#load(InputStream) with the result of javax.servlet.ServletContext#getResourceAsStream(String).

bluish
  • 26,356
  • 27
  • 122
  • 180
nitind
  • 19,089
  • 4
  • 34
  • 43
  • 2
    This is the correct way of doing it. We must place the file in {YourApp}\src\main\resources in a Maven Project. And under WebContent in a non maven Dynamic Web Project – Oliver Jun 24 '15 at 07:24
4

Normally, you make sure the properties file is in the project runtime classpath (e.g. WEB-INF/classes) and then load it using either the System classloader or the property file handler's classloader, i.e. (Freehand typing from memory -- NOT COMPILED)

try{
     Properties p = new Properties();
     InputStream in = MyPropertyHandler.getClass()
           .getClassLoader()
           .getResourceAsStream("com/package/props/database.properties");
     p.load(in);
catch(IOException e){
     e.printStackTrace(System.err);
}

I'm betting you aren't pointing at the correct location. Make sure you're properties file is in the correct place. Using that code, I believe it is looking for ${CURRENT_WORKING_DIR}/database.properties, which is the case of a web app in eclipse is WEB-INF/classes (i think).

MadConan
  • 3,749
  • 1
  • 16
  • 27
  • yeah! I just found that the path I gave refers to the eclipse folder.You mean I should keep the .properties file in the package in which the class which using .properties file? – Dilma Oct 28 '13 at 18:26
  • @Dilma: That's not strictly necessary. You can put it anywhere, but it's easiest (and better practice) to at leaset make sure it is in the runtime classpath. It doesn't even need to be a source folder -- just someplace declared as a resource location (e.g. ext folder). – MadConan Oct 28 '13 at 18:29
1

Try to give absolute path or relative path to the proprty file, also check this propery file path has been add to source folders or not, if not it will not be copied to your classes folder. (Right cclick on project , check java build path under source tab.

Ashok Gudise
  • 79
  • 1
  • 7
1

You should have .properties file in same package as class that is using it.

Or better, read properties file with getResourceAsStream method (otherwise you can have some problem later when you'll have file in .war archive).

InputStream inputStream = 
    getClass().getClassLoader().getResourceAsStream("database.properties");
mtk
  • 13,221
  • 16
  • 72
  • 112
Dario
  • 2,053
  • 21
  • 31