简体   繁体   English

无法运行Spring Hello World并持续收到NoClassDefFoundError

[英]Can't run Spring Hello World keep getting NoClassDefFoundError

EDIT: When I removed PROVIDED from the SPRING DEPENDENCY @ POM.XML it suddenly worked, can someone please tell me why it worked instead? 编辑:当我从SPD DEPENDENCY @ POM.XML中删除PROVIDED时 ,它突然起作用了,有人可以告诉我为什么它起作用了吗?

Hi guys I keep getting 大家好,我不断

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

Whenever I try to execute 每当我尝试执行

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

my code 我的代码

The code is as follows 代码如下

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);    
}

and this is my POM.XML 这是我的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: hello-world-context.properties:

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

Can someone please tell me how to resolve this? 有人可以告诉我如何解决吗? I'm at wits end :( 我机智:(

maven controls you compile time classpath, but does not control your runtime classpath. Maven控制您编译时间类路径,但不控制您的运行时类路径。 Check your runtime classpath (often an environment variable named CLASSPATH) to make sure that all the dependancies are in the runtime classpath (including the spring jars). 检查您的运行时类路径(通常是一个名为CLASSPATH的环境变量),以确保所有依赖项都在运行时类路径中(包括spring jars)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM