Reputation: 1622
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
Reputation: 1622
Another solution:
The problem was: maven-archetype-quickstart only sets **/*.java in the "Included" list.
Upvotes: 1
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
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