Reputation: 51
EDIT: When I removed PROVIDED from the SPRING DEPENDENCY @ POM.XML it suddenly worked, can someone please tell me why it worked instead?
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
<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
Can someone please tell me how to resolve this? I'm at wits end :(
Upvotes: 1
Views: 1740
Reputation: 38290
maven controls you compile time classpath, but does not control your runtime classpath. 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).
Upvotes: 1