Barun
Barun

Reputation: 1622

Reading Properties file in Maven Java Project created by maven-archetype-quickstart

I'm trying to read a properties file using the below code on a Java Project created using Maven ArchetypeId=maven-archetype-quickstart

(properties = new Properties()).load(DbCopy.class.getClassLoader()
   .getResourceAsStream("config.properties"));

And it throws exception:

Exception in thread "main" java.lang.NullPointerException
  at java.util.Properties.load(Properties.java:284)
  at benz.bnp.db.DbCopy.main(DbCopy.java:77)

Thanks for helping !

Upvotes: 1

Views: 22514

Answers (3)

Barun
Barun

Reputation: 1622

Another solution:

  1. Right Click on Project in Eclipse
  2. Select Build Path -> Configure Build Path
  3. In Source tab : Add **/*.properties in "Included" for "src/main/java".

The problem was: maven-archetype-quickstart only sets **/*.java in the "Included" list.

Upvotes: 1

SparkOn
SparkOn

Reputation: 8946

Assuming your config.properties is under src/main/resources try something like this :

Properties props = new Properties();
try(InputStream resourceStream = Thread.currentThread().getContextClassLoader().getResourceAsStream("config.properties")) {
    props.load(resourceStream);
}

Upvotes: -1

ppuskar
ppuskar

Reputation: 771

Have a look at the target folder, if it contains your properties file. Hopefully it will not contain. To include your property file edit your pom:

<build>        
<resources>
                        <resource>
                            <directory>${project.basedir}/src/main/java</directory>
                            <filtering>true</filtering>
                            <includes>
                                <include>**/*.properties</include>
                            </includes>
                        </resource>
     </resources>
</build>

Add it to your POM.xml and configure the same for your properties file.

Upvotes: 1

Related Questions