0
public xFbConfigReader()
{

    //props = new Properties();
    propsdatabase = new Properties();

    try 
    {
        // load a properties file
        InputStream dbin = getClass().getResourceAsStream("/properties/database.properties");


        propsdatabase.load(dbin);

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

}

I keep My properties File named 'Database.properties' in a folder where the project is named 'Properties'.

When I do a Export as jar in Eclipse . The Properties Folder is visible.

You can see the properties folder

But When I run the program it shows that there is a NUll point exception in dbin.

So which means I require the proper way to form a jar in Eclipse .Kindly suggest.

Meenakshi
  • 468
  • 3
  • 12
  • 31

2 Answers2

1

The better solution while handling properties file would be reading

static Properties databaseproperties= new Properties();
    static {
        try {
            connectionProps.load(YourClassName.class.getClassLoader()
                    .getResourceAsStream("databaseproperties.properties"));
        } catch (Exception e) {
            System.out.println("Exception is " + e.getMessage());
        }
    }

This is better approch because

  • we can move our properties file to someother folder.
  • And infact we can keep properties folder out side of jar. say you can create a folder called Configuration where you can include all the properties files. As it is out side of jar you can change the properties file when ever is required.
  • For change in properties file no need to unjar it.

(OR) simply you can make this change no need to think about directory structure

  1. Step 1: Move properties file to SRC
  2. step 2: change this line as follows

    InputStream dbin = getClass().getResourceAsStream("/database.properties");

This is not much different from previous code as it is anyway stays inside the JAR file.

Maddy
  • 3,726
  • 9
  • 41
  • 55
0

you are getting null pointer exception because properties file is not loaded try to use FileInputStream to load the properties as follows FileInputStream dbin = new FileInputStream("/properties/database.properties"); properties.load(dbin);

Gouse
  • 57
  • 1
  • 8
  • Yes Gouse I understand that. But If u observe in the Image attached showing the extracted tree structure of jar file. the file does not get loaded though you can see that properties folder is present. – Meenakshi May 07 '12 at 05:30