简体   繁体   中英

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.

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:

I had this problem for 3 days. I found very strange solution. My problem didn't go by updating project or clean build anything. SORRY BUT I HAVE TO EXPLAIN MY PROJECT SENARIO TO BE CLEAR ON SOLUTION. My project build on maven,POM,selenium testng. I had made three class. Testbase"browser call n control", homePage"obj repo n methods" and testsclass" test scenarios n to call methods from Page classes". All worked fine but Now when i added one more test, i made loginpage"obj repo n methods for log in webpage" class. when i called LonginPage object repo from testclass it gave "Nosuchmethodsexception". SOLUTION:- REMOVING loginpage CONSTRUCTOR SOLVED THE PROBLEM. now i am able to call the method using @Test as a separate test to give priorities.

Note: i still have constructor in my homePage class.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM