0

I am facing this issue while reading property file. I searched a lot on the Internet but nothing worked. Below is the code and the image contains the path of the prop.properties file

package Utility;

import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;

public class PropUtility {

    //private static Properties prop;
    
    private static Properties prop = new Properties();
    static {
        prop = new Properties();
        InputStream in = prop.getClass().getResourceAsStream("/resources/prop.properties");
        try {
            prop.load(in);
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }

    public static String getProperty(String key) {
        return prop.getProperty(key);
    }
}

Screen capture of my IDE

Abra
  • 19,142
  • 7
  • 29
  • 41
  • This links may help: [Absolute vs Relative paths](http://www.differencebetween.net/technology/difference-between-absolute-and-relative-path/) and [some rules for working with paths](https://stackoverflow.com/a/16570799/16815462) – Vlad Nitu Aug 06 '22 at 12:56
  • @VladNitu *Never* put `..` in a resource path. A resource path not a file name. – VGR Aug 06 '22 at 14:01
  • @VGR can you elaborate a bit please? Thanks in advance – Vlad Nitu Aug 06 '22 at 15:50
  • @VladNitu The Class.getResource* and ClassLoader.getResource* methods take a relative URL as an argument. That URL is resolved against each classpath entry. No getResource method can access data outside of the classpath. The javadoc of those methods explains this fairly well. – VGR Aug 06 '22 at 15:59
  • @VGR thanks for clarification. I’ll remove my confusing comment. The top response of this [question](https://stackoverflow.com/questions/6608795/what-is-the-difference-between-class-getresource-and-classloader-getresource) and also the javadocs may come in handy to solve the problem – Vlad Nitu Aug 06 '22 at 16:13
  • Based on the structure of your project, I suspect you want `PropUtility.class.getResourceAsStream("/prop.properties")`. – VGR Aug 06 '22 at 16:52

2 Answers2

0
 public static void main(String[] args) throws IOException {
        FileReader reader= null;
        try {
            reader = new FileReader("prop.properties");
        } catch (FileNotFoundException ex) {
            throw new RuntimeException(ex);
        }

        Properties p=new Properties();
        p.load(reader);

        System.out.println(p.getProperty("user"));
        System.out.println(p.getProperty("password"));
    }
Mesut
  • 1
  • 1
  • Your answer could be improved with additional supporting information. Please [edit] to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Aug 09 '22 at 15:37
0

When you run the code, it cannot access your properties file. You get a nullPointerException error when loading in from that one. Get your properties file like in the picture below, put it in the project, then run your code. I also made a similar example with FileReader. Extract from the resource folder. You can read it that way if you import it into the project. Give this a try.

prop.properties is in Projectname/src folder, which also contains the resource folder

Laurel
  • 5,965
  • 14
  • 31
  • 57
Mesut
  • 1
  • 1