3
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

5
  • Looks like your property file is not on your classpath (in this case inside your jar) Commented May 4, 2012 at 10:35
  • Yes .. thats right . any Idea how to overcome this Commented May 4, 2012 at 10:36
  • Is the file /properties/database.properties in the jar file that intellij is using? Commented May 4, 2012 at 10:37
  • How_do_I_add_resources_to_my_JAR maven.apache.org/guides/getting-started/… ??? Commented May 4, 2012 at 10:38
  • 1
    Move your file to $rootproject/src/main/resources/properties/database.properties Commented May 4, 2012 at 10:39

4 Answers 4

2

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.

Sign up to request clarification or add additional context in comments.

Comments

1

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.

1 Comment

The jar in Eclipse works for me . But for my team mates it does not recognise the path still. How to handle in Eclipse (Export as jar file)
0

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.

7 Comments

I find that there is no properties file in my jar
Then that's your problem, you have to include the file (or all *.properties files or something) when building the jar file.
Have to add something like this to your maven build: <directory>src</directory> <includes> <include>**/*.properties</include> </includes>
sure .. In maven + IntelliJ I put my properties in resource folder .It wrks fine
OK that's another way, but hard to copy the files back to a non-maven build then, such as run it in eclipse.
|
0

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(...){...}

2 Comments

Hi Daniel ... This works only if I give CONFIGURATION_FILE = "C://...//database.properties" It requires a hardcoded path of the properties .
You are right, I though you want to have the properties files outside the jar. As Guillaume Polet and Andrew Newdigate point out, the solution will be to move database.properties to src/main/resources

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.