java Apache Commons 配置——无法加载/使用配置文件
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/10197218/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Apache Commons Configuration -- Cannot load/use config files
提问by Patrick Garrity
I've been trying to configure a Java application (specifically my integration tests) with absolutely no success. I originally had a single XMLConfiguration which I could load and use without issue. My directory setup looks like:
我一直在尝试配置 Java 应用程序(特别是我的集成测试),但没有成功。我最初只有一个 XMLConfiguration,我可以毫无问题地加载和使用它。我的目录设置如下:
- src
- test
- java
- mypackage
- resources
- java
- test
- 源文件
- 测试
- 爪哇
- 我的包裹
- 资源
- 爪哇
- 测试
Before, I had a file myconfig.xml in the resources directory. I was loading it it like so:
之前,我在资源目录中有一个文件 myconfig.xml。我是这样加载它的:
private static XMLConfiguration configuration = null;
public static void configure() {
try {
configuration = new XMLConfiguration("myconfig.xml");
}
catch (ConfigurationException e) {
System.err.println(e);
}
}
Now I'm trying to make it so that users can configure the application themselves by writing a configuration file in /etc or /home or whatever, so I made a new file in the same location called config-test.xml:
现在我试图让用户可以通过在 /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>
The file /etc/myconf/myconfig.xml does not exist, but that's the whole point of making it optional. I've tried a few different approaches to loading my configuration, including how the documentation does it: http://commons.apache.org/configuration/userguide/howto_configurationbuilder.html. What I have currently is the following:
文件 /etc/myconf/myconfig.xml 不存在,但这就是使其成为可选文件的全部意义所在。我尝试了几种不同的方法来加载我的配置,包括文档是如何加载的:http: //commons.apache.org/configuration/userguide/howto_configurationbuilder.html。我目前拥有的是以下内容:
private static CombinedConfiguration configuration = null;
public static void configure() {
try {
DefaultConfigurationBuilder builder =
new DefaultConfigurationBuilder("config-test.xml");
configuration = builder.getConfiguration(true);
System.err.println("Yay");
}
catch (ConfigurationException e) {
System.err.println("Herp");
}
catch (Exception e) {
System.err.println("Derp");
}
}
When I run my integration tests, I see no Yay, I see no Herp and I see no Derp. If I place a println
before I try to load things, it does print so configure
is being called. The output I get from commons configuration amounts to:
当我运行集成测试时,我看不到 Yay,看不到 Herp,也看不到 Derp。如果我println
在尝试加载东西之前放置 a ,它会打印所以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
Those initialize methods are just calling configure
and then trying to get data from the configuration that is loaded. I've been working with this for a couple hours now and could really use another pair of eyes. Any ideas? Is there anything I could have changed that I'm not mentioning that would affect this? Thanks.
这些初始化方法只是调用configure
然后尝试从加载的配置中获取数据。我已经为此工作了几个小时,并且真的可以使用另一双眼睛。有任何想法吗?有什么我可以改变的,我没有提到会影响这个吗?谢谢。
Edit #1 (1:58pm):After making sure beanutils were on the classpath, I now get a brutal explosion of stack trace that starts with
编辑 #1(下午 1:58):在确保 beanutils 在类路径上之后,我现在得到了一个以
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]
And so on and so on... but I do see 'Yay' now. Now what I don't get is that it doesn't seem to be looking for my non-optional configuration source, or assuming it's in a location I never told it to look.
等等等等......但我现在确实看到了'Yay'。现在我不明白的是它似乎没有在寻找我的非可选配置源,或者假设它位于我从未告诉它查看的位置。
Edit #2:I looked through my output a bit more and buried in the complaints from commons I found that it seemed to load the correct file eventually. I think I may have it working? Maybe my path to the properties change when I load it like this... I've got some stuff to try. Anyway, the problem that I asked about has been fixed. Thanks for the help.
编辑 #2:我仔细查看了我的输出,并埋藏在来自公地的抱怨中,我发现它最终似乎加载了正确的文件。我想我可以让它工作吗?也许当我像这样加载它时,我的属性路径会改变......我有一些东西要尝试。不管怎样,我问的问题已经解决了。谢谢您的帮助。
回答by Bozho
You seem to be missing commons-beanutils on your classpath. Make sure that jar is there.
您的类路径中似乎缺少 commons-beanutils。确保那个罐子在那里。
回答by eel ghEEz
Pfft. A multi-page documenttalks about how to load a configuration file. But did they forget to implement detection of file URLs in absolute Windows paths such as c:\Users\USER\FOO\bar.xml? Commons 1.9 will show this scrupulous message,
噗。一个多页文档讨论了如何加载配置文件。但是他们是否忘记在绝对 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