Java Apache Commons配置--无法加载/使用配置文件

Java Apache Commons配置--无法加载/使用配置文件,java,configuration,apache-commons,Java,Configuration,Apache Commons,我一直在尝试配置Java应用程序(特别是我的集成测试),但完全没有成功。我最初有一个XMLConfiguration,可以毫无问题地加载和使用它。我的目录设置如下所示: src 试验 爪哇 我的包裹 资源 以前,我在resources目录中有一个文件myconfig.xml。我是这样加载的: private static XMLConfiguration configuration = null; public static void configure() { try

我一直在尝试配置Java应用程序(特别是我的集成测试),但完全没有成功。我最初有一个XMLConfiguration,可以毫无问题地加载和使用它。我的目录设置如下所示:

  • src
    • 试验
      • 爪哇
        • 我的包裹
      • 资源
以前,我在resources目录中有一个文件myconfig.xml。我是这样加载的:

private static XMLConfiguration configuration = null;
public static void configure() {
    try {
        configuration = new XMLConfiguration("myconfig.xml");
    }
    catch (ConfigurationException e) {
        System.err.println(e);
    }
}
现在,我正在尝试使用户可以通过在/etc或/home或其他文件中编写配置文件来配置应用程序,因此我在同一位置创建了一个名为config-test.xml的新文件:

<?xml version="1.0" encoding="ISO-8859-1"?>
<configuration>
    <!-- Attempt to load configuration provided on the system -->
    <xml fileName="/etc/myconf/myconfig.xml" config-optional="true" />

    <!-- Load default configuration from the classpath -->
    <xml fileName="myconfig.xml" />
</configuration>
当我运行集成测试时,我没有看到Yay,没有看到Herp,也没有看到Derp。如果我在尝试加载东西之前放置了一个
println
,它确实会打印,因此调用了
configure
。我从commons配置中获得的输出总计为:

-------------------------------------------------------
 T E S T S
-------------------------------------------------------
Running TestSuite
13:36:57.752 [main] DEBUG o.a.c.c.ConfigurationUtils - ConfigurationUtils.locate(): base is null, name is config-test.xml
13:36:57.753 [main] DEBUG o.a.c.c.DefaultFileSystem - Could not locate file config-test.xml at null: no protocol: config-test.xml
13:36:57.756 [main] DEBUG o.a.c.c.ConfigurationUtils - Loading configuration from the context classpath (config-test.xml)
13:36:57.766 [main] DEBUG o.a.c.c.DefaultConfigurationBuilder - Creating configuration null with name null
13:36:57.795 [main] DEBUG o.a.c.c.ConfigurationUtils - ConfigurationUtils.locate(): base is null, name is config-test.xml
13:36:57.795 [main] DEBUG o.a.c.c.DefaultFileSystem - Could not locate file config-test.xml at null: no protocol: config-test.xml
13:36:57.796 [main] DEBUG o.a.c.c.ConfigurationUtils - Loading configuration from the context classpath (config-test.xml)
13:36:57.800 [main] DEBUG o.a.c.c.DefaultConfigurationBuilder - Creating configuration null with name null
Tests run: 56, Failures: 2, Errors: 0, Skipped: 16, Time elapsed: 2.589 sec <<< FAILURE!

Results :

Failed tests:   initialize(mypackage.ImportTestIT): org/apache/commons/beanutils/PropertyUtils
  initialize(mypackage.TransferTestIT): org/apache/commons/beanutils/PropertyUtils

Tests run: 56, Failures: 2, Errors: 0, Skipped: 16
等等等等等等。。。但我现在确实看到了“耶”。现在我不明白的是,它似乎不是在寻找我的非可选配置源,或者假设它位于我从未告诉过它的位置

编辑#2:
我仔细查看了一下我的输出结果,在commons的抱怨声中我发现它似乎最终加载了正确的文件。我想我能让它工作吗?当我像这样加载时,属性的路径可能会改变。。。我有些东西要试试。不管怎样,我问的问题已经解决了。谢谢你的帮助。

你的课程路径上似乎缺少了commons beanutils。确保罐子在那里。

Pfft。讨论如何加载配置文件。但他们是否忘记在绝对Windows路径(如c:\Users\USER\FOO\bar.xml)中检测文件URL?Commons 1.9将显示这一谨慎的信息

DEBUG DefaultFileSystem - Could not locate file C:\Users\USER\FOO\bar.xml at null: unknown protocol: c

奇怪。我已经习惯了maven为我处理依赖项,但它不在那里——我专门为它添加了一个依赖项,它确实改变了一些东西。谢谢我的类路径中有这个bean utils,问题仍然存在@Bozho
Running TestSuite
14:00:16.088 [main] DEBUG o.a.c.c.ConfigurationUtils - ConfigurationUtils.locate(): base is null, name is config-test.xml
14:00:16.089 [main] DEBUG o.a.c.c.DefaultFileSystem - Could not locate file config-test.xml at null: no protocol: config-test.xml
14:00:16.093 [main] DEBUG o.a.c.c.ConfigurationUtils - Loading configuration from the context classpath (config-test.xml)
14:00:16.103 [main] DEBUG o.a.c.c.DefaultConfigurationBuilder - Creating configuration null with name null
14:00:16.178 [main] DEBUG o.a.c.c.ConfigurationUtils - ConfigurationUtils.locate(): base is file:///home/pgarrity/projects/myproject/target/test-classes/config-test.xml, name is /etc/myconf/myproject/myconfig.xml
14:00:16.179 [main] DEBUG o.a.c.c.DefaultFileSystem - Could not locate file /etc/myconf/myproject/myconfig.xml at file:///home/pgarrity/projects/myproject/target/test-classes/config-test.xml: /etc/myconf/myproject/myconfig.xml (No such file or directory)
14:00:16.181 [main] DEBUG o.a.c.c.DefaultConfigurationBuilder - Load failed for optional configuration xml: Cannot locate configuration source /etc/myconf/myproject/myconfig.xml
14:00:16.185 [main] WARN  o.a.c.c.DefaultConfigurationBuilder - Internal error
org.apache.commons.configuration.ConfigurationException: Cannot locate configuration source /etc/myconf/myproject/myconfig.xml
at org.apache.commons.configuration.AbstractFileConfiguration.load(AbstractFileConfiguration.java:259) ~[commons-configuration-1.8.jar:1.8]
at org.apache.commons.configuration.AbstractFileConfiguration.load(AbstractFileConfiguration.java:238) ~[commons-configuration-1.8.jar:1.8]
DEBUG DefaultFileSystem - Could not locate file C:\Users\USER\FOO\bar.xml at null: unknown protocol: c