Reputation: 468
propsdatabase = new Properties();
InputStream dbin = getClass().getResourceAsStream("/properties/database.properties");
propsdatabase.load(dbin);
I am reading my database connection details via a properties file which is named 'database.properties' in a folder named 'properties'. Jus below the root directory.
The code was perfectly working fine when the jar was exported in Eclipse.
But I used Maven project in IntelliJ to get the jar . It throws NUll pointer exception .
Since the Value of dbin is NULL.(I printed and checked also).
I conclude that the path is not recognised to read the file.
Now The things are fine with IntelliJ .
While doing an export as jar in Eclipse the jar although contains propertioes folder IT is not detected. pl help
Upvotes: 3
Views: 12426
Reputation: 1842
You may try to read the properties file using the path and a FileInputStream:
Properties properties = new Properties();
FileInputStream input = null;
try {
input = new FileInputStream(new File(CONFIGURATION_FILE));
properties.load(input);
}catch(...){...}
Upvotes: 0
Reputation: 6205
The reason that getResourceAsStream
is returning null is that the /properties/database.properties
file is not in the Maven classpath.
Move your properties folder to under /src/main/resources
folder and when Maven creates a jar file, the /properties/database.properties
resource will be included, and you'll stop getting the NPE.
Upvotes: 2
Reputation: 11909
Does your maven build step include the properties file in the jar? Check the jar-file that is produced. If you don't know how you can always rename it and add a ".zip" at the end and open it.
Upvotes: 0
Reputation: 1500595
Yes, getResourceAsStream
is no doubt returning null. I suspect that your jar file doesn't include the file. Try running:
jar tvf yourfile.jar
to check. Next, check the build steps for the jar file. It's hard to say what's wrong without seeing the build file, so please edit your question to include that information if it doesn't leap out at you when you look at the steps.
Upvotes: 1