GoogleSearchBox

Custom Search

Friday, June 7, 2013

Cannot load JDBC driver class com.mysql.jdbc.Driver java.lang.ClassNotFoundException: com.mysql.jdbc.Driver Error while setting up CAS for SSO functionality

Basically I got below exceptions while setting up CAS server for single sign on (SSO) functionality:

Cannot load JDBC driver class 'com.mysql.jdbc.Driver'
java.lang.ClassNotFoundException: com.mysql.jdbc.Driver
        at org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1680)
        at org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1526)
        at org.apache.commons.dbcp.BasicDataSource.createConnectionFactory(BasicDataSource.java:1420)
...
        at org.springframework.jdbc.core.JdbcTemplate.queryForObject(JdbcTemplate.java:745)
        at org.springframework.jdbc.core.JdbcTemplate.queryForInt(JdbcTemplate.java:776)
        at org.springframework.jdbc.core.simple.SimpleJdbcTemplate.queryForInt(SimpleJdbcTemplate.java:118)
        at org.jasig.cas.adaptors.jdbc.SearchModeSearchDatabaseAuthenticationHandler.authenticateUsernamePasswordInternal(SearchModeSearchDatabaseAuthenticationHandler.java:59)
.
.
Cannot load JDBC driver class 'com.mysql.jdbc.Driver'

java.lang.ClassNotFoundException: com.mysql.jdbc.Driver
.
.
.
        at org.springframework.jdbc.core.simple.SimpleJdbcTemplate.queryForInt(Simp2013-06-06 06:58:37,606 ERROR [org.jasig.cas.authentication.AuthenticationManagerImpl] - <org.jasig.cas.adaptors.jdbc.SearchModeSearchDatabaseAuthenticationHandler threw error authenticating [username: scott]>

org.springframework.jdbc.CannotGetJdbcConnectionException: Could not get JDBC Connection; nested exception is org.apache.commons.dbcp.SQLNestedException: Cannot load JDBC driver class 'com.mysql.jdbc.Driver'
 at org.springframework.jdbc.datasource.DataSourceUtils.getConnection(DataSourceUtils.java:80)


Basically you got above error while deploying CAS server for SSO functionality with Java Application like me.

These reason is that you you don't have the mysql jdbc driver jar file available for the CAS server application at CAS's /WEB-INF/lib/  directory.

So follow below steps (if you are using plain JDBC connection through mysql):

Step 1.

Grab a mysql-connector-java jar,  I have used mysql-connector-java-5.1.18-bin.jar    where my cas server environment is:  cas-server-3.5.2, Tomcat 6.x, Java 1.6 and Maven 2.1

Step 2.

Now Copy above jar to your CAS server's Workspace Build Location (e.g. /opt/cas-workspace/local-cas/src/main/webapp/WEB-INF/lib/ )   Or directly to  your CAS server's deployed location (e.g.  /usr/local/apache-tomcat-6.0.37/webapps/cas/WEB-INF/lib).

Difference between these two location is that, the later pics jars from the former's path while build.

So if you put the jar directly on later path, chances are that when you do a new build those jars will be deleted, as you have not put those jars in the source build location (the Former directory location).

If you are following CAS Maven2 WAR Overlay way to build the CAS.

Step 3.

Now do a maven build (assuming you are in your workspace home build location  e.g. /opt/cas-workspace/local-cas/      where you have the  pom.xml  file):

$ mvn clean package

Step 4.

Before starting the tomcat, delete the  "cas" directory (war archived) created at /opt/cas-workspace/local-cas/src/main/webapp/  (This is actually my Path ;) ) or your similar path as per your project settings.

$ rm -rf cas

Step 5.

Test your application and check the logs, you should not get mentioned errors above. It should have been solved. And you should be able to authenticate uses corresponding to your MySql database users table.

Follow below steps (if you are using Spring JDBC connection):

Step1.

Edit your pom.xml file to have the spring-jdbc dependency inside  <dependencies>  tag as below:

<dependency>

<groupId>org.springframework</groupId>

<artifactId>spring-jdbc</artifactId>

<version>${spring.version}</version>

</dependency>

Also add (inside  <properties>  tag) if not there:

<spring.version>3.1.1.RELEASE</spring.version>  <!--  Edit this version to match your spring requirement  -->

Step2.

Now do a maven build (assuming you are in your workspace home build location  e.g. /opt/cas-workspace/local-cas/      where you have the  pom.xml  file):

$ mvn clean package

Step3.

Before starting the tomcat, delete the  "cas" directory (war archived) created at /opt/cas-workspace/local-cas/src/main/webapp/  (This is actually my Path ;) ) or your similar path as per your project settings.

$ rm -rf cas

Step4.

Test your application and check the logs, you should not get mentioned errors above.