Search code examples
javapropertiesrelative-pathfileinputstream

Unable to read a file from a jar into a FileInputStream using a relative path


Given I am able to load my mail.properties file such as :

ResourceBundle rb = ResourceBundle.getBundle("META-INF.mail");
System.out.prinltn("subject :" + rb.getString("email.subject")); //output : 'subject : Job Application'

However I am unable to read; in the same method; my mail.properties file as a FileInputStream. I have tried all these options none of them worked and I am always getting a FileNotFoundException :

FileInputStream fis = new FileInputStream("META-INF/mail.properties");
FileInputStream fis = new FileInputStream("META-INF//mail.properties");
FileInputStream fis = new FileInputStream("META-INF\\mail.properties");

The folder META-INF exists within a jar that is loaded at build time :

enter image description here

Any help will be more than appreciated.


Solution

  • The problem is FileInputStream() will look for paths relative to current directory. Since your META-INF is inside the imported jar, it won't be able to find it.

    As per this post, you have following options:

    1. Specify an absolute filename
    2. Specify a relative filename which takes into account where you're running this
    3. Bundle the file as a resource and use Class.getResourceAsStream or similar

    check it out for more details. My question if you are able to load it with ResourceBundle, why are you trying to reload it in a different way?