简体   繁体   English

缺少依赖? java.lang.NoSuchMethodError

[英]Dependency missing? java.lang.NoSuchMethodError

I'm somewhat experienced in programming but pretty new to Maven.我在编程方面有些经验,但对 Maven 还是很陌生。 In my latest project I'm using the Apache Commons API (configuration, cli and so on).在我的最新项目中,我使用的是 Apache Commons API(配置、cli 等)。 It compiles but throws me a NoSuchMethod-exception on runtime.它编译但在运行时抛出一个 NoSuchMethod 异常。

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. Stack Overflow 上有几篇文章建议在依赖项中包含“commons-beanutils”。 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. 问题是,在Apache Commons BeanUtils的1.8.3和1.9.0之间添加了org.apache.commons.beanutils.PropertyUtilsBean.addBeanIntrospector方法。

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. POM依赖关系表明您正在根据1.9.3编译代码,但是有证据表明您的JVM正在运行时加载较旧的版本。 Check your runtime classpath / WAR file / whatever to ensure that you have only one BeanUtils JAR, and that it is the correct version. 检查运行时类路径/ WAR文件/任何内容,以确保只有一个BeanUtils JAR,并且它是正确的版本。

It is possible that there is an unnoticed conflict between your POM file's dependencies. 您的POM文件的依存关系之间可能会引起不明显的冲突。 You can diagnose this by using the Maven Dependency Plugin to print out the dependency tree: 您可以通过使用Maven依赖插件来打印出依赖树来诊断此问题:

I had this problem for 3 days.我有这个问题3天。 I found very strange solution.我发现了非常奇怪的解决方案。 My problem didn't go by updating project or clean build anything.我的问题不是 go 通过更新项目或干净构建任何东西。 SORRY BUT I HAVE TO EXPLAIN MY PROJECT SENARIO TO BE CLEAR ON SOLUTION.对不起,但我必须解释我的项目 SENARIO 才能明确解决方案。 My project build on maven,POM,selenium testng.我的项目建立在 maven,POM,selenium testng 上。 I had made three class.我做了三个 class。 Testbase"browser call n control", homePage"obj repo n methods" and testsclass" test scenarios n to call methods from Page classes". Testbase“浏览器调用n控制”,主页“obj repo n方法”和testsclass“测试场景n从Page类调用方法”。 All worked fine but Now when i added one more test, i made loginpage"obj repo n methods for log in webpage" class.一切正常,但现在当我再添加一个测试时,我创建了 loginpage“用于登录网页的 obj repo n 方法”class。 when i called LonginPage object repo from testclass it gave "Nosuchmethodsexception".当我从 testclass 调用 LonginPage object 存储库时,它给出了“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.现在我可以使用@Test 作为单独的测试来调用该方法来给出优先级。

Note: i still have constructor in my homePage class.注意:我的主页 class 中仍然有构造函数。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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