java.lang.ClassNotFoundException: org.apache.log4j.Logger

huangapple go评论86阅读模式
英文:

java.lang.ClassNotFoundException: org.apache.log4j.Logger

问题

我正在尝试运行一个Spring应用程序,但它一直抛出这个错误:

"Servlet.service() for servlet [dispatcherServlet] in context with path [] threw exception [Handler dispatch failed; nested exception is java.lang.NoClassDefFoundError: org/apache/log4j/Logger] with root cause

java.lang.ClassNotFoundException: org.apache.log4j.Logger"

以下是错误的堆栈跟踪。

这是出现错误的RouteConfigurationServiceAmazonS3Impl类中的getRoutingParameters方法,错误发生在STWLogger行上。我不知道如何修复这个。有人能帮忙吗?

更新:我根据下面第一个答案中的第二种方法进行了更改,但错误仍然存在。

以下是我应用程序的更新后的pom.xml文件

<!-- 以下为pom.xml文件内容 -->
英文:

I am trying to run a Spring Application but it keeps throwing this error:

"Servlet.service() for servlet [dispatcherServlet] in context with path [] threw exception [Handler dispatch failed; nested exception is java.lang.NoClassDefFoundError: org/apache/log4j/Logger] with root cause

java.lang.ClassNotFoundException: org.apache.log4j.Logger"

Below is the stack trace of the error.

