Javax.servlet.ServletException: java.lang.NoClassDefFoundError: org / apache / commons / pool / KeyedObjectPoolFactory

I am trying to establish a connection to an Oracle database using BasicDataSource using DBCP .

I downloaded commons-dbcp-1.4-bin.zip from here .

There are three jar files in the classpath.

  • ordinary-DBHP-1,4
  • Commons-DBCD-1.4-Sources
  • Ordinary-DBHP-1,4-Javadoc

I am using the following code to establish a connection.

 import java.sql.Connection; import java.sql.PreparedStatement; import java.sql.ResultSet; import java.sql.SQLException; import org.apache.commons.dbcp.BasicDataSource; public class DatabaseConnection { private final static BasicDataSource BASIC_DATA_SOURCE = new BasicDataSource(); private final static String SQL = "SELECT * FROM admin WHERE login_id=? AND admin_pwd=?"; static { BASIC_DATA_SOURCE.setDriverClassName("oracle.jdbc.OracleDriver"); BASIC_DATA_SOURCE.setUrl("jdbc:oracle:thin:@localhost:1521:xe"); BASIC_DATA_SOURCE.setUsername("wagafashiondb"); BASIC_DATA_SOURCE.setPassword("root"); } private static Connection getConnection() throws SQLException { return BASIC_DATA_SOURCE.getConnection(); } public boolean exists(String userName, String password) throws SQLException { Connection connection = null; PreparedStatement ps = null; ResultSet rs = null; boolean exists = false; try { connection = getConnection(); ps = connection.prepareStatement(SQL); ps.setString(1, userName); ps.setString(2, password); rs = ps.executeQuery(); exists = rs.next(); } finally { try { if (connection != null) {connection.close();} if (ps != null) {ps.close();} if (rs != null) {rs.close();} } catch (SQLException e) { } } return exists; } } 

It throws the following exception,

 javax.servlet.ServletException: java.lang.NoClassDefFoundError: org/apache/commons/pool/KeyedObjectPoolFactory 

It looks like I'm using the wrong source. What could be the reason for this exception?

+2
source share
1 answer

Decision

You need to get the commons-pool library (JAR) in the classpath. The fact is indicated as commons-pool , which is the dependence of commons-dbcp on the list of project dependencies

Explanation

Basically, NoClassDefFoundError means that Java execution turned out to require a particular class that is not in the classpath. This is because classes need other classes (this is usually specified in import s). In your case (at least) one of the classes from commons-dbcp declares a dependency on org/apache/commons/pool/KeyedObjectPoolFactory .

The class name is specified using / as delimiters. If it's a common name (and not your own class), you can try to find where to get it by simply typing the full name (e.g. org/apache/commons/pool/KeyedObjectPoolFactory ) on Google.

+6
source

All Articles