【问题标题】:Properties file not found in jar在 jar 中找不到属性文件
【发布时间】:2016-05-14 08:13:14
【问题描述】:

这是我在项目中读取 .properties 文件的代码。

public class Configuration {

    private static JSONObject readConfigFile(File filename) throws JSONException {

        JSONObject configProperties = new JSONObject();
        Properties prop = new Properties();
        InputStream input = null;

        try {

            input = new FileInputStream(filename);

            // load a properties file
            prop.load(input);

            // get the property value and print it out


        } catch (IOException ex) {
            ex.printStackTrace();
        } finally {
            if (input != null) {
                try {
                    input.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }

        return configProperties;

    }

    public static JSONObject loadConfigurationDetails(String configFilePath) throws JSONException {
        JSONObject configurationDetails = new JSONObject();

        File configFile = new File(configFilePath);

        if(configFile.exists()){
            configurationDetails = readConfigFile(configFile);
        }else{
            System.out.println("configuration.properties file not found");
            System.exit(0);
        }

        return configurationDetails;
    }

    public static JSONObject loadConfigurationDetails() throws JSONException {

        String configFilePath = "configuration.properties";

        JSONObject loadConfigurationDetails = loadConfigurationDetails(configFilePath);

        return loadConfigurationDetails;
    }
}

以下是我的项目结构

项目名称
-src
-目标
-属性文件
-pom.xml

您可以查看我的项目结构:link

我的属性文件位于根项目下。 当我构建 jar 文件并运行它时,我无法读取这个文件。 我找不到属性文件的文件。

在我的 Main 方法中,我按如下方式调用我的方法:

JSONObject loadConfigurationDetails = Configuration.loadConfigurationDetails();

修改后的代码和错误

public class Configuration {

  public static JSONObject loadConfigurationDetails() throws JSONException {

    Properties prop = new Properties();
    String filePath = "";
        JSONObject configProperties = new JSONObject();
    try {

      InputStream inputStream = 
        Configuration.class.getClassLoader().getResourceAsStream("configuration.properties");

      prop.load(inputStream);
      configProperties.put("URL",prop.getProperty("URL",""));

    } catch (IOException e) {
        e.printStackTrace();
    }

    return configProperties;

  }

}


Exception in thread "main" java.lang.NullPointerException
    at java.util.Properties$LineReader.readLine(Properties.java:434)
    at java.util.Properties.load0(Properties.java:353)
    at java.util.Properties.load(Properties.java:341)
    at com.demo.main.Configuration.loadConfigurationDetails(Configuration.java:33)
    at com.demo.main.Run.main(Run.java:30)

【问题讨论】:

  • 资源不是文件,不能使用FileInputStream 或其同源访问。您应该使用Class.getResource() 和朋友。
  • 请看一下我修改过的代码和我的项目结构。在我修改后的代码中,我收到空指针异常,因为找不到文件。请看我的项目结构。感谢您的回复。
  • 我在 mkyong 的链接中使用了 Alan Hay Sir 提供的代码

标签: java properties-file


【解决方案1】:

您需要从类路径中读取它。因此,假设它位于 JAR 的根目录中(它将位于 src/main/resources/my.props 的标准 Maven 项目布局下):

input = Configuration.class.getResourceAsStream(filename);

http://docs.oracle.com/javase/6/docs/api/java/lang/ClassLoader.html#getResourceAsStream(java.lang.String)

【讨论】:

  • 如果我使用上述方法,我会得到这个错误:非静态变量,这不能从静态上下文中引用。因为我的功能是静态的。如果我从我的函数中删除 static 关键字,这将起作用。但是我将无法在 main 方法中调用我的方法。它也会给我同样的错误。
  • 先生,我已经编辑了我的问题并发布了我现在面临的空指针。感谢您的回复。
  • 当我在 netbeans 文件窗口选项中看到我的配置文件时,我看到的是 src 和目标级别。我的意思是 scr,target,pom.xml 和文件处于同一级别。
  • 你可以查看我的项目结构:link
  • 简单java项目中prop文件的放置位置
猜你喜欢
  • 2014-01-29
  • 2012-07-24
  • 1970-01-01
  • 2017-05-10
  • 2011-05-06
  • 2016-06-01
  • 2018-01-24
  • 2019-03-02
  • 2019-08-11
相关资源
最近更新 更多