How to read properties file inside jar?
16,176
Solution 1
To obtain the stream you don't need FileInputStream at all. You can get the properties' file stream like this
InputStream is = VerifyFolderStructure.class.getResourceAsStream("/com/abc/properties/config.properties");
Solution 2
I think you need to add "/" in the front of you path.if you don't add "/" that means the properties is located in the same packages path as VerifyFolderStructure class.
Related videos on Youtube
Author by
NaveenBharadwaj
Updated on June 04, 2022Comments
-
NaveenBharadwaj almost 2 years
I have a config.properties file under the package com.abc.properties. From one of the java class present in com.abc.util, I need to read the property file. Both the files are present inside jar. I have tried using
fs = new FileInputstream(VerifyFolderStructure.class.getResourceAsStream("com/abc/properties/config.properties"));
But it doesn't seem to work. Please help.
P.S: VerifyFolderStructure is my java class from which I need to load the properties file.
-
NaveenBharadwaj about 9 yearsAlso I have tried fs = new FileInputstream(VerifyFolderStructure.class.getResourceAsStream("../properties/config.properties")); this also doesn't seem to work.
-
AKS about 9 yearsAre you getting some error? And, what do you mean by
VerifyFolderStructure is my java class from which I need to load the properties file.
-
MadProgrammer about 9 yearsWell, you won't be able to use it as ` FileInputStream` for starters, because it's not a
File
if it's inside thejar
. -
NaveenBharadwaj about 9 yearsCan you please suggest an alternative? I tried using InputStream as well.
-
-
Gospel about 9 years"/" in front of path means load properties from the classpath.
-
NaveenBharadwaj about 9 yearsI tried that also. After adding that I'm getting a FileNotFoundException. Otherwise I'm getting a NullPointerException
-
Gospel about 9 yearsdoes VerifyFolderStructure in the same path of you file?
-
NaveenBharadwaj about 9 yearsVerifyFolderStructure is under the package com.abc.util