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.

Share:
16,176

Related videos on Youtube

NaveenBharadwaj
Author by

NaveenBharadwaj

Updated on June 04, 2022

Comments

  • NaveenBharadwaj
    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
      NaveenBharadwaj about 9 years
      Also I have tried fs = new FileInputstream(VerifyFolderStructure.class.getResourceAsStr‌​eam("../properties/c‌​onfig.properties")); this also doesn't seem to work.
    • AKS
      AKS about 9 years
      Are 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
      MadProgrammer about 9 years
      Well, you won't be able to use it as ` FileInputStream` for starters, because it's not a File if it's inside the jar.
    • NaveenBharadwaj
      NaveenBharadwaj about 9 years
      Can you please suggest an alternative? I tried using InputStream as well.
  • Gospel
    Gospel about 9 years
    "/" in front of path means load properties from the classpath.
  • NaveenBharadwaj
    NaveenBharadwaj about 9 years
    I tried that also. After adding that I'm getting a FileNotFoundException. Otherwise I'm getting a NullPointerException
  • Gospel
    Gospel about 9 years
    does VerifyFolderStructure in the same path of you file?
  • NaveenBharadwaj
    NaveenBharadwaj about 9 years
    VerifyFolderStructure is under the package com.abc.util