cplusplusrat
cplusplusrat

Reputation: 1445

Spring 4.2 ApplicationContext NoClassDefFound Error

I have the following code.

// MessagePrinter.java
public class MessagePrinter {
    private String message;
    public MessagePrinter(String input){
        message = input;
    }
}

// ApplicationConfig.java
@Configuration
public class ApplicationConfig {

    @Bean
    public MessagePrinter getMessagePrinter(){
         return new MessagePrinter("Bean");
    }
}

// Application.java
public class Application {
    public static void main(String[] args) {    
        ApplicationContext context = new AnnotationConfigApplicationContext(ApplicationConfig.class);
    }
}

The error I get when I run Application in Eclipse is java.lang.NoClassDefFoundError. I am in the process of migrating from cplusplus to java and brand new to the spring framework. All the documentation I read online tells me that this code should work fine. My goal is to extract the MessagePrinter class from the ApplicationContext which is created in main. However, the first statement itself causes the code to crash. Any clues? I am an absolute Java noob. So feel free to point out the most obvious.

I am using Spring 4.2.7

The actual error message from Eclipse is given below.

Exception in thread "main" java.lang.NoClassDefFoundError: org/apache/commons/logging/LogFactory
at org.springframework.context.support.AbstractApplicationContext.<init>(AbstractApplicationContext.java:159)
at org.springframework.context.support.GenericApplicationContext.<init>(GenericApplicationContext.java:102)
at org.springframework.context.annotation.AnnotationConfigApplicationContext.<init>(AnnotationConfigApplicationContext.java:60)
at org.springframework.context.annotation.AnnotationConfigApplicationContext.<init>(AnnotationConfigApplicationContext.java:82)
at hello.Application.main(Application.java:8)
Caused by: java.lang.ClassNotFoundException: org.apache.commons.logging.LogFactory
at java.net.URLClassLoader.findClass(URLClassLoader.java:381)
at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:331)
at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
... 5 more

Upvotes: 0

Views: 1113

Answers (1)

Master Chief
Master Chief

Reputation: 2541

You are missing commons-logging.jar in your classpath. This jar is required by spring-core.

If your project is a maven project add this to your pom.xml in dependencies section

<!-- https://mvnrepository.com/artifact/commons-logging/commons-logging -->
<dependency>
    <groupId>commons-logging</groupId>
    <artifactId>commons-logging</artifactId>
    <!-- change version as applicable -->
    <version>1.2</version>
</dependency>

You can manually add the jars in Eclipse. Download jars from

https://mvnrepository.com/artifact/commons-logging/commons-logging/1.2

Upvotes: 1

Related Questions