2020-10-05 12:52:34.835 ERROR 20412 --- [nio-8080-exec-2] o.a.c.c.C.[.[.[/].[dispatcherServlet]    : Servlet.service() for servlet [dispatcherServlet] in context with path [] threw exception [Handler dispatch failed; nested exception is java.lang.NoClassDefFoundError: org/apache/log4j/Logger] with root cause
java.lang.ClassNotFoundException: org.apache.log4j.Logger
	at java.net.URLClassLoader.findClass(URLClassLoader.java:382) ~[na:1.8.0_265]
	at java.lang.ClassLoader.loadClass(ClassLoader.java:418) ~[na:1.8.0_265]
	at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:352) ~[na:1.8.0_265]
	at java.lang.ClassLoader.loadClass(ClassLoader.java:351) ~[na:1.8.0_265]
	at com.mis.serverdata.utils.STWLogger.&lt;init&gt;(STWLogger.java:26) ~[ServerSharedUtils-1.1.4.jar:na]
	at com.mis.serverdata.utils.STWLogger.getLogger(STWLogger.java:35) ~[ServerSharedUtils-1.1.4.jar:na]
	at com.moveinsync.routeconfiguration.application.services.RouteConfigurationServiceAmazonS3Impl.getRoutingParameters(RouteConfigurationServiceAmazonS3Impl.java:102) ~[classes/:na]
	at com.moveinsync.routeconfiguration.application.controllers.RouteConfigurationController.getConfiguration(RouteConfigurationController.java:23) ~[classes/:na]
	at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[na:1.8.0_265]
	at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) ~[na:1.8.0_265]
	at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[na:1.8.0_265]
	at java.lang.reflect.Method.invoke(Method.java:498) ~[na:1.8.0_265]
	at org.springframework.web.method.support.InvocableHandlerMethod.doInvoke(InvocableHandlerMethod.java:190) ~[spring-web-5.2.9.RELEASE.jar:5.2.9.RELEASE]
	at org.springframework.web.method.support.InvocableHandlerMethod.invokeForRequest(InvocableHandlerMethod.java:138) ~[spring-web-5.2.9.RELEASE.jar:5.2.9.RELEASE]
	at org.springframework.web.servlet.mvc.method.annotation.ServletInvocableHandlerMethod.invokeAndHandle(ServletInvocableHandlerMethod.java:105) ~[spring-webmvc-5.2.9.RELEASE.jar:5.2.9.RELEASE]
	at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.invokeHandlerMethod(RequestMappingHandlerAdapter.java:878) ~[spring-webmvc-5.2.9.RELEASE.jar:5.2.9.RELEASE]
	at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.handleInternal(RequestMappingHandlerAdapter.java:792) ~[spring-webmvc-5.2.9.RELEASE.jar:5.2.9.RELEASE]
	at org.springframework.web.servlet.mvc.method.AbstractHandlerMethodAdapter.handle(AbstractHandlerMethodAdapter.java:87) ~[spring-webmvc-5.2.9.RELEASE.jar:5.2.9.RELEASE]
	at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:1040) ~[spring-webmvc-5.2.9.RELEASE.jar:5.2.9.RELEASE]
	at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:943) ~[spring-webmvc-5.2.9.RELEASE.jar:5.2.9.RELEASE]
	at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:1006) ~[spring-webmvc-5.2.9.RELEASE.jar:5.2.9.RELEASE]
	at org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:898) ~[spring-webmvc-5.2.9.RELEASE.jar:5.2.9.RELEASE]
	at javax.servlet.http.HttpServlet.service(HttpServlet.java:626) ~[tomcat-embed-core-9.0.38.jar:4.0.FR]
	at org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:883) ~[spring-webmvc-5.2.9.RELEASE.jar:5.2.9.RELEASE]
	at javax.servlet.http.HttpServlet.service(HttpServlet.java:733) ~[tomcat-embed-core-9.0.38.jar:4.0.FR]
	at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:231) ~[tomcat-embed-core-9.0.38.jar:9.0.38]
	at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166) ~[tomcat-embed-core-9.0.38.jar:9.0.38]
	at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:53) ~[tomcat-embed-websocket-9.0.38.jar:9.0.38]
	at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193) ~[tomcat-embed-core-9.0.38.jar:9.0.38]
	at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166) ~[tomcat-embed-core-9.0.38.jar:9.0.38]
	at org.springframework.web.filter.RequestContextFilter.doFilterInternal(RequestContextFilter.java:100) ~[spring-web-5.2.9.RELEASE.jar:5.2.9.RELEASE]
	at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:119) ~[spring-web-5.2.9.RELEASE.jar:5.2.9.RELEASE]
	at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193) ~[tomcat-embed-core-9.0.38.jar:9.0.38]
	at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166) ~[tomcat-embed-core-9.0.38.jar:9.0.38]
	at org.springframework.web.filter.FormContentFilter.doFilterInternal(FormContentFilter.java:93) ~[spring-web-5.2.9.RELEASE.jar:5.2.9.RELEASE]
	at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:119) ~[spring-web-5.2.9.RELEASE.jar:5.2.9.RELEASE]
	at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193) ~[tomcat-embed-core-9.0.38.jar:9.0.38]
	at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166) ~[tomcat-embed-core-9.0.38.jar:9.0.38]
	at org.springframework.web.filter.CharacterEncodingFilter.doFilterInternal(CharacterEncodingFilter.java:201) ~[spring-web-5.2.9.RELEASE.jar:5.2.9.RELEASE]
	at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:119) ~[spring-web-5.2.9.RELEASE.jar:5.2.9.RELEASE]
	at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193) ~[tomcat-embed-core-9.0.38.jar:9.0.38]
	at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166) ~[tomcat-embed-core-9.0.38.jar:9.0.38]
	at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:202) ~[tomcat-embed-core-9.0.38.jar:9.0.38]
	at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:97) [tomcat-embed-core-9.0.38.jar:9.0.38]
	at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:541) [tomcat-embed-core-9.0.38.jar:9.0.38]
	at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:143) [tomcat-embed-core-9.0.38.jar:9.0.38]
	at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:92) [tomcat-embed-core-9.0.38.jar:9.0.38]
	at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:78) [tomcat-embed-core-9.0.38.jar:9.0.38]
	at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:343) [tomcat-embed-core-9.0.38.jar:9.0.38]
	at org.apache.coyote.http11.Http11Processor.service(Http11Processor.java:374) [tomcat-embed-core-9.0.38.jar:9.0.38]
	at org.apache.coyote.AbstractProcessorLight.process(AbstractProcessorLight.java:65) [tomcat-embed-core-9.0.38.jar:9.0.38]
	at org.apache.coyote.AbstractProtocol$ConnectionHandler.process(AbstractProtocol.java:868) [tomcat-embed-core-9.0.38.jar:9.0.38]
	at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1590) [tomcat-embed-core-9.0.38.jar:9.0.38]
	at org.apache.tomcat.util.net.SocketProcessorBase.run(SocketProcessorBase.java:49) [tomcat-embed-core-9.0.38.jar:9.0.38]
	at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149) [na:1.8.0_265]
	at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624) [na:1.8.0_265]
	at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61) [tomcat-embed-core-9.0.38.jar:9.0.38]
	at java.lang.Thread.run(Thread.java:748) [na:1.8.0_265]

This is the getRoutingParameters method in RouteConfigurationServiceAmazonS3Impl class where the error is happening on the STWLogger line. I don't know how to fix this. Anyone?

    @Override
    public RoutingParameters getRoutingParameters(String businessUnit) throws Exception {
        RoutingParameters params = null;
        String content = getRouteConfigurationData(businessUnit,
                Constants.ROUTE_PARAMETER_FILENAME);
        if (content == null) {
            STWLogger.getLogger().debug(&quot;No Parameter Configuration found for &quot; + businessUnit + &quot;\nFetching Configuration for Default for businessUnit &quot;);
            content = getRouteConfigurationData(Constants.ROUTE_CONFIGURATION_DEFAULT_BUSINESSUNIT, Constants.ROUTE_PARAMETER_FILENAME);
            if (content == null) {
                return params;
            }
            params = GsonUtils.getGson().fromJson(content, RoutingParameters.class);
            saveRoutingParameters(businessUnit, params);
            return params;
        }
        params = GsonUtils.getGson().fromJson(content, RoutingParameters.class);
        return params;
    }

Update: I have made the changes based on the 2nd approach of the 1st answer given below, but the error persists.

Below is the updated pom.xml file for my application

&lt;?xml version=&quot;1.0&quot; encoding=&quot;UTF-8&quot;?&gt;
&lt;project xmlns=&quot;http://maven.apache.org/POM/4.0.0&quot; xmlns:xsi=&quot;http://www.w3.org/2001/XMLSchema-instance&quot;
	xsi:schemaLocation=&quot;http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd&quot;&gt;
	&lt;modelVersion&gt;4.0.0&lt;/modelVersion&gt;
	&lt;parent&gt;
		&lt;groupId&gt;org.springframework.boot&lt;/groupId&gt;
		&lt;artifactId&gt;spring-boot-starter-parent&lt;/artifactId&gt;
		&lt;version&gt;2.3.4.RELEASE&lt;/version&gt;
		&lt;relativePath/&gt; &lt;!-- lookup parent from repository --&gt;
	&lt;/parent&gt;

	&lt;groupId&gt;com.moveinsync.routeconfiguration&lt;/groupId&gt;
	&lt;artifactId&gt;RouteConfigurationCRUD&lt;/artifactId&gt;
	&lt;version&gt;0.0.1-SNAPSHOT&lt;/version&gt;
	&lt;name&gt;RouteConfigurationCRUD&lt;/name&gt;
	&lt;description&gt;Spring Application for Route Configuration&lt;/description&gt;

	&lt;properties&gt;
		&lt;java.version&gt;1.8&lt;/java.version&gt;
		&lt;spring-cloud.version&gt;Hoxton.SR8&lt;/spring-cloud.version&gt;
	&lt;/properties&gt;

	&lt;dependencies&gt;

		&lt;!-- MoveInSync Dependencies--&gt;
		&lt;dependency&gt;
			&lt;groupId&gt;com.moveinsync.routeconfiguration&lt;/groupId&gt;
			&lt;artifactId&gt;mis-routeconfiguration-model&lt;/artifactId&gt;
			&lt;version&gt;2.1.4-SNAPSHOT&lt;/version&gt;
		&lt;/dependency&gt;

		&lt;dependency&gt;
			&lt;groupId&gt;com.moveinsync.routeconfiguration&lt;/groupId&gt;
			&lt;artifactId&gt;mis-routeconfiguration-client&lt;/artifactId&gt;
			&lt;version&gt;2.1.8-SNAPSHOT&lt;/version&gt;
		&lt;/dependency&gt;

		&lt;!-- Spring Boot --&gt;
		&lt;dependency&gt;
			&lt;groupId&gt;org.springframework.boot&lt;/groupId&gt;
			&lt;artifactId&gt;spring-boot-starter-web&lt;/artifactId&gt;
			&lt;exclusions&gt;
				&lt;exclusion&gt;
					&lt;groupId&gt;org.springframework.boot&lt;/groupId&gt;
					&lt;artifactId&gt;spring-boot-starter-logging&lt;/artifactId&gt;
				&lt;/exclusion&gt;
			&lt;/exclusions&gt;
		&lt;/dependency&gt;

		&lt;dependency&gt;
			&lt;groupId&gt;org.springframework.boot&lt;/groupId&gt;
			&lt;artifactId&gt;spring-boot-starter-log4j2&lt;/artifactId&gt;
		&lt;/dependency&gt;

		&lt;dependency&gt;
			&lt;groupId&gt;org.springframework.boot&lt;/groupId&gt;
			&lt;artifactId&gt;spring-boot-starter-cache&lt;/artifactId&gt;
			&lt;exclusions&gt;
				&lt;exclusion&gt;
					&lt;groupId&gt;org.springframework.boot&lt;/groupId&gt;
					&lt;artifactId&gt;spring-boot-starter-logging&lt;/artifactId&gt;
				&lt;/exclusion&gt;
			&lt;/exclusions&gt;
		&lt;/dependency&gt;

		&lt;dependency&gt;
			&lt;groupId&gt;org.springframework.cloud&lt;/groupId&gt;
			&lt;artifactId&gt;spring-cloud-starter-aws&lt;/artifactId&gt;
			&lt;version&gt;2.2.2.RELEASE&lt;/version&gt;
			&lt;exclusions&gt;
				&lt;exclusion&gt;
					&lt;groupId&gt;org.springframework.boot&lt;/groupId&gt;
					&lt;artifactId&gt;spring-boot-starter-logging&lt;/artifactId&gt;
				&lt;/exclusion&gt;
			&lt;/exclusions&gt;
		&lt;/dependency&gt;

		&lt;dependency&gt;
			&lt;groupId&gt;org.springframework.boot&lt;/groupId&gt;
			&lt;artifactId&gt;spring-boot-configuration-processor&lt;/artifactId&gt;
			&lt;optional&gt;true&lt;/optional&gt;
		&lt;/dependency&gt;

		&lt;!-- SLF4J-API --&gt;
		&lt;dependency&gt;
			&lt;groupId&gt;org.slf4j&lt;/groupId&gt;
			&lt;artifactId&gt;slf4j-api&lt;/artifactId&gt;
			&lt;version&gt;1.7.30&lt;/version&gt;
		&lt;/dependency&gt;

		&lt;dependency&gt;
			&lt;groupId&gt;org.slf4j&lt;/groupId&gt;
			&lt;artifactId&gt;slf4j-simple&lt;/artifactId&gt;
			&lt;version&gt;1.7.30&lt;/version&gt;
			&lt;scope&gt;test&lt;/scope&gt;
		&lt;/dependency&gt;

		&lt;!-- Link SLF4J and log4j2 --&gt;
		&lt;dependency&gt;
			&lt;groupId&gt;org.apache.logging.log4j&lt;/groupId&gt;
			&lt;artifactId&gt;log4j-slf4j-impl&lt;/artifactId&gt;
			&lt;version&gt;2.0.1&lt;/version&gt;
		&lt;/dependency&gt;

		&lt;!-- Spring --&gt;
		&lt;dependency&gt;
			&lt;groupId&gt;org.springframework&lt;/groupId&gt;
			&lt;artifactId&gt;spring-web&lt;/artifactId&gt;
			&lt;version&gt;5.2.9.RELEASE&lt;/version&gt;
		&lt;/dependency&gt;

		&lt;dependency&gt;
			&lt;groupId&gt;org.springframework&lt;/groupId&gt;
			&lt;artifactId&gt;spring-context&lt;/artifactId&gt;
			&lt;version&gt;5.2.9.RELEASE&lt;/version&gt;
			&lt;exclusions&gt;
				&lt;!-- Exclude Commons Logging in favor of SLF4j --&gt;
				&lt;exclusion&gt;
					&lt;groupId&gt;commons-logging&lt;/groupId&gt;
					&lt;artifactId&gt;commons-logging&lt;/artifactId&gt;
				&lt;/exclusion&gt;
			&lt;/exclusions&gt;
		&lt;/dependency&gt;

	&lt;/dependencies&gt;

	&lt;dependencyManagement&gt;

		&lt;dependencies&gt;
			&lt;!-- https://mvnrepository.com/artifact/org.springframework.cloud/spring-cloud-aws-context --&gt;
			&lt;dependency&gt;
				&lt;groupId&gt;org.springframework.cloud&lt;/groupId&gt;
				&lt;artifactId&gt;spring-cloud-aws-context&lt;/artifactId&gt;
				&lt;version&gt;2.2.2.RELEASE&lt;/version&gt;
			&lt;/dependency&gt;
		&lt;/dependencies&gt;

	&lt;/dependencyManagement&gt;

	&lt;build&gt;
		&lt;plugins&gt;
			&lt;plugin&gt;
				&lt;groupId&gt;org.springframework.boot&lt;/groupId&gt;
				&lt;artifactId&gt;spring-boot-maven-plugin&lt;/artifactId&gt;
				&lt;version&gt;2.3.4.RELEASE&lt;/version&gt;
			&lt;/plugin&gt;

			&lt;plugin&gt;
				&lt;groupId&gt;org.apache.maven.plugins&lt;/groupId&gt;
				&lt;artifactId&gt;maven-jar-plugin&lt;/artifactId&gt;
				&lt;version&gt;3.2.0&lt;/version&gt;
				&lt;configuration&gt;
					&lt;archive&gt;
						&lt;manifest&gt;
							&lt;addClasspath&gt;true&lt;/addClasspath&gt;
							&lt;classpathPrefix&gt;lib/&lt;/classpathPrefix&gt;
							&lt;mainClass&gt;theMainClass&lt;/mainClass&gt;
						&lt;/manifest&gt;
					&lt;/archive&gt;
				&lt;/configuration&gt;
			&lt;/plugin&gt;
		&lt;/plugins&gt;
	&lt;/build&gt;

&lt;/project&gt;

答案1

得分: 2

好的。以下是翻译好的内容:

故障的代码行如下:

STWLogger.getLogger().debug("未找到业务单元" + businessUnit + "的参数配置\n正在获取默认的业务单元配置");

我无法找到STWLogger类的源代码以进行直接分析确认,但从堆栈跟踪中看,似乎它试图通过Log4j显式地实例化日志记录系统,而不是依赖于标准的日志记录API SLF4J,在运行时选择适当的日志记录系统。

问题在于,在您应用的Spring Boot的标准配置中,您没有可用的Log4j。实际上,Spring Boot将在类路径中导入并使用Logback作为默认的日志记录解决方案,由启动器spring-boot-starter-logging提供,该启动器是您在pom.xml中使用的启动器spring-boot-starter-web的依赖项。

在我看来,您有两个解决方案。

1)通过调用SLF4J API来替换有问题的代码行,它会自动在后台使用Logback。可能如下所示:

package org.mypackage;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class TestLog {
    
    private static final Logger LOGGER = LoggerFactory.getLogger(TestLog.class);
    
    public void test() {
        LOGGER.info("您的日志。");
    }
}

2)在您的Spring Boot应用程序中将Logback替换为Log4j。您可以按照以下指南执行:

英文:

Ok. The faulty line here is:

STWLogger.getLogger().debug(&quot;No Parameter Configuration found for &quot; + businessUnit + &quot;\nFetching Configuration for Default for businessUnit &quot;);

I cannot find the source code of the STWLogger class to confirm by a direct analysis, but it seems from the stacktrace that it tries to instantiate the logging system explicitly through Log4j, instead of relying on the standard logging API SLF4J that would select the appropriate logging system at runtime.

The problem here is that you do not have Log4j available with the standard configuration of Spring Boot that you applied. Indeed Spring Boot will import and use Logback as the default logging solution in your classpath, provided by the starter spring-boot-starter-logging that is a dependency of the starter spring-boot-starter-web you use in you pom.xml.

You have two solutions in my opinion.

  1. Replace the faulty line by a call to the SLF4J API, that will use Logback automatically behind. It could be something like:
package org.mypackage;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class TestLog {
    
    private static final Logger LOGGER = LoggerFactory.getLogger(TestLog.class);
    
    public void test() {
        LOGGER.info(&quot;Your log.&quot;);
    }
}

  1. Replace Logback with Log4j in you Spring Boot application. You can follow these guides to do so:

huangapple
  • 本文由 发表于 2020年10月5日 15:51:45
  • 转载请务必保留本文链接:https://go.coder-hub.com/64204485.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定