1
votes

I was trying to use apache commons configuration and running into an issue where it throws a NoClassDefFoundError. It happens on Parameters.xml() call. Below is the full code and the exception details that my IntelliJ generated. I am using jdk1.8.0_91. I appreciate any help in solving this.

import org.apache.commons.configuration2.*;
import org.apache.commons.configuration2.ex.*;
import org.apache.commons.configuration2.builder.*;
import org.apache.commons.configuration2.builder.fluent.*;

public class TestDBA {

    public static void main(String[] args)
    {
        try {
            Parameters params = new Parameters();
            FileBasedConfigurationBuilder<XMLConfiguration> builder = new FileBasedConfigurationBuilder<XMLConfiguration>(XMLConfiguration.class);
            XMLBuilderParameters px = params.xml();
            builder.configure(px.setFileName("myconfig.xml"));
            XMLConfiguration config = builder.getConfiguration();
            System.out.println(config.toString());
        } catch (ConfigurationException cex) {
            // loading of the configuration file failed
            System.out.println("some error occurred");
        }
    }
}

Exception details:

Exception in thread "main" java.lang.NoClassDefFoundError: org/apache/commons/beanutils/DynaBean at java.lang.Class.forName0(Native Method) at java.lang.Class.forName(Class.java:264) at com.sun.proxy.$Proxy0.(Unknown Source) at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method) at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62) at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45) at java.lang.reflect.Constructor.newInstance(Constructor.java:423) at java.lang.reflect.Proxy.newProxyInstance(Proxy.java:739) at org.apache.commons.configuration2.builder.fluent.Parameters.createParametersProxy(Parameters.java:294) at org.apache.commons.configuration2.builder.fluent.Parameters.xml(Parameters.java:232) at TestDBA.main(TestDBA.java:16) at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) at java.lang.reflect.Method.invoke(Method.java:498) at com.intellij.rt.execution.application.AppMain.main(AppMain.java:144) Caused by: java.lang.ClassNotFoundException: org.apache.commons.beanutils.DynaBean at java.net.URLClassLoader.findClass(URLClassLoader.java:381) at java.lang.ClassLoader.loadClass(ClassLoader.java:424) at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:331) at java.lang.ClassLoader.loadClass(ClassLoader.java:357) ... 16 more

Process finished with exit code 1

2

2 Answers

3
votes

commons-configurations depends on commons-beanutils where the missing class (org/apache/commons/beanutils/DynaBean) is defined.

The required JAR is probably missing from your runtime classpath. Additional information about your build process may help for further investigating the problem (what's on your build path, what's on your classpath, etc...).

0
votes

This was how I managed to fix this issue. Firstly, I added commons-beanutil to my pom.xml file:

<!-- Apache Commons Configuration -->
<dependency>
    <groupId>commons-beanutils</groupId>
    <artifactId>commons-beanutils</artifactId>
    <version>1.9.3</version> <!-- Add the latest version of BeanUtils here. -->
</dependency>

<dependency>
    <groupId>org.apache.commons</groupId>
    <artifactId>commons-configuration2</artifactId>
    <version>2.0</version> <!-- Add the latest version of Apache Commons Configuration2 here. -->
</dependency>

<!-- Add any additional dependencies here. -->

I then copied Apache BeanUtils and Apache Commons Configuration into my classpath (in my case this was lib).

Download URLs: