簡體   English   中英

在 jar 中找不到屬性文件

[英]Properties file not found in jar

這是我在我的項目中讀取 .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

您可以查看我的項目結構: 鏈接

我的屬性文件在根項目下。 當我構建 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)

您需要從類路徑中讀取它。 因此,假設它位於 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)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM