user886614
user886614

Reputation: 375

Property file not found in JAR file

I have a project which has Main method to invoke the application. Which is bundled in a JAR file.

I am trying to invoke the application using following line:

java -jar sample.jar -sample 123

This sample.jar bundled with config/config.properties in it.

I have following line of code to read the property file from the JAR.

InputStream inputStream =
    this.getClass().getClassLoader().getResourceAsStream("config/config.properties");

OR

InputStream inputStream = 
    this.getClass().getClassLoader().getResourceAsStream("/config/config.properties");

This code is not able to find property file from the JAR file, While this property file is already exist in the JAR.

Does any one know the solution on this ?

Upvotes: 0

Views: 2145

Answers (1)

user886614
user886614

Reputation: 375

When i moved config.properties out from the config Folder, it's working fine...

I am not sure why that's the case. But if i use following line of code it's working now..

ClassLoader loader = Thread.currentThread().getContextClassLoader();
InputStream inputStream = loader.getResourceAsStream("config.properties");

Upvotes: 1

Related Questions