【问题标题】:Maven Properties file not found [duplicate]未找到 Maven 属性文件 [重复]
【发布时间】:2016-12-08 23:22:06
【问题描述】:

我正在使用 Maven 网络应用程序。 以下是我放置属性文件的项目结构。

以下是我用来读取此文件的代码:

public static Properties loadProducerProperty() throws FileException {
     Properties myProperties = new Properties();

     try {
         String resourceName = "newproperties.properties"; // could also be a constant
         ClassLoader loader = Thread.currentThread().getContextClassLoader();
         try (InputStream resourceStream = loader.getResourceAsStream(resourceName)) {
             myProperties.load(resourceStream);
         }


     } catch (FileNotFoundException e) {
         throw new FileException();
     } catch (IOException e) {
         throw new FileException();
     }

     return myProperties;
 }

但我收到了FileNotFound 异常

我已经浏览了以下链接并尝试了其他方法,但我遇到了同样的错误:

Cannot load properties file from resources directory

我在这里做错了什么。

谢谢

【问题讨论】:

    标签: java maven


    【解决方案1】:

    在文件名前加一个斜杠:

    String resourceName = "/newproperties.properties"; // could also be a constant
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2014-04-08
      • 2016-11-21
      • 2013-06-20
      • 1970-01-01
      • 2018-11-24
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多