非常简单的 Apache-commons 配置示例抛出 NoClassDefFoundError
2022-09-02 05:17:09
我试图测试Apache-commons配置库用户指南中给出的一个关于声明和创建bean的非常简单的例子。我几乎逐字逐句地复制了示例中的代码,但是我得到了一个NoClassDefFoundError异常。
以下是我正在使用的xml文件 - :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>
以下是文件中的代码:WindowManager.java
package test;
public interface WindowManager {}
以下是文件中的代码:DefaultWindowManager.java
package test;
public class DefaultWindowManager implements WindowManager {
private boolean resizable;
private boolean closable;
private int defaultWidth;
private int defaultHeight;
}
以下是文件中的代码: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);
}
}
以下是运行时的输出:
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
如何使这个简单的示例起作用?
我使用的是commons-configuration package的1.9版本,由IntelliJ IDEA在将依赖项放入文件后自动导入,以及Windows 8 64bit上运行的Java版本1.7.0_17。pom.xml