title: Apache Commons Configuration -- Cannot load/use config files
body: <p>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:</p>
  
  <ul>
  <li>src
  <ul>
  <li>test
  <ul>
  <li>java
  <ul>
  <li>mypackage</li>
  </ul></li>
  <li>resources</li>
  </ul></li>
  </ul></li>
  </ul>
  
  <p>Before, I had a file myconfig.xml in the resources directory. I was loading it it like so:</p>
  
  <pre><code>private static XMLConfiguration configuration = null;
  public static void configure() {
      try {
          configuration = new XMLConfiguration("myconfig.xml");
      }
      catch (ConfigurationException e) {
          System.err.println(e);
      }
  }
  </code></pre>
  
  <p>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:</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="ISO-8859-1"?&gt;
  &lt;configuration&gt;
      &lt;!-- Attempt to load configuration provided on the system --&gt;
      &lt;xml fileName="/etc/myconf/myconfig.xml" config-optional="true" /&gt;
  
      &lt;!-- Load default configuration from the classpath --&gt;
      &lt;xml fileName="myconfig.xml" /&gt;
  &lt;/configuration&gt;
  </code></pre>
  
  <p>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: <a href="http://commons.apache.org/configuration/userguide/howto_configurationbuilder.html" rel="nofollow">http://commons.apache.org/configuration/userguide/howto_configurationbuilder.html</a>. What I have currently is the following:</p>
  
  <pre><code>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");
      }
  }
  </code></pre>
  
  <p>When I run my integration tests, I see no Yay, I see no Herp and I see no Derp. If I place a <code>println</code> before I try to load things, it does print so <code>configure</code> is being called. The output I get from commons configuration amounts to:</p>
  
  <pre><code>-------------------------------------------------------
   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 &lt;&lt;&lt; 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
  </code></pre>
  
  <p>Those initialize methods are just calling <code>configure</code> 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.</p>
  
  <p><strong>Edit #1 (1:58pm):</strong> After making sure beanutils were on the classpath, I now get a brutal explosion of stack trace that starts with</p>
  
  <pre><code>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]
  </code></pre>
  
  <p>And so on and so on... but I do see 'Yay' now. <strike>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.</strike></p>
  
  <p><strong>Edit #2:</strong>
  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.</p>