javamavenapache-commons

Dependency missing? java.lang.NoSuchMethodError


I'm somewhat experienced in programming but pretty new to Maven. In my latest project I'm using the Apache Commons API (configuration, cli and so on). It compiles but throws me a NoSuchMethod-exception on runtime.

My dependencies look like this:

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

    <dependency>
        <groupId>org.apache.commons</groupId>
        <artifactId>commons-configuration2</artifactId>
        <version>2.0</version>
    </dependency>

    <dependency>
        <groupId>commons-cli</groupId>
        <artifactId>commons-cli</artifactId>
        <version>1.4</version>
    </dependency>

This is the method where the error occurs:

private Configuration parseConfig(String path) {
        File configFile = new File(path);
        if(!configFile.exists() || configFile.isDirectory()) {
            // Error config file path invalid
            CustomLogger.warn("ERROR file not found");
        }
        Configurations configs = new Configurations();
        Configuration config = null;
        try {
            config = configs.properties(configFile);
        }
        catch (ConfigurationException cex) {
            // Something went wrong
            CustomLogger.warn("Config Exception");
            cex.printStackTrace();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return config;
    }

The line/part/block where the error is happening exactly is:

try {
    config = configs.properties(configFile);
}

The stack trace is:

java.lang.NoSuchMethodError: org.apache.commons.beanutils.PropertyUtilsBean.addBeanIntrospector(Lorg/apache/commons/beanutils/BeanIntrospector;)V
        at org.apache.commons.configuration2.beanutils.BeanHelper.initBeanUtilsBean(BeanHelper.java:631)
        at org.apache.commons.configuration2.beanutils.BeanHelper.<clinit>(BeanHelper.java:89)
        at java.lang.Class.forName0(Native Method)
        at java.lang.Class.forName(Unknown Source)
        at com.sun.proxy.$Proxy0.<clinit>(Unknown Source)
        at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
        at sun.reflect.NativeConstructorAccessorImpl.newInstance(Unknown Source)
        at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(Unknown Source)
        at java.lang.reflect.Constructor.newInstance(Unknown Source)
        at java.lang.reflect.Proxy.newProxyInstance(Unknown Source)
        at org.apache.commons.configuration2.builder.fluent.Parameters.createParametersProxy(Parameters.java:294)
        at org.apache.commons.configuration2.builder.fluent.Parameters.fileBased(Parameters.java:185)
        at org.apache.commons.configuration2.builder.fluent.Configurations.fileParams(Configurations.java:602)
        at org.apache.commons.configuration2.builder.fluent.Configurations.fileParams(Configurations.java:614)
        at org.apache.commons.configuration2.builder.fluent.Configurations.fileBasedBuilder(Configurations.java:132)
        at org.apache.commons.configuration2.builder.fluent.Configurations.propertiesBuilder(Configurations.java:238)
        at org.apache.commons.configuration2.builder.fluent.Configurations.properties(Configurations.java:282)
        at com.core.utils.CustomConfiguration.parseConfig(CustomConfiguration.java:130)

What am I missing? There are several posts out there on Stack Overflow suggesting to include "commons-beanutils" in the dependencies. Doing so didn't change anything. Any help appreciated.


Solution

  • It is not a missing dependency. It is an inconsistency between the dependencies at compile time and at runtime.

    The problem is that the org.apache.commons.beanutils.PropertyUtilsBean.addBeanIntrospector method was added between 1.8.3 and 1.9.0 of Apache Commons BeanUtils.

    The POM dependencies say that you are compiling your code against 1.9.3, but the evidence is that your JVM is loading an older version at runtime. Check your runtime classpath / WAR file / whatever to ensure that you have only one BeanUtils JAR, and that it is the correct version.

    It is possible that there is an unnoticed conflict between your POM file's dependencies. You can diagnose this by using the Maven Dependency Plugin to print out the dependency tree: