Very simple Apache-commons configuration example throws NoClassDefFoundError

偶尔善良 提交于 2019-12-18 18:47:56

问题


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 NoClassDefFoundError exception.

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;

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" java.lang.NoClassDefFoundError: org/apache/commons/beanutils/PropertyUtils
    at org.apache.commons.configuration.beanutils.BeanHelper.initProperty(BeanHelper.java:269)
    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)
    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:23)
Caused by: java.lang.ClassNotFoundException: org.apache.commons.beanutils.PropertyUtils
    at java.net.URLClassLoader$1.run(URLClassLoader.java:366)
    at java.net.URLClassLoader$1.run(URLClassLoader.java:355)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.net.URLClassLoader.findClass(URLClassLoader.java:354)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:423)
    at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:308)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:356)
    ... 9 more

How do I make this simple example work?

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


回答1:


Import org.apache.commons.beanutils.PropertyUtils in your class.




回答2:


I had the same problem, I've added this dependency:

<dependency>
    <groupId>commons-beanutils</groupId>
    <artifactId>commons-beanutils</artifactId>
    <version>1.9.3</version>
</dependency>


来源:https://stackoverflow.com/questions/16266047/very-simple-apache-commons-configuration-example-throws-noclassdeffounderror

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!