2011-05-10 67 views
1

编辑:当我从SPRING DEPENDENCY @ POM.XML删除PROVIDED它突然工作,有人可以告诉我为什么它的工作,而不是?不能运行春天你好世界继续得到NoClassDefFoundError

嗨,大家好我不断收到

Caused by: java.lang.NoClassDefFoundError: org/springframework/beans/factory/support/BeanDefinitionRegistry 

每当我试图执行

mvn exec:java -Dexec.mainClass="org.carlos.spring.FirstSpringHelloWorld" 

我的代码

的代码如下

package org.carlos.spring; 

import org.springframework.beans.factory.support.BeanDefinitionReader; 
import org.springframework.beans.factory.support.DefaultListableBeanFactory; 
import org.springframework.beans.factory.support.PropertiesBeanDefinitionReader; 
import org.springframework.core.io.ClassPathResource; 

import org.carlos.decoupled.MessageSource; 
import org.carlos.decoupled.MessageDestination; 

public class FirstSpringHelloWorld { 

    public static void main(String[] args) {   
     DefaultListableBeanFactory bf = new DefaultListableBeanFactory();  
     BeanDefinitionReader reader = new PropertiesBeanDefinitionReader(bf); 
     reader.loadBeanDefinitions(new ClassPathResource("/META-INF/spring/helloworld-context.properties")); 

     MessageSource source = (MessageSource) bf.getBean("source"); 
     MessageDestination destination = (MessageDestination) bf.getBean("destination"); 

     destination.write(source.getMessage());  
    } 
} 

package org.carlos.decoupled; 

public interface MessageSource { 
    String getMessage(); 
} 


package org.carlos.decoupled; 

public interface MessageDestination { 
    void write(String message);  
} 

,这是我的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/maven-v4_0_0.xsd"> 
    <modelVersion>4.0.0</modelVersion> 
    <groupId>org.carlos.hello</groupId> 
    <artifactId>hello</artifactId> 
    <packaging>jar</packaging> 
    <version>1.0</version> 
    <name>hello Maven Webapp</name> 
    <url>http://maven.apache.org</url> 
    <dependencies> 
    <dependency> 
     <groupId>junit</groupId> 
     <artifactId>junit</artifactId> 
     <version>3.8.1</version> 
     <scope>test</scope> 
    </dependency> 
    <dependency> 
     <groupId>javax.servlet</groupId> 
     <artifactId>servlet-api</artifactId> 
     <version>2.4</version> 
     <scope>provided</scope> 
    </dependency> 
    <dependency> 
     <groupId>javax.servlet.jsp</groupId> 
     <artifactId>jsp-api</artifactId> 
     <version>2.0</version> 
     <scope>provided</scope> 
    </dependency> 
    <dependency> 
     <groupId>org.springframework</groupId> 
     <artifactId>spring-beans</artifactId> 
     <version>2.5.5</version> 
     <scope>provided</scope>  
    </dependency> 
    <dependency> 
     <groupId>org.springframework</groupId> 
     <artifactId>spring-core</artifactId> 
     <version>2.5.5</version> 
     <scope>provided</scope>  
    </dependency> 
    </dependencies> 
    <build> 
    <finalName>hello</finalName> 
    <plugins> 
     <plugin> 
     <artifactId>maven-compiler-plugin</artifactId> 
     <configuration> 
      <source>1.5</source> 
      <target>1.5</target> 
     </configuration> 
     </plugin> 
     <plugin> 
     <groupId>org.mortbay.jetty</groupId> 
     <artifactId>maven-jetty-plugin</artifactId> 
     </plugin> 
     <plugin> 
     <artifactId>maven-assembly-plugin</artifactId> 
     <configuration> 
      <descriptorRefs> 
      <descriptorRef>jar-with-dependencies</descriptorRef> 
      </descriptorRefs> 
     </configuration> 
     </plugin> 
    </plugins> 
    </build> 
</project> 

hello-world-context.properties:

source.(class)=org.carlos.decoupled.SimpleMessageSource 
destination.(class)=org.carlos.decoupled.StdoutMessageDestination 

可有人请告诉我如何解决这个问题?我很有趣:(

+0

尝试在其中创建一个罐子,它将使所有的lib在运行时也可以使用 – 2011-05-10 12:23:27

+0

使用mvn instal assembly:assembly?我很新MVN请详细说明ty! – Jimmy 2011-05-10 12:32:22

回答

1

maven控制你编译时间类路径,但不控制你的运行时类路径。检查你的运行时类路径(通常是一个名为CLASSPATH的环境变量),以确保所有的依赖关系在运行时classpath(包括弹簧罐)

+0

我从http://www.sonatype.com/books/mvnex-book/reference/customizing.html获得了另一个示例,我如何使用mvn exec直接运行该应用程序,即使它依赖于velocity,jaxen,dom4j和log4j? – Jimmy 2011-05-10 12:31:35

+0

速度,jaxen,dom4j和log4j al准备在你的执行类路径中吗? – DwB 2011-05-10 13:03:43

+0

忘了编译w/dependencies .. – Jimmy 2011-05-17 10:37:28