Apache Commons 配置:ClassNotFoundException:org.apache.commons.beanutils.DynaBean

Apache Commons Configuration: ClassNotFoundException: org.apache.commons.beanutils.DynaBean

我的 POM 包含:

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

来自 Quick start guide, Reading a properties file 的两个示例代码:

Configurations configs = new Configurations();
try
{
    Configuration config = configs.properties(new File("config.properties"));
    // access configuration properties
    ...
}
catch (ConfigurationException cex)
{
    // Something went wrong
}

Properties files, Using PropertiesConfiguration:

Parameters params = new Parameters();
FileBasedConfigurationBuilder<FileBasedConfiguration> builder =
    new FileBasedConfigurationBuilder<FileBasedConfiguration>(PropertiesConfiguration.class)
    .configure(params.properties()
        .setFileName("usergui.properties"));
try
{
    Configuration config = builder.getConfiguration();
    ...
}
catch(ConfigurationException cex)
{
    // loading of the configuration file failed
}

抛出:

java.lang.NoClassDefFoundError: org/apache/commons/beanutils/DynaBean
...
at org.apache.commons.configuration2.builder.fluent.Parameters.createParametersProxy(Parameters.java:307)
at org.apache.commons.configuration2.builder.fluent.Parameters.fileBased(Parameters.java:186)
at properties.PropertiesTest.testLoadAndStoreWithCommonsConfiguration(PropertiesTest.java:52)
...
Caused by: java.lang.ClassNotFoundException: org.apache.commons.beanutils.DynaBean

mvn dependency:tree 显示:

...
[INFO] +- org.apache.commons:commons-configuration2:jar:2.2:compile
[INFO] |  +- org.apache.commons:commons-lang3:jar:3.6:compile
[INFO] |  \- commons-logging:commons-logging:jar:1.2:compile
...

commons-configuration2's POM 包含:

    ...
    <dependency>
        <groupId>commons-beanutils</groupId>
        <artifactId>commons-beanutils</artifactId>
        <version>1.9.3</version>
        <optional>true</optional><
    /dependency>
    ...

我在我的 POM 中添加了 the following dependency,它起作用了:

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

更新

The latest POM of commons-configuration2(截至 2021 年 10 月)声明 <version>1.9.4。我没试过,但它可能适用于更高版本的 Commons Configuration。

<dependency>
    <groupId>commons-beanutils</groupId>
    <artifactId>commons-beanutils</artifactId>
    <version>1.9.4</version>
</dependency>

添加pom.xml中的代码后,发现不行

我下载了jar,放到Web-INF下的lib内容里,就可以了

我认为Maven的插件日志导致它死掉,它不能自动更新或删除jar。