Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

Iam doing a project on spring MVC with maven.Iam getting an error java.lang.ClassNotFoundException: org.apache.commons.dbcp.BasicDataSource while running project.I include all the dependencies i think..below my codes.waiting for your reply

WEB.xml

  <?xml version="1.0" encoding="UTF-8"?>
  <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" version="3.0">
  <servlet>
 <servlet-name>AccPerSpring</servlet-name>
 <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
 <init-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>/WEB-INF/servlet-context.xml</param-value>
 </init-param>
    <load-on-startup>1</load-on-startup>
 </servlet>
    <servlet-mapping>
       <servlet-name>AccPerSpring</servlet-name>
       <url-pattern>/</url-pattern>
    </servlet-mapping>
 </web-app>

servlet-context.xml

  <?xml version="1.0" encoding="UTF-8"?>
 <beans xmlns="http://www.springframework.org/schema/beans" 
        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xmlns:mvc="http://www.springframework.org/schema/mvc"              xmlns:beans="http://www.springframework.org/schema/beans"
  xmlns:context="http://www.springframework.org/schema/context"   xmlns:tx="http://www.springframework.org/schema/tx"
  xsi:schemaLocation="http://www.springframework.org/schema/mvc    http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.2.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.2.xsd">

   <!-- Enable @Controller annotation support -->
   <mvc:annotation-driven />


   <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
         <property name="prefix" value="/WEB-INF/" />
         <property name="suffix" value=".jsp" />
   </bean>
  <context:component-scan base-package="com.gerrytan.pizzashop"/>
  <bean id="myDataSource" class=" org.apache.commons.dbcp.BasicDataSource" destroy-method="close"> 
       <property name="driverClassName" value="com.mysql.jdbc.Driver"/>
       <property name="url" value="jdbc:mysql://localhost:3306/mydatabase"/>
       <property name="username" value="root"/>
       <property name="password" value="kca@fnpl#12"/>
 </bean>

     <!-- Hibernate Session Factory -->
    <bean id="mySessionFactory"class="org.springframework.orm.hibernate4.LocalSessionFactoryBean>
    <property name="dataSource" ref="myDataSource"/>
    <property name="packagesToScan">
    <array>
     <value>com.gerrytan.pizzashop</value>
    </array>
   </property>
    <property name="hibernateProperties">
   <value>
    hibernate.dialect=org.hibernate.dialect.MySQLDialect
   </value>
   </property>
   </bean>

    <!-- Hibernate Transaction Manager -->
    <bean id="transactionManager"    class="org.springframework.orm.hibernate4.HibernateTransactionManager">
    <property name="sessionFactory" ref="mySessionFactory"/>
   </bean>

   <mvc:resources mapping="/resources/**" location="/WEB-INF/resources/" />
   <!-- Activates annotation based transaction management -->
   <tx:annotation-driven transaction-manager="transactionManager"/>
   </beans>

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/xsd/maven-4.0.0.xsd">
     <modelVersion>4.0.0</modelVersion>
     <groupId>AccPerSpring</groupId>
     <artifactId>AccPerSpring</artifactId>
      <version>0.0.1-SNAPSHOT</version>
     <packaging>war</packaging>

     <properties>
     <spring.version>3.2.3.RELEASE</spring.version>
      <hibernate.version>4.2.2.Final</hibernate.version>
      </properties>

    <dependencies>
     <!-- Spring -->
         <dependency>
         <groupId>org.springframework</groupId>
         <artifactId>spring-context</artifactId>
         <version>${spring.version}</version>
        </dependency>
     <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-webmvc</artifactId>
      <version>${spring.version}</version>
    </dependency>
   <dependency>
     <groupId>org.springframework</groupId>
     <artifactId>spring-orm</artifactId>
     <version>${spring.version}</version>
   </dependency>

<!-- Hibernate -->
<dependency>
  <groupId>org.hibernate</groupId>
  <artifactId>hibernate-core</artifactId>
  <version>${hibernate.version}</version>
</dependency>

<!-- Java EE -->
<dependency>
  <groupId>javax.servlet</groupId>
  <artifactId>servlet-api</artifactId>
  <version>2.5</version>
  <scope>provided</scope>
</dependency>
<dependency>
  <groupId>jstl</groupId>
  <artifactId>jstl</artifactId>
  <version>1.2</version>
</dependency>

<!-- Others -->
<dependency>
  <groupId>commons-dbcp</groupId>
  <artifactId>commons-dbcp</artifactId>
  <version>1.4</version>
</dependency>
<dependency>
  <groupId>mysql</groupId>
  <artifactId>mysql-connector-java</artifactId>
  <version>5.1.25</version>
</dependency>
<dependency>
  <groupId>log4j</groupId>
  <artifactId>log4j</artifactId>
  <version>1.2.14</version>
</dependency>

 </dependencies>

   <build>
    <plugins>
     <plugin>
       <groupId>org.apache.maven.plugins</groupId>
         <artifactId>maven-compiler-plugin</artifactId>
         <version>3.1</version>
       <configuration>
        <source>1.7</source>
        <target>1.7</target>
       </configuration>
     </plugin>
     </plugins>
     </build>
  </project>
share|improve this question
1  
Is this jar in your classpath commons-dbcp.jar ?? check –  Naren Mar 10 at 10:08
    
ya sure..but not working –  sneha Mar 10 at 10:16
    
commons-dbcp-1.4.jar commons-dbcp-1.4-javadoc.jar commons-dbcp-1.4-sources.jar have these 3 jars –  sneha Mar 10 at 10:17
    
Are you deploying into Tomcat, by any chance? –  ATG Mar 10 at 10:26
1  
I've just noticed you have a leading space in the "class" attribute of your declaration of the "myDataSource" bean. I'll be surprised if this is the problem, but probably worth eliminating it just in case! –  ATG Mar 10 at 10:37
show 3 more comments

2 Answers

Once the build completes check in your war file-> WEB-INF/lib for commons-dbcp-1.4.jar. If it is available in the class path then your issue will be solved. One more thing is, open your commons-dbcp-1.4.jar and check that particular class(org.apache.commons.dbcp.BasicDataSource) is available or not? if not you have to try some other versions of commons-dbcp-1.4.jar.

share|improve this answer
    
1.4 jar contain BasicDataSource class..then how –  sneha Mar 10 at 10:29
add comment

Sometimes you get these strange exceptions when you have conflicting versions of the same class. This can happen because a different version of the same JAR is already on the application server's classpath, or if it's been included via a different dependency.

mvn dependency:tree is a command you can run to see all the dependencies that come into your application, including those that get brought in from other JARs. It might be worth running this to check for other "dbcp" JARs that may be causing a conflict.

It's also worth checking your "lib" folder in the application server into which you're deploying to see if there's any "dbcp" JAR in there already. If this is the case, then changing the scope to "provided" for the "commons-dbcp" artefact should fix the problem.

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.