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

I am doing a spring MVC project with maven.Iam receives the java.lang.ClassNotFoundException: org.apache.commons.dbcp.BasicDataSource error message when the project starts. I include all the dependencies that I think ... below my codes waiting for your answer

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> 
+8
java spring spring-mvc maven
source share
3 answers

As soon as the assembly completes the check in your military file-> WEB-INF / lib for commons-dbcp-1.4.jar. If it is available on the way to the class, your problem will be solved. One more thing: open your commons-dbcp-1.4.jar and make sure that this class (org.apache.commons.dbcp.BasicDataSource) is available or not? if not, you should try other versions of commons-dbcp-1.4.jar.

+5
source share

Sometimes you get these strange exceptions when you have conflicting versions of the same class. This may be due to the fact that another version of the JAR is already on the path to the application server application class, or if it was included through another dependency.

mvn dependency:tree is a command that you can run to see all the dependencies that go into your application, including those that fall into other JARs. It might be worth running this to check for other "dbcp" JAR files that might cause a conflict.

It is also worth checking your "lib" folder on the application server you are deploying to to see if there is another "dbcp" JAR file there. If so, then changing the scope provided is for the commons-dbcp artifact should fix the problem.

+3
source share

One common error might be importing a class.

org.apache.tomcat.dbcp.dbcp.BasicDataSource

instead

org.apache.commons.dbcp.BasicDataSource

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

+1
source share

All Articles