【问题标题】:Cannot load properties file from resources directory无法从资源目录加载属性文件
【发布时间】:2013-12-19 08:42:39
【问题描述】:

我从 Git 存储库中导入了一个项目,并在 Eclipse 中添加了 Maven 特性。在资源文件夹中,我添加了一个名为myconf.properties 的配置文件。现在,每当我尝试从我的 Java 代码打开这个文件时,我都会得到FileNotFoundException。该文件也存在于maven编译项目后生成的target/classes文件夹中。

谁能告诉我可能是什么问题?我尝试加载此文件的 Java 代码是:

props.load(new FileInputStream("myconf.properties"));

其中propsProperties 对象。

谁能给我一些关于如何解决这个问题的提示?

【问题讨论】:

  • props.load(new FileInputStream("src/main/resources/myconf.properties"));

标签: java eclipse maven maven-3


【解决方案1】:

如果文件在编译后放在 target/classes 下,那么它已经在构建路径的一部分目录中。目录 src/main/resources 是此类资源的 Maven 默认目录,它由 Eclipse Maven 插件 (M2E) 自动放置到构建路径中。因此,无需移动您的属性文件。

另一个主题是,如何检索这些资源。构建路径中的资源自动位于正在运行的 Java 程序的类路径中。考虑到这一点,您应该始终使用类加载器加载此类资源。示例代码:

String resourceName = "myconf.properties"; // could also be a constant
ClassLoader loader = Thread.currentThread().getContextClassLoader();
Properties props = new Properties();
try(InputStream resourceStream = loader.getResourceAsStream(resourceName)) {
    props.load(resourceStream);
}
// use props here ...

【讨论】:

  • 如果您不幸被困在 Java 6 或更低版本上,此代码会给您消息“资源规范不允许在此处用于低于 1.7 的源级别”。
  • @k-den 我使用了try-with-resources statement - 你是对的 - 是 Java 7(及更高版本)的功能。但是,它可以被替换为典型的 try-finally 块,该块也适用于旧 Java 版本。
  • JavaServer 页面的目录位置是否相同?
  • props.load(new FileInputStream("src/main/resources/myconf.properties"));
  • @Maninder 什么?
【解决方案2】:

我觉得你需要把它放在src/main/resources下面,然后按如下方式加载:

props.load(new FileInputStream("src/main/resources/myconf.properties"));

您尝试加载它的方式将首先检查项目的基本文件夹。如果它在 target/classes 中并且您想从那里加载它,请执行以下操作:

props.load(new FileInputStream("target/classes/myconf.properties"));

【讨论】:

  • 那行不通。随着应用程序的编译,这些补丁不再存在。要从类路径获取资源,您需要使用类加载器。您需要使用类似 ResourceBundle.getBundle("myconf.properties")
【解决方案3】:

如果是简单的应用,也可以使用getSystemResourceAsStream

try (InputStream inputStream = ClassLoader.getSystemResourceAsStream("config.properties"))..

【讨论】:

  • 您所说的“简单应用程序”是什么意思?在“复杂应用程序”中使用您的解决方案会产生什么后果?
【解决方案4】:

右击Resources文件夹并选择Build Path > Add to Build Path

【讨论】:

【解决方案5】:

使用ClassLoader.getSystemClassLoader()

示例代码:

Properties prop = new Properties();
InputStream input = null;
try {
    input = ClassLoader.getSystemClassLoader().getResourceAsStream("conf.properties");
    prop.load(input);

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

【讨论】:

    【解决方案6】:

    我使用类似的东西来加载属性文件。

            final ResourceBundle bundle = ResourceBundle
                    .getBundle("properties/errormessages");
    
            for (final Enumeration<String> keys = bundle.getKeys(); keys
                    .hasMoreElements();) {
                final String key = keys.nextElement();
                final String value = bundle.getString(key);
                prop.put(key, value);
            }
    

    【讨论】:

      【解决方案7】:

      我相信从 Eclipse 运行,如果您使用 "myconf.properties" 作为相对路径,您的文件结构应该看起来像这样

      ProjectRoot
               src
               bin
               myconf.properties
      

      如果文件路径中没有指定其他目录,Eclipse 将在项目根目录中查找该文件

      【讨论】:

        猜你喜欢
        • 2017-06-28
        • 2019-10-12
        • 2011-09-26
        • 1970-01-01
        • 1970-01-01
        • 2014-10-11
        • 1970-01-01
        • 1970-01-01
        • 2012-03-10
        相关资源
        最近更新 更多