0

当我们使用最新的 commons-collections4-4.0jar 时,我们会收到一条错误消息,如下所示,关于使用 ComparatorChain 和 BeanComparator 进行排序选项。你知道这有什么问题吗?我们为 beanutils 使用 commons-bean utile-1.9.2.jar。

■成功代码(commons-collections-3.2.1.jar)

@SuppressWarnings("unchecked")
private void testCommonsChain() {

   ComparatorChain comparator = new ComparatorChain();

   List<TestDataBean> testDataBeanList = this.createTestDataBeanList();

   comparator.addComparator(new BeanComparator<TestDataBean>("name"), true);
   comparator.addComparator(new BeanComparator<TestDataBean>("kngk"));
   Collections.sort(testDataBeanList, comparator);

   this.printData(testDataBeanList);

}

■失败代码(commons-collections4-4.0.jar)

private void testCommonsChain() {

    ComparatorChain<TestDataBean> comparator = new ComparatorChain<TestDataBean>();

    List<TestDataBean> testDataBeanList = this.createTestDataBeanList();

    comparator.addComparator(new BeanComparator<TestDataBean>("name"), true);
    comparator.addComparator(new BeanComparator<TestDataBean>("kngk"));
    Collections.sort(testDataBeanList, comparator);

    this.printData(testDataBeanList);

}

错误日志

Exception in thread "main" java.lang.NoClassDefFoundError: org/apache/commons/collections/comparators/ComparableComparator
    at org.apache.commons.beanutils.BeanComparator.<init>(BeanComparator.java:87)
    at logic.TestComparator.testCommonsChain(TestComparator.java:36)
    at logic.TestComparator.executeComparator(TestComparator.java:19)
    at myTestOrange.TestMain.main(TestMain.java:10)
Caused by: java.lang.ClassNotFoundException: org.apache.commons.collections.comparators.ComparableComparator
    at java.net.URLClassLoader$1.run(URLClassLoader.java:202)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.net.URLClassLoader.findClass(URLClassLoader.java:190)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:306)
    at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:301)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:247)
    ... 4 more
4

1 回答 1

2

至于有什么问题:查看POM文件,commons-beanutils-1.9.2依赖于commons-collections-3.2.1。问题是 common-beanutils 引用了 commons-collections-4.4.0 中不再可用的类。

于 2014-06-25T23:53:16.303 回答