Announcement Announcement Module
Collapse
No announcement yet.
Simple Spring Application Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • Simple Spring Application

    Hi every body,

    I am new in spring(please also recommend me the useful tutorial). I have made a very simple spring application. When I ran this application then I got the following error

    Exception in thread "main" java.lang.NoClassDefFoundError: org/apache/commons/logging/LogFactory

    I first made the class Name behaving as a business object.

    package examples;

    public class Namer {
    private String name;

    public String getName() {
    return name;
    }
    public void setName(String name) {
    this.name = name;
    }
    }


    And then I wrote the class for main, and for specifying the resources


    package examples;

    import org.springframework.beans.factory.*;
    import org.springframework.beans.factory.xml.*;
    import org.springframework.core.io.*;

    public class SimpleSpringApp {

    public static void main(String args[]){

    Resource namerXmlFile = new FileSystemResource("src/resources/namer.xml");
    BeanFactory factory = new XmlBeanFactory(namerXmlFile);
    Namer namer = (Namer)factory.getBean("namerId");
    System.out.println(namer.getName());
    }
    }



    And this is my namer.xml file


    <?xml version="1.0" encoding="UTF-8"?>

    <beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd">

    <bean id = "namerId"
    class = "examples.Namer">
    <property name = "name">
    <value>Javabeat</value>
    </property>
    </bean>

    </beans>

    Thanks in advance

  • #2
    Please use [ code][/code ] tags when posting.

    Your exception indicatates (obviously) that a class cannot be found. Including commons-logging.jar in your classpath. A tip next time use google on the missing class and you probably get a solution/jar.

    Comment


    • #3
      Thanks but further errors

      Originally posted by mdeinum View Post
      Please use [ code][/code ] tags when posting.

      Your exception indicatates (obviously) that a class cannot be found. Including commons-logging.jar in your classpath. A tip next time use google on the missing class and you probably get a solution/jar.

      Thanks for providing me a solution and special for tip. But I am getting now the following error. While I have inlcluded the log4j liberary And also provided the bean defintion and you can check please where is the mistake?


      log4j:WARN No appenders could be found for logger (org.springframework.core.CollectionFactory).
      log4j:WARN Please initialize the log4j system properly.
      Exception in thread "main" org.springframework.beans.factory.BeanDefinitionSt oreException: Line 6 in XML document from file [e:\Dokumente und Einstellungen\praktikum\workspace\simplespring\src \resources\namer.xml] is invalid; nested exception is org.xml.sax.SAXParseException: cvc-elt.1: Cannot find the declaration of element 'beans'.
      org.xml.sax.SAXParseException: cvc-elt.1: Cannot find the declaration of element 'beans'.
      at com.sun.org.apache.xerces.internal.util.ErrorHandl erWrapper.createSAXParseException(Unknown Source)
      at com.sun.org.apache.xerces.internal.util.ErrorHandl erWrapper.error(Unknown Source)
      at com.sun.org.apache.xerces.internal.impl.XMLErrorRe porter.reportError(Unknown Source)
      at com.sun.org.apache.xerces.internal.impl.XMLErrorRe porter.reportError(Unknown Source)
      at com.sun.org.apache.xerces.internal.impl.xs.XMLSche maValidator.handleStartElement(Unknown Source)
      at com.sun.org.apache.xerces.internal.impl.xs.XMLSche maValidator.startElement(Unknown Source)
      at com.sun.org.apache.xerces.internal.impl.XMLNSDocum entScannerImpl.scanStartElement(Unknown Source)
      at com.sun.org.apache.xerces.internal.impl.XMLNSDocum entScannerImpl$NSContentDispatcher.scanRootElement Hook(Unknown Source)
      at com.sun.org.apache.xerces.internal.impl.XMLDocumen tFragmentScannerImpl$FragmentContentDispatcher.dis patch(Unknown Source)
      at com.sun.org.apache.xerces.internal.impl.XMLDocumen tFragmentScannerImpl.scanDocument(Unknown Source)
      at com.sun.org.apache.xerces.internal.parsers.XML11Co nfiguration.parse(Unknown Source)
      at com.sun.org.apache.xerces.internal.parsers.XML11Co nfiguration.parse(Unknown Source)
      at com.sun.org.apache.xerces.internal.parsers.XMLPars er.parse(Unknown Source)
      at com.sun.org.apache.xerces.internal.parsers.DOMPars er.parse(Unknown Source)
      at com.sun.org.apache.xerces.internal.jaxp.DocumentBu ilderImpl.parse(Unknown Source)
      at org.springframework.beans.factory.xml.XmlBeanDefin itionReader.doLoadBeanDefinitions(XmlBeanDefinitio nReader.java:357)
      at org.springframework.beans.factory.xml.XmlBeanDefin itionReader.loadBeanDefinitions(XmlBeanDefinitionR eader.java:308)
      at org.springframework.beans.factory.xml.XmlBeanDefin itionReader.loadBeanDefinitions(XmlBeanDefinitionR eader.java:283)
      at org.springframework.beans.factory.xml.XmlBeanFacto ry.<init>(XmlBeanFactory.java:73)
      at org.springframework.beans.factory.xml.XmlBeanFacto ry.<init>(XmlBeanFactory.java:61)
      at examples.SimpleSpringApp.main(SimpleSpringApp.java :12)

      Comment


      • #4
        You have 2 unrelated issues
        1. log4j needs configuration file - log4j properties or log4j.xml.
        2. you have an error in your namer.xml or (less likely but possible) you use quite outdated SAX parser. Which JDK are you using? Do you have some XML-related libraries on your classpath (that did not came with JDK)?

        For the 1st problem just put log4j.properties in your classpath with following contents
        Code:
        log4j.rootLogger=debug, console,rollingFiler 
        
        log4j.appender.console=org.apache.log4j.ConsoleAppender
        log4j.appender.console.layout=org.apache.log4j.SimpleLayout
        log4j.appender.console.Threshold=INFO
        
        log4j.appender.rollingFiler=org.apache.log4j.RollingFileAppender
        log4j.appender.rollingFiler.File=namer.log
        log4j.appender.rollingFiler.MaxFileSize=5MB
        log4j.appender.rollingFiler.MaxBackupIndex=3
        log4j.appender.rollingFiler.layout=org.apache.log4j.PatternLayout
        log4j.appender.rollingFiler.layout.ConversionPattern=%-7p %d{ISO8601} %c{2} - %m%n
        Then you may read log4j documentation and refine the contents according to your needs.

        If your application is web one, then you may want take a look on javadoc for org.springframework.web.util.Log4jConfigListener (and related classes).

        Concerning 2nd probleim nothing is possible to say without looking on your XML. Just post it here (do not forget CODE tags!).


        Originally posted by ahmadgee View Post
        Thanks for providing me a solution and special for tip. But I am getting now the following error. While I have inlcluded the log4j liberary And also provided the bean defintion and you can check please where is the mistake?


        log4j:WARN No appenders could be found for logger (org.springframework.core.CollectionFactory).
        log4j:WARN Please initialize the log4j system properly.
        Exception in thread "main" org.springframework.beans.factory.BeanDefinitionSt oreException: Line 6 in XML document from file [e:\Dokumente und Einstellungen\praktikum\workspace\simplespring\src \resources\namer.xml] is invalid; nested exception is org.xml.sax.SAXParseException: cvc-elt.1: Cannot find the declaration of element 'beans'.
        org.xml.sax.SAXParseException: cvc-elt.1: Cannot find the declaration of element 'beans'.
        at com.sun.org.apache.xerces.internal.util.ErrorHandl erWrapper.createSAXParseException(Unknown Source)
        at com.sun.org.apache.xerces.internal.util.ErrorHandl erWrapper.error(Unknown Source)
        at com.sun.org.apache.xerces.internal.impl.XMLErrorRe porter.reportError(Unknown Source)
        at com.sun.org.apache.xerces.internal.impl.XMLErrorRe porter.reportError(Unknown Source)
        at com.sun.org.apache.xerces.internal.impl.xs.XMLSche maValidator.handleStartElement(Unknown Source)
        at com.sun.org.apache.xerces.internal.impl.xs.XMLSche maValidator.startElement(Unknown Source)
        at com.sun.org.apache.xerces.internal.impl.XMLNSDocum entScannerImpl.scanStartElement(Unknown Source)
        at com.sun.org.apache.xerces.internal.impl.XMLNSDocum entScannerImpl$NSContentDispatcher.scanRootElement Hook(Unknown Source)
        at com.sun.org.apache.xerces.internal.impl.XMLDocumen tFragmentScannerImpl$FragmentContentDispatcher.dis patch(Unknown Source)
        at com.sun.org.apache.xerces.internal.impl.XMLDocumen tFragmentScannerImpl.scanDocument(Unknown Source)
        at com.sun.org.apache.xerces.internal.parsers.XML11Co nfiguration.parse(Unknown Source)
        at com.sun.org.apache.xerces.internal.parsers.XML11Co nfiguration.parse(Unknown Source)
        at com.sun.org.apache.xerces.internal.parsers.XMLPars er.parse(Unknown Source)
        at com.sun.org.apache.xerces.internal.parsers.DOMPars er.parse(Unknown Source)
        at com.sun.org.apache.xerces.internal.jaxp.DocumentBu ilderImpl.parse(Unknown Source)
        at org.springframework.beans.factory.xml.XmlBeanDefin itionReader.doLoadBeanDefinitions(XmlBeanDefinitio nReader.java:357)
        at org.springframework.beans.factory.xml.XmlBeanDefin itionReader.loadBeanDefinitions(XmlBeanDefinitionR eader.java:308)
        at org.springframework.beans.factory.xml.XmlBeanDefin itionReader.loadBeanDefinitions(XmlBeanDefinitionR eader.java:283)
        at org.springframework.beans.factory.xml.XmlBeanFacto ry.<init>(XmlBeanFactory.java:73)
        at org.springframework.beans.factory.xml.XmlBeanFacto ry.<init>(XmlBeanFactory.java:61)
        at examples.SimpleSpringApp.main(SimpleSpringApp.java :12)

        Comment


        • #5
          Thanks

          Now the problem has been solved and you said right that I was using quite outdated parser. Thanks best regards.

          Comment

          Working...
          X