Simple Apache-commons configuration example throws ConfigurationRuntimeException

Joe picture Joe · Apr 28, 2013 · Viewed 8.7k times · Source

I'm trying to test a very simple example given in the Apache-commons configuration library user's guide regarding declaring and creating beans. I copied the code in the example almost word by word, and yet I'm getting a ConfigurationRuntimeException (this after overcoming a different exception, see this question).

Here is the xml file I'm using - windowcongif.xml:

<?xml version="1.0" encoding="ISO-8859-1" ?>
<config>
  <gui>
    <windowManager config-class="test.DefaultWindowManager"
      closable="false" resizable="true" defaultWidth="400"
      defaultHeight="250">      
    </windowManager>
  </gui>
</config>

Here is the code in the file WindowManager.java:

package test;
public interface WindowManager {}

Here is the code in the file DefaultWindowManager.java:

package test;
public class DefaultWindowManager  implements WindowManager {
    private boolean resizable;
    private boolean closable;
    private int defaultWidth;
    private int defaultHeight;
}

Here is the code in the file Main.java:

package test;
import org.apache.commons.configuration.ConfigurationException;
import org.apache.commons.configuration.XMLConfiguration;
import org.apache.commons.configuration.beanutils.BeanDeclaration;
import org.apache.commons.configuration.beanutils.BeanHelper;
import org.apache.commons.configuration.beanutils.XMLBeanDeclaration;
import org.apache.commons.beanutils.PropertyUtils;

public class Main {
    public static void main(String[] args) throws ConfigurationException {
        XMLConfiguration config = new XMLConfiguration("windowconfig.xml");
        BeanDeclaration decl = new XMLBeanDeclaration(config, "gui.windowManager");
        WindowManager wm = (WindowManager) BeanHelper.createBean(decl);
    }
}

Here is the output during runtime:

Exception in thread "main" org.apache.commons.configuration.ConfigurationRuntimeException: org.apache.commons.configuration.ConfigurationRuntimeException: Property defaultHeight cannot be set on test.DefaultWindowManager
    at org.apache.commons.configuration.beanutils.BeanHelper.createBean(BeanHelper.java:341)
    at org.apache.commons.configuration.beanutils.BeanHelper.createBean(BeanHelper.java:358)
    at org.apache.commons.configuration.beanutils.BeanHelper.createBean(BeanHelper.java:372)
    at test.Main.main(Main.java:24)
Caused by: org.apache.commons.configuration.ConfigurationRuntimeException: Property defaultHeight cannot be set on test.DefaultWindowManager
    at org.apache.commons.configuration.beanutils.BeanHelper.initProperty(BeanHelper.java:271)
    at org.apache.commons.configuration.beanutils.BeanHelper.initBeanProperties(BeanHelper.java:229)
    at org.apache.commons.configuration.beanutils.BeanHelper.initBean(BeanHelper.java:166)
    at org.apache.commons.configuration.beanutils.DefaultBeanFactory.initBeanInstance(DefaultBeanFactory.java:108)
    at org.apache.commons.configuration.beanutils.DefaultBeanFactory.createBean(DefaultBeanFactory.java:64)
    at org.apache.commons.configuration.beanutils.BeanHelper.createBean(BeanHelper.java:336)
    ... 3 more

How do I make this simple example work?

I'm using version 1.9 of the commons-configuration package and version 1.8.3 of the commons-beanutils package, auto-imported by IntelliJ IDEA after putting the dependencies in the pom.xml file, and version 1.7.0_17 of java running on Windows 8 64bit.

Answer

Peter Lawrey picture Peter Lawrey · Apr 28, 2013

If you are using JavaBeans you will need to add a setter for each field you want to set.

I suggest using the add setter and getter in IntelliJ for these fields.

The example states

// getters and setters ommitted, also the WindowManager methods