0

I am new to Spring framework. I checked the following answer regarding the similar problem as i have :Getting java.lang.ClassNotFoundException: org.apache.commons.logging.LogFactory exception But,i have tried all the solutions mentioned in that link,None worked.So,i request not to close this post/mark duplicate.

I have been using the the JavaBrains Spring youtube lectures and running the same code as mentioned in the below lecture-4 link

YoutubeLinkForCode

Below is the main class as mentioned in above youtube lecture :

 package org.learningSpring;


import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;


public class DrawingApp {

    Log logger =  LogFactory.getLog(DrawingApp.class);
    public static void main(String[] args) {
              ApplicationContext context=new ClassPathXmlApplicationContext("org/learningSpring/Spring.xml");


            Triangle triangle=(Triangle) context.getBean("triangle");

            triangle.draw();

            ((ClassPathXmlApplicationContext) context).close();

        }

    }

Rest of the code is same as in the link :YoutubeLinkForCode

I am not using Maven for this & added all Jars manually.

Jars used in project :

Below is the stack trace from exception:

Exception in thread "main" java.lang.NoClassDefFoundError: org/apache/commons/logging/LogFactory
    at org.springframework.context.support.AbstractApplicationContext.<init>(AbstractApplicationContext.java:160)
    at org.springframework.context.support.AbstractApplicationContext.<init>(AbstractApplicationContext.java:224)
    at org.springframework.context.support.AbstractRefreshableApplicationContext.<init>(AbstractRefreshableApplicationContext.java:88)
    at org.springframework.context.support.AbstractRefreshableConfigApplicationContext.<init>(AbstractRefreshableConfigApplicationContext.java:58)
    at org.springframework.context.support.AbstractXmlApplicationContext.<init>(AbstractXmlApplicationContext.java:61)
    at org.springframework.context.support.ClassPathXmlApplicationContext.<init>(ClassPathXmlApplicationContext.java:136)
    at org.springframework.context.support.ClassPathXmlApplicationContext.<init>(ClassPathXmlApplicationContext.java:83)
    at org.deen.DrawingApp.main(DrawingApp.java:14)

I debug and found that the code throws exception in AbstractApplicationContext.class at below line:

public AbstractApplicationContext() {
        this.logger = LogFactory.getLog(super.getClass()); //here 

I made sure if there is LogFactory.class in commons-logging-1.2 jars and whether this class contains getLog method.

Below is my .classpath file.This contains the commons jar :

<?xml version="1.0" encoding="UTF-8"?>
<classpath>
    <classpathentry kind="src" path="src"/>
    <classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/CDC-1.1%Foundation-1.1"/>
    <classpathentry kind="con" path="org.eclipse.jdt.USER_LIBRARY/Spring"/>
    <classpathentry kind="lib" path="D:/Software/JAR/commons-logging-1.2-bin (2)/commons-logging-1.2/commons-logging-1.2.jar"/>
    <classpathentry kind="output" path="bin"/>
</classpath>

I read some answers on stackoverflow regarding this error and all they say is to add commons jar which i already added.

I have spent whole day trying to solve this error & feeling demoralised.I would be very much thankful if someone could help me to resolve this error.If you need any more detail then please let me know.

Community
  • 1
  • 1
Deen John
  • 3,522
  • 4
  • 29
  • 32
  • 1
    How you added the jar files? Are you using any IDE? You can place these lines in DrawingApp class, `Logger logger = LogFactory.getLog(DrawingApp.class)`, If you are getting compiler error in this line, then commons jar is not in the classpath of your application. – Sundararaj Govindasamy Sep 08 '16 at 22:08
  • I am using Eclipse IDE and yes,the code you mentioned compiles fine :import java.util.logging.Logger; import org.apache.commons.logging.LogFactory; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; public class DrawingApp { Logger logger = (Logger) LogFactory.getLog(DrawingApp.class); public static void main(String[] args) { – Deen John Sep 08 '16 at 22:10
  • Why you are importing import java.util.logging.Logger instead commons log or logger? – Sundararaj Govindasamy Sep 08 '16 at 22:12
  • For using commons logger, I have to change the code you mentioned to : Log logger = LogFactory.getLog(DrawingApp.class); This compiles fine too.Still getting the same : Exception in thread "main" java.lang.NoClassDefFoundError: org/apache/commons/logging/LogFactory – Deen John Sep 08 '16 at 22:16
  • check the .classpath file of eclipse for commons jar.NoClassDefFoundError will come if a class was present during compile time but not available in java classpath during runtime. – Sundararaj Govindasamy Sep 08 '16 at 22:28
  • this is my .classpath file.This contains the common jar : – Deen John Sep 08 '16 at 22:30
  • 1
    If the answer proposed by our gentile moderator at the time of closing your question does not satisfy you, remember to check the **execution classpath**: When you click on "Run as...", check that the "classpath" tab has all the needed JARs. – Little Santi Sep 08 '16 at 23:12
  • @LittleSanti: big big thanks :D that was the issue.Despite adding jar in classpath and .classpath showing the jar is added ,i was getting exception.but with your advice, i tried "Run as ...then run configuration..added jar " and voila ! it worked :) no idea how ,but it worked :D – Deen John Sep 08 '16 at 23:47
  • 1
    @JarrodRoberson You've been too much rushed at marking this question as duplicated, as it was not. The right answer is the one I gave in my previous comment. – Little Santi Sep 08 '16 at 23:56
  • @Naruto_Uzumaki You are welcome. – Little Santi Sep 08 '16 at 23:56

0 Answers0