我遇到了一个问题,但我无法弄清楚这个问题的原因,因为错误相当普遍。
java.lang.NoClassDefFoundError:无法初始化类org.springframework.web.util.NestedServletException at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:980) 在 org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:859) 在javax.servlet.http.HttpServlet.service(HttpServlet.java:687)at org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:844) 在javax.servlet.http.HttpServlet.service(HttpServlet.java:790)at org.eclipse.jetty.servlet.ServletHolder.handle(ServletHolder.java:845) 在 org.eclipse.jetty.servlet.ServletHandler.doHandle(ServletHandler.java:583) 在 org.eclipse.jetty.server.handler.ContextHandler.doHandle(ContextHandler.java:1160) 在 org.eclipse.jetty.servlet.ServletHandler.doScope(ServletHandler.java:511) 在 org.eclipse.jetty.server.handler.ContextHandler.doScope(ContextHandler.java:1092) 在 org.eclipse.jetty.server.handler.ScopedHandler.handle(ScopedHandler.java:141) 在 org.eclipse.jetty.server.handler.HandlerWrapper.handle(HandlerWrapper.java:134) 在org.eclipse.jetty.server.Server.handle(Server.java:518)at org.eclipse.jetty.server.HttpChannel.handle(HttpChannel.java:308)at at org.eclipse.jetty.server.HttpConnection.onFillable(HttpConnection.java:244) 在 org.eclipse.jetty.io.AbstractConnection $ ReadCallback.succeeded(AbstractConnection.java:273) 在org.eclipse.jetty.io.FillInterest.fillable(FillInterest.java:95) 在 org.eclipse.jetty.io.SelectChannelEndPoint $ 2.run(SelectChannelEndPoint.java:93) 在 org.eclipse.jetty.util.thread.strategy.ExecuteProduceConsume.produceAndRun(ExecuteProduceConsume.java:246) 在 org.eclipse.jetty.util.thread.strategy.ExecuteProduceConsume.run(ExecuteProduceConsume.java:156) 在 org.eclipse.jetty.util.thread.QueuedThreadPool.runJob(QueuedThreadPool.java:654) 在 org.eclipse.jetty.util.thread.QueuedThreadPool $ 3.run(QueuedThreadPool.java:572) 在java.lang.Thread.run(Thread.java:745)
这是我的项目结构:
这是一个非常简单的项目,我将Embedded Jetty与SpringMVC一起使用。
这是pom.xml
文件:
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.biendltb</groupId>
<artifactId>com.biendltb.world_map</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>jar</packaging>
<name>com.biendltb.world_map</name>
<properties>
<endorsed.dir>${project.build.directory}/endorsed</endorsed.dir>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<jetty.version>9.3.8.v20160314</jetty.version>
<spring.version>4.2.5.RELEASE</spring.version>
<servlet.version>2.5</servlet.version>
</properties>
<dependencies>
<dependency>
<groupId>javax</groupId>
<artifactId>javaee-web-api</artifactId>
<version>7.0</version>
<scope>provided</scope>
</dependency>
<!-- SPRING DEPENDENCY -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-aop</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-web</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-beans</artifactId>
<version>4.2.5.RELEASE</version>
</dependency>
<!-- JETTY DEPENDENCY -->
<dependency>
<groupId>org.eclipse.jetty</groupId>
<artifactId>jetty-server</artifactId>
<version>${jetty.version}</version>
</dependency>
<dependency>
<groupId>org.eclipse.jetty</groupId>
<artifactId>jetty-servlet</artifactId>
<version>${jetty.version}</version>
</dependency>
<dependency>
<groupId>org.eclipse.jetty</groupId>
<artifactId>jetty-webapp</artifactId>
<version>${jetty.version}</version>
</dependency>
<!-- SERVLET DEPENDENCY -->
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>servlet-api</artifactId>
<version>${servlet.version}</version>
</dependency>
<!-- COMMON LOGGING DEPENDENCY -->
<dependency>
<groupId>commons-logging</groupId>
<artifactId>commons-logging</artifactId>
<version>1.1.3</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.1</version>
<configuration>
<source>1.7</source>
<target>1.7</target>
<compilerArguments>
<endorseddirs>${endorsed.dir}</endorseddirs>
</compilerArguments>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-dependency-plugin</artifactId>
<version>2.6</version>
<executions>
<execution>
<phase>validate</phase>
<goals>
<goal>copy</goal>
</goals>
<configuration>
<outputDirectory>${endorsed.dir}</outputDirectory>
<silent>true</silent>
<artifactItems>
<artifactItem>
<groupId>javax</groupId>
<artifactId>javaee-endorsed-api</artifactId>
<version>7.0</version>
<type>jar</type>
</artifactItem>
</artifactItems>
</configuration>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.eclipse.jetty</groupId>
<artifactId>jetty-maven-plugin</artifactId>
<version>9.3.7.v20160115</version>
</plugin>
<plugin>
<artifactId>maven-war-plugin</artifactId>
<version>2.4</version>
<configuration>
<warSourceDirectory>WebContent</warSourceDirectory>
<failOnMissingWebXml>false</failOnMissingWebXml>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-shade-plugin</artifactId>
<version>2.4.3</version>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>shade</goal>
</goals>
<configuration>
<createDependencyReducedPom>false</createDependencyReducedPom>
<transformers>
<transformer
implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
<mainClass>com.biendltb.main_server.TripMapServer</mainClass>
</transformer>
</transformers>
<filters>
<filter>
<artifact>*:*</artifact>
<excludes>
<exclude>META-INF/*.SF</exclude>
<exclude>META-INF/*.DSA</exclude>
<exclude>META-INF/*.RSA</exclude>
</excludes>
</filter>
</filters>
</configuration>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-resources-plugin</artifactId>
<version>2.7</version>
<dependencies>
<dependency>
<groupId>org.apache.maven.shared</groupId>
<artifactId>maven-filtering</artifactId>
<version>1.3</version>
</dependency>
</dependencies>
</plugin>
</plugins>
<resources>
<resource>
<targetPath>webapp</targetPath>
<directory>src/main/webapp</directory>
</resource>
<resource>
<directory>src/main/resources</directory>
</resource>
</resources>
</build>
这是main
类:
public class TripMapServer {
private static final int DEFAULT_PORT = 8080;
private static final String CONTEXT_PATH = "/";
private static final String MAPPING_URL = "/*";
private static final String CONFIG_LOCATION = "com.biendltb.config";
private static final String DEFAULT_PROFILE = "dev";
public static void main(String[] args) throws Exception {
new TripMapServer().startJetty(getPortFromArgs(args));
}
private static int getPortFromArgs(String[] args) {
if (args.length > 0) {
try {
return Integer.valueOf(args[0]);
} catch (NumberFormatException ignore) {
}
}
return DEFAULT_PORT;
}
private void startJetty(int port) throws Exception {
Server server = new Server(port);
server.setHandler(getServletContextHandler(getContext()));
server.start();
server.join();
}
private static ServletContextHandler getServletContextHandler(WebApplicationContext context) throws IOException {
ServletContextHandler contextHandler = new ServletContextHandler();
contextHandler.setErrorHandler(null);
contextHandler.setContextPath(CONTEXT_PATH);
contextHandler.addServlet(new ServletHolder("default", new DispatcherServlet(context)), MAPPING_URL);
contextHandler.addEventListener(new ContextLoaderListener(context));
contextHandler.setResourceBase(new ClassPathResource("webapp").getURI().toString());
return contextHandler;
}
private static WebApplicationContext getContext() {
AnnotationConfigWebApplicationContext context = new AnnotationConfigWebApplicationContext();
context.setConfigLocation(CONFIG_LOCATION);
context.getEnvironment().setDefaultProfiles(DEFAULT_PROFILE);
return context;
}
}
这是类WebConfig.java
而不是.xml
文件:
@Configuration
@EnableWebMvc
@Import({DefaultConfig.class})
@ComponentScan(basePackages = {"com.biendltb.controller"})
public class WebConfig extends WebMvcConfigurerAdapter{
private static final Charset UTF8 = Charset.forName("UTF-8");
@Autowired
public Environment env;
@Override
public void configureMessageConverters(List<HttpMessageConverter<?>> converters) {
converters.add(stringConverter());
}
private StringHttpMessageConverter stringConverter() {
StringHttpMessageConverter stringConverter = new StringHttpMessageConverter();
stringConverter.setSupportedMediaTypes(Arrays.asList(new MediaType("text", "plain", UTF8)));
return stringConverter;
}
@Override
public void addResourceHandlers(ResourceHandlerRegistry registry) {
registry.addResourceHandler("/assets/**").addResourceLocations("classpath:/src/main/resources/").setCachePeriod(Integer.MAX_VALUE);
registry.addResourceHandler("/js/**").addResourceLocations("/js/").setCachePeriod(Integer.MAX_VALUE);
registry.addResourceHandler("/css/**").addResourceLocations("/css/").setCachePeriod(Integer.MAX_VALUE);
registry.addResourceHandler("/img/**").addResourceLocations("/img/").setCachePeriod(Integer.MAX_VALUE);
}
@Override
public void configureDefaultServletHandling(DefaultServletHandlerConfigurer configurer) {
configurer.enable();
}
@Bean
public InternalResourceViewResolver getInternalResourceViewResolver() {
InternalResourceViewResolver resolver = new InternalResourceViewResolver();
resolver.setPrefix("/src/main/resources/pages/");
resolver.setSuffix(".jsp");
return resolver;
}
}
请帮助找出原因或如何追踪该错误。
非常感谢!
答案 0 :(得分:2)
NoClassDefFoundError通常意味着您在运行时丢失了一个jar文件,但并非总是如此,在您的情况下也不如此。类加载器的其他错误也可能导致它。
我在org.springframework.web.util.NestedServletException上遇到了同样的错误。我在调试器中运行了Jetty,并在org.springframework.web.servlet.FrameworkServlet试图构造NestedServletException的地方设置了一个断点,因此我可以看到它试图嵌套的异常。
事实证明这是一个StackOverflowError,并且由于JVM的堆栈帧不足,类加载器无法加载NestedServletException类。 Jetty ServletHolder正在调用Spring DispatcherServlet,然后将其转发回ServletHolder,从而使其再次调用DispatcherServlet。
答案 1 :(得分:0)
您在运行时缺少一个类。 调试启动并在NoClassDefFoundError上设置异常断点。 比你看到哪个类无法找到。在IDE或Web中搜索该类,以找出您缺少的依赖项。
答案 2 :(得分:0)
我建议您清除.m2
文件夹,可能会发生某些jar文件在下载过程中损坏。
请评论,如果它没有帮助你!