java 非常简单的 Apache-commons 配置示例抛出 NoClassDefFoundError
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/16266047/
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
Very simple Apache-commons configuration example throws NoClassDefFoundError
提问by Joe
I'm trying to test a very simple examplegiven 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.
我正在尝试测试Apache-commons 配置库用户指南中关于声明和创建 bean 的一个非常简单的示例。我几乎逐字复制了示例中的代码,但我收到了 NoClassDefFoundError 异常。
Here is the xml file I'm using - windowcongif.xml
:
这是我正在使用的 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>
Here is the code in the file WindowManager.java
:
这是文件中的代码WindowManager.java
:
package test;
public interface WindowManager {}
Here is the code in the file DefaultWindowManager.java
:
这是文件中的代码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
:
这是文件中的代码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.run(URLClassLoader.java:366)
at java.net.URLClassLoader.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.
我使用的是 commons-configuration 包的 1.9 版,在将依赖项放入pom.xml
文件后由 IntelliJ IDEA 自动导入,以及在 Windows 8 64 位上运行的 java 1.7.0_17 版。
采纳答案by aran
Import org.apache.commons.beanutils.PropertyUtils
in your class.
org.apache.commons.beanutils.PropertyUtils
在您的班级中导入。
回答by Jean-Daniel Gamache
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>