我想使用commons-collections4-4.0 ComparatorChain进行排序

时间:2014-06-16 15:37:56

标签: java apache-commons apache-commons-beanutils apache-commons-collection

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

■成功代码(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

1 个答案:

答案 0 :(得分:1)

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