0

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
user3743289
  • 33
  • 1
  • 6

1 Answers1

2

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.

Robby Cornelissen
  • 91,784
  • 22
  • 134
  • 156
  • Note that it is healthy and normal to use Commons Collections 3.x and 4.x in the same project. The packages do not overlap. Even in 2022, the latest `commons-beanutils:1.9.4` still wants `commons-collections:3.2.2`. – AndrewF Feb 16 '22 at 06:46