20

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.

Joe
  • 2,994
  • 5
  • 31
  • 34

2 Answers2

56

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>
Regular Jo
  • 5,190
  • 3
  • 25
  • 47
  • 5
    Obviously this is the right answer. Dynamic dependencies are given here: https://commons.apache.org/proper/commons-configuration/dependencies.html – cquezel Apr 11 '17 at 15:02
  • 1
    Note the group is `commons-beanutils`, not `org.apache.commons` like many of the other Apache Commons libraries – jamesthollowell Aug 11 '17 at 15:48
  • 1
    This fixed it for me, thanks Jean. As cquezel pointed out, there are a whole bunch of *runtime* dependencies. So your project will compile just fine and throw exceptions when searching for beanutils, or whatever runtime dependencies you'd need. – SomeGuy Apr 24 '18 at 21:58
  • 11
    But shouldn't Apache Commons make `beanutils` a dependency in Maven? This is exactly the kind of dependency problem a build tool is used to solve, isn't it? – flow2k Sep 06 '18 at 23:27
  • @flow2k: see my comment at https://stackoverflow.com/questions/51983073/apache-commons-configuration-classnotfoundexception-org-apache-commons-beanuti/51983207#comment124973202_51983073 – Olivier Faucheux Jan 13 '22 at 08:21
1

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

aran
  • 10,978
  • 5
  • 39
  • 69
  • 1
    You're absolutely right, sorry about that. I posted a new question here: http://stackoverflow.com/q/16266523/566639 – Joe Apr 28 '13 at 18:58
  • @AdamArold It did solve the problem for the user back in 2013. You can tell looking at the accepted answer mark. The user already had the dependency on the pom.xml so the answer below mine (from 2017) is something he already did, He just needed to make the import. You can take a look at his comment and following question to see that he solved the issue with just the IMPORT sentence. You're welcome. – aran Apr 17 '18 at 08:56