getResourceAsStream() is returning null. Properties file is not loading

前端 未结 6 1276
眼角桃花
眼角桃花 2020-12-09 16:23

I am trying to load properties file. Here is my structure

\"Directory

Now i am trying to lo

相关标签:
6条回答
  • 2020-12-09 16:24

    Oh oh ... There are several problems here:

    1) In your first provided code snippet, you are using a ClassLoader for loading a resource file. This is indeed a good decision. But the getResourceAsStream method needs a "class-path relative" name. You are providing an absolute path.

    2) Your second code snippet (after edit) results in not being able to find the file "D:...\LS360BatchImportIntegration\test.properties". According to your screenshot, the file should be "D:...\LS360AutomatedRegulatorsReportingService\test.properties". This is another directory.

    I fear, that your descriptions are not up to date with the findings on your machine.

    But let's just move to a reasonable solution:

    1) In your Eclipse project (the screenshot tells us, that you are using Eclipse), create a new directory named "resources" in the same depth as your "src" directory. Copy - or better move - the properties file into it.

    2) This new directory must be put into the "build path". Right-click the directory in the Package Explorer or Project Explorer view, select "Build Path", then "Use as Source Folder". Note: This build path will be the class path for the project, when you run it.

    3) As the resources directory now is part of your class path and contains your properties file, you can simply load it with getResourceAsStream("test.properties").

    EDIT

    I just see, that you also use Maven (the pom.xml file). In Maven, such a resources directory exists by default and is part of the build path. It is "src/main/resources". If so, just use this.

    0 讨论(0)
  • 2020-12-09 16:24

    I had a similar problem with a file not being found by getResourceAsStream(). The file was in the resources folder (src/main/resources), and still not found.

    The problem got resolved when I went into the eclipse Package Explorer and "refreshed" the resources folder. It was in the directory, but Eclipse did not see it until the folder was refreshed (right-click on the folder and select Refresh).

    0 讨论(0)
  • 2020-12-09 16:26

    You are using the class loader (which reads in the classpath) whereas you are using the absolute path.

    Simply try:

    InputStream resourceAsStream =  new FileInputStream(temp);
    

    As a side note, try instanciating your file doing:

    File temp = new File(workingDir, "test.properties");
    

    to use the system-dependent path spearator.

    0 讨论(0)
  • 2020-12-09 16:41

    I hope this helps !! You can keep your test.properties into src/main/resources

     public static Properties props = new Properties();
        InputStream inStream = Test.class.getResourceAsStream("/test.properties");
        try {
                loadConfigurations(inStream);
            } catch (IOException ex) {
                String errMsg = "Exception in loading configuration file. Please check if application.properties file is present in classpath.";
                ExceptionUtils.throwRuntimeException(errMsg, ex, LOGGER);
            }
        public static void loadConfigurations(InputStream inputStream) throws IOException{
                props.load(inputStream);
            }
    
    0 讨论(0)
  • 2020-12-09 16:42

    You're passing a file path to getResourceAsStream(String name), but name here is a class path, not a file path...

    You could make sure the file is on your classpath, or use a FileInputStream instead.

    0 讨论(0)
  • 2020-12-09 16:45

    Please put your property file in /src/main/resources folder and load from ClassLoader. It will be fix.

    like

     /src/main/resources/test.properties
    
    
    
    Properties properties = null;
    
    try {
        properties = new Properties();
        InputStream resourceAsStream =  Test.class.getClassLoader().getResourceAsStream("test.properties");
        if (resourceAsStream != null) {
            properties.load(resourceAsStream);
        }
    
    
    } catch (IOException e) {
        e.printStackTrace();
    }
    
    0 讨论(0)
提交回复
热议问题