Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

java.lang.ClassNotFoundException: org.apache.commons.dbcp.BasicDataSource

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>
like image 963
sneha Avatar asked Mar 10 '14 10:03

sneha


3 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.

like image 126
Sivaranjani D Avatar answered Nov 14 '22 10:11

Sivaranjani D


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.

like image 23
ATG Avatar answered Nov 14 '22 09:11

ATG


One common mistake could be importing class

org.apache.tomcat.dbcp.dbcp.BasicDataSource

instead of

org.apache.commons.dbcp.BasicDataSource

in your java code. It can be easily missed out, even in the error messages.

like image 41
soysal Avatar answered Nov 14 '22 10:11

soysal