javaapache-commonsapache-commons-beanutilsapache-commons-collection

I want to sort using commons-collections4-4.0 ComparatorChain


When we use the latest commons-collections4-4.0jar, we get an error message as shown below regarding making a sort option with a usage of the ComparatorChain and the BeanComparator. Do you have any idea what's wrong with this? We use commons-bean utile-1.9.2.jar for beanutils.

■success code (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);

}

■failure code (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);

}

error log

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

Solution

  • As to what's wrong with it: looking at the POM file, commons-beanutils-1.9.2 depends on commons-collections-3.2.1. The problem is that common-beanutils references classes that are no longer available in commons-collections-4.4.0.