缺少依赖?java.lang.NoSuchMethodError [英] Dependency missing? java.lang.NoSuchMethodError

查看:46
本文介绍了缺少依赖?java.lang.NoSuchMethodError的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在编程方面有点经验,但对 Maven 还是很陌生.在我的最新项目中,我使用了 Apache Commons API(配置、cli 等).它编译但在运行时抛出一个 NoSuchMethod 异常.

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.

我的依赖项如下所示:

    <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>

这是发生错误的方法:

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);
}

堆栈跟踪是:

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)

我错过了什么?Stack Overflow 上有几篇文章建议在依赖项中包含commons-beanutils".这样做并没有改变任何东西.任何帮助表示赞赏.

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.

问题是 org.apache.commons.beanutils.PropertyUtilsBean.addBeanIntrospector 方法是在 Apache Commons BeanUtils 的 1.8.3 和 1.9.0 之间添加的.

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.

POM 依赖项表明您正在针对 1.9.3 编译代码,但证据表明您的 JVM 在运行时正在加载旧版本.检查您的运行时类路径/WAR 文件/任何内容,以确保您只有一个 BeanUtils JAR,并且它是正确的版本.

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.

您的 POM 文件的依赖项之间可能存在未被注意到的冲突.您可以通过使用 Maven Dependency Plugin 打印出依赖树来诊断这一点:

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:

这篇关于缺少依赖?java.lang.NoSuchMethodError的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