的NoClassDefFoundError越来越后端谟到我的前端时

问题描述:

我有这个后台项目在我的pom.xml:的NoClassDefFoundError越来越后端谟到我的前端时

<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>GestionScolaire</groupId> 
    <artifactId>GestionScolaire</artifactId> 
    <version>0.0.1-SNAPSHOT</version> 
    <packaging>war</packaging> 
    <build> 
    <sourceDirectory>src/java</sourceDirectory> 
... 

然后我的前端项目获得这个后台项目在我的pom.xml

<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>GestionScolaire</groupId> 
<artifactId>GestionScolaireWeb</artifactId> 
<version>0.0.1-SNAPSHOT</version> 
<packaging>war</packaging> 
<description>project_Spring</description> 
<name>GestionScolaireWeb</name> 
<url>http://maven.apache.org</url> 
<properties> 
    <java.version>1.6</java.version> 
    <spring.version>3.1.0.RELEASE</spring.version> 
    <cglib.version>2.2.2</cglib.version> 
</properties> 

<dependencies> 
<dependency> 
    <groupId>GestionScolaire</groupId> 
    <artifactId>GestionScolaire</artifactId> 
    <version>0.0.1-SNAPSHOT</version> 
    <type>war</type> 
</dependency> 

Matiere

package model; 

import java.util.List; 

import javax.persistence.Entity; 
import javax.persistence.FetchType; 
import javax.persistence.GeneratedValue; 
import javax.persistence.Id; 
import javax.persistence.JoinColumn; 
import javax.persistence.ManyToOne; 
import javax.persistence.OneToMany; 
import javax.persistence.Version; 

@Entity 
public class Matiere { 

... 

开始我的服务器时,我有这样的错误:

org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping': Invocation of init method failed; nested exception is java.lang.NoClassDefFoundError: Matiere

你们有,为什么我有这样的错误任何想法?

谢谢!

+0

我的同事创造了2 “的web项目”。因此,类在WEB-INF文件夹....无论如何,谢谢你们 – Spiiff77

至少部分问题就在于此:

<dependency> 
    <groupId>GestionScolaire</groupId> 
    <artifactId>GestionScolaire</artifactId> 
    <version>0.0.1-SNAPSHOT</version> 
    <type>war</type> 
</dependency> 

如果你包括战争类型的神器,你不能从它的WEB-INF/classes文件夹就像我认为你要使用的类这样做,所以你看到的错误是预期的行为。

一场战争不像一个罐子。

关于可能的解决方案,我想提一下Maven中的WAR项目也可以将其作为jar发布为classes。 这link here更好地描述它:

如何创建包含在我的web应用程序的类的JAR?

If you would simply like to package the classes and resources as a JAR in WEB-INF/lib rather than as loose files under WEB-INF/classes, 

使用以下配置:

  <plugin> 
      <artifactId>maven-war-plugin</artifactId> 
      <version>3.1.0</version> 
      <configuration> 
       <archiveClasses>true</archiveClasses> 
      </configuration> 
      </plugin> 

If you need to re-use this JAR in another project, the recommended approach is to move the classes to a separate module that builds a 

JAR,然后宣布对JAR依赖从你的web应用的 以及从任何需要它的其他项目。

If you can't move the classes to another project, you can deploy the classes and resources included in your webapp as an "attached" 

神器,用分类,使用以下配置:

<project> 
     ... 
     <artifactId>mywebapp</artifactId> 
     <version>1.0-SNAPSHOT</version> 
     ... 
     <build> 
     <plugins> 
      <plugin> 
      <artifactId>maven-war-plugin</artifactId> 
      <version>3.1.0</version> 
      <configuration> 
       <attachClasses>true</attachClasses> 
      </configuration> 
      </plugin> 
     </plugins> 
     </build> 
     ... 
    </project> 

This will result in two artifacts being deployed: mywebapp-1.0-SNAPSHOT.war and mywebapp-1.0-SNAPSHOT-classes.jar.