简体   繁体   中英

How to specify the path for getResourceAsStream() method in java

I know this question has been asked several times but I still can't get it work by those solutions.

I have a maven project. And one Config.java file located in consumer/src/main/java . Here's the content:

import java.util.Properties;

public class Config {
    Properties configFile;
    public Config() {
        configFile = new Properties();
        try {
            configFile.load(this.getClass().getClassLoader().
                    getResourceAsStream("property_table.config.txt"));
        } catch(Exception e) {
            e.printStackTrace();
        }
    }

    public String getProperty(String key) {
        String value = this.configFile.getProperty(key);
        return value;
    }
    public static void main(String[] args) {
        Config config = new Config();
        System.out.println("URL: " + config.getProperty("URL"));
        System.out.println("PASSWORD: " + config.getProperty("PASSWORD"));
    }
}

I kept getting nullpointer exception . I know that's because it can't find the file property_table.config.txt .

At first I put the property_table_config.txt file in the same folder( consumer/src/main/java/ ) as Config.java file. And tried use /property_table_config.txt and 'property_table_config.txt`. Neither of them work.

And then I tried using absolute path, not working. And tried using /main/java/property_table_config , not working either.

Then I saw this solution: https://stackoverflow.com/a/2103625/8159477 . So I make a directory called resources and put it under main folder (ie the path of the folder is consumer/src/main/resources , and create a sub-folder config under resources . After putting the property_table_config.txt file there, I changed the code into this:

configFile.load(this.getClass().getClassLoader().getResourceAsStream("/config/property_table.config.txt"));

But this still didn't work. Can anyone give some hint on this? Any suggestions will be appreciated!!

According to the Class.getResourceAsStream :

This method delegates to this object's class loader. If this object was loaded by the bootstrap class loader, the method delegates to ClassLoader.getSystemResourceAsStream.

Before delegation, an absolute resource name is constructed from the given resource name using this algorithm:

  • If the name begins with a '/' ('\/'), then the absolute name of the resource is the portion of the name following the '/'.
  • Otherwise, the absolute name is of the following form:
    modified_package_name/name
    Where the modified_package_name is the package name of this object with '/' substituted for '.' ('\.').

This is how I understand the comments:

  • If you use ClassLoader.getResourceAsStream , send the absolute path from package root, but omitting the first / .
  • If you use Class.getResourceAsStream , send either a path relative the the current Class object (and the method will take the package into account), or send the absolute path from package root, starting with a / .

But in addition to this, you need to be cognizant of your build system. With maven, resource files are stored under src/main/resources .

So, in your case, I believe making the following changes should resolve the issue:

  • Put the file in src/main/resources .
  • Change the code to

     this.getClass() .getResourceAsStream("/property_table.config.txt") //or `Config.class.getResource... 
  • Alternatively, use

      this.getClass().getClassLoader() .getResourceAsStream("property_table.config.txt")` 

I've tried this with a similar setup, and it works as expected.

ClassLoader().getResourceAsStream() is looking files only in classpath. What you need is to have your config file in directory which is in classpath.

So, you have options:

  • when you run your java application from command line you can set path to directory in -cp parameter or CLASSPATH system variable. point there is: directory from where you need to get config file must be in class path - not a file. (eg if file location is c:\\my_projects\\test-project\\config\\my_config.properties and c:\\my_projects\\test-project\\ is in classpath then getResourceAsStream call will be ClassLoader().getResourceAsStream("config/my_config.properties")

  • you can package your file into jar file and root of jar file is starting point for getResourceAsStream("config/my_config.properties")

  • If your Maven project is a jar project you need to use Maven resource plugin to put additional resource(s) into jar.

BTW: Maven does not put anything into jar file from src/main/java/ directory (if you do not explicitly specify it for resource plugin)

If you use IDE like Eclipse with your Maven project src/main/resources is a part of build classpath. Double check is it there and if it is not - do "Update Maven Project" or add it manually.

Still ClassLoader will see your properties file from src/main/resources folder only when you run project in IDE not from standalone Jar file - if you did not package your file or provide location in classpath.

Can you try following code.

Config.class.getResourceAsStream("property_table.config.txt")

Update: This is the code I tried.

package test;

import java.util.Properties;

public class Config
{
  Properties configFile;

  public Config()
  {
    configFile = new Properties();
    try
    {
      configFile.load(Config.class.getResourceAsStream("property_table.config.txt"));
    }
    catch (Exception e)
    {
      e.printStackTrace();
    }
  }

  public String getProperty(String key)
  {
    String value = this.configFile.getProperty(key);
    return value;
  }

  public static void main(String[] args)
  {
    Config config = new Config();
    System.out.println("URL: " + config.getProperty("URL"));
    System.out.println("PASSWORD: " + config.getProperty("PASSWORD"));
  }
}

And I placed property_table.config.txt file withn test package and it worked.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM