2

After the version bump to log4j 2.17.0 this exception was raised during the unit tests:

java.lang.ClassNotFoundException: org.apache.logging.log4j.core.util.SetUtils

How to work around this problem?

freedev
  • 25,946
  • 8
  • 108
  • 125

2 Answers2

5

After some trial and error I found here that upgrading to log4j 2.17.0 implies a new dependency log4j-web

<!-- https://mvnrepository.com/artifact/org.apache.logging.log4j/log4j-web -->
<dependency>
    <groupId>org.apache.logging.log4j</groupId>
    <artifactId>log4j-web</artifactId>
    <version>2.17.0</version>
</dependency>
freedev
  • 25,946
  • 8
  • 108
  • 125
  • 1
    Where this class is specified? I try to find it and do not see it? https://logging.apache.org/log4j/log4j-2.17.0/log4j-web/apidocs/index.html?overview-summary.html – Seweryn Habdank-Wojewódzki Jan 04 '22 at 08:35
  • 1
    That's an internal class -- you should not be pulling log4j-web dependency for the sake of using such util/private class. @SewerynHabdank-Wojewódzki post is the correct solution. – PaoloC Jan 19 '22 at 12:09
  • 1
    @PaoloC I don't use this internal class, in my case it's tomcat that (somehow) has this internal dependency. This answer is written only to help the people that has this problem. – freedev Jan 19 '22 at 12:14
  • In this case, I'd recommend to add "Tomcat" in the title, and also specify Tomcat's version. Otherwise this seems a general question, but it only applies to Application Servers (configured in certain ways). – PaoloC Jan 19 '22 at 12:19
  • This link also helps: https://www.mail-archive.com/announce@tomcat.apache.org/msg00499.html – PaoloC Jan 19 '22 at 12:19
1

I had asked Log4j developers what to do with this. The class is treated as internal and shall not be used.

See. https://issues.apache.org/jira/browse/LOG4J2-3309

The code which might be used to replace the class shall be more less like below (using: org.apache.commons.collections4)

import org.apache.commons.collections4.Predicate;
import org.apache.commons.collections4.SetUtils;

// generic
Predicate<E> predicate = x -> (doSthWith(x));
final Set<E> resultSet = SetUtils.predicatedSet(setOfElements, predicate);
final String[] array = (String[]) resultSet.toArray();

// for example
Predicate<String> containsString = str -> (str.startsWith(stringToSearch));
final Set<String> resultSet = SetUtils.predicatedSet(setOfStrings, containsString);
final String[] arrayOfStrings= (String[]) resultSet.toArray();