【发布时间】:2022-01-22 11:08:52
【问题描述】:
在版本升级到 log4j 2.17.0 之后,在单元测试期间引发了此异常:
java.lang.ClassNotFoundException: org.apache.logging.log4j.core.util.SetUtils
如何解决这个问题?
【问题讨论】:
在版本升级到 log4j 2.17.0 之后,在单元测试期间引发了此异常:
java.lang.ClassNotFoundException: org.apache.logging.log4j.core.util.SetUtils
如何解决这个问题?
【问题讨论】:
我曾询问过 Log4j 开发人员如何处理这个问题。 该类被视为内部类,不应使用。
看。 https://issues.apache.org/jira/browse/LOG4J2-3309
可能用来替换类的代码应该更不像下面这样(使用: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();
【讨论】:
经过反复试验I found here 升级到 log4j 2.17.0 意味着新的依赖 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>
【讨论】: