NoClass:org/apache/juli/logging/LogFactory

原创
2016/08/02 13:32
阅读数 197

SpringMvc+Spring+Mybatis,内嵌Jetty服务器,jdk1.8

main方法如下:

package com.config;

import java.io.IOException;

import org.apache.jasper.servlet.JspServlet;
import org.eclipse.jetty.server.Server;
import org.eclipse.jetty.servlet.ServletContextHandler;
import org.eclipse.jetty.servlet.ServletHolder;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.context.ContextLoaderListener;
import org.springframework.web.context.WebApplicationContext;
import org.springframework.web.context.support.AnnotationConfigWebApplicationContext;
import org.springframework.web.servlet.DispatcherServlet;

public class ComMain {

	private static final Logger LOGGER = LoggerFactory.getLogger(ComMain.class);

	private static final int PORT = 8888;

	private static final String CONTEXT_PATH = "/";
	private static final String CONFIG_LOCATION_PACKAGE = "com.config";
	//	private static final String MAPPING_URL = "/rest/*";
	private static final String MAPPING_URL = "/";
	/**
	 * 如果是/,则所有路径都会被mvc-dispatcher拦截,默认静态页面是不能访问的,
	 * 这时要在WebMvcConfig里将这些静态页面设为不被spring拦截器处理
	 */
	private static final String WEBAPP_DIRECTORY = "src/main/webapp";

	public static void main(String[] args) throws Exception {
		new ComMain().startJetty(PORT);
	}

	private void startJetty(int port) throws Exception {
		LOGGER.debug("Starting server at port {}", port);
		Server server = new Server(port);

		//		server.setHandler(getWebAppContext());
		server.setHandler(getServletContextHandler());
		addRuntimeShutdownHook(server);
		server.start();
		LOGGER.info("Server started at port {}", port);
		server.join();
	}

	private static ServletContextHandler getServletContextHandler() throws IOException {
		ServletContextHandler contextHandler = new ServletContextHandler(
				ServletContextHandler.SESSIONS); // SESSIONS requerido para JSP 
		contextHandler.setErrorHandler(null);

		contextHandler.setResourceBase(WEBAPP_DIRECTORY);
		contextHandler.setContextPath(CONTEXT_PATH);

		// JSP
		contextHandler.setClassLoader(Thread.currentThread().getContextClassLoader()); // Necesario para cargar JspServlet
		contextHandler.addServlet(JspServlet.class, "*.jsp");
		//不加jspServlet,.jsp文件访问时会报404

		// Spring
		WebApplicationContext webAppContext = getWebApplicationContext();
		DispatcherServlet dispatcherServlet = new DispatcherServlet(webAppContext);
		ServletHolder springServletHolder = new ServletHolder("mvc-dispatcher",
				dispatcherServlet);
		contextHandler.addServlet(springServletHolder, MAPPING_URL);
		contextHandler.addEventListener(new ContextLoaderListener(webAppContext));

		return contextHandler;
	}

	private static WebApplicationContext getWebApplicationContext() {
		AnnotationConfigWebApplicationContext context = new AnnotationConfigWebApplicationContext();
		context.setConfigLocation(CONFIG_LOCATION_PACKAGE);
		return context;
	}

	private static void addRuntimeShutdownHook(final Server server) {
		Runtime.getRuntime().addShutdownHook(new Thread(new Runnable() {
			@Override
			public void run() {
				if (server.isStarted()) {
					server.setStopAtShutdown(true);
					try {
						server.stop();
					} catch (Exception e) {
						System.out.println(
								"Error while stopping jetty server: " + e.getMessage());
						LOGGER.error(
								"Error while stopping jetty server: " + e.getMessage(),
								e);
					}
				}
			}
		}));
	}

}

运行后报错:

java.lang.NoClassDefFoundError: org/apache/juli/logging/LogFactor

按提示加上相应jar包:

<dependency>
            <groupId >org.apache.tomcat </groupId >
            <artifactId >juli </artifactId >
            <version >6.0.45 </version >
 </dependency >

再次运行又报错如下:

org.apache.jasper.JasperException: java.lang.IllegalStateException: No org.apache.tomcat.InstanceManager set in ServletContext

这时我就觉得奇怪了,我明明是用的Jetty服务器,怎么连续两个错都是关于Tomcat的,后来在网上相关查询,折腾了一下午,原来问题出在该项目的运行环境选择了Tomcat,解决方法是:

右键项目-->Properties-->Project facet -->Runtimes,将选中的Apache Tomcat 7.0给去掉。

展开阅读全文
打赏
0
0 收藏
分享
加载中
更多评论
打赏
0 评论
0 收藏
0
分享
返回顶部
顶部