• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1315
  • Last Modified:

Struts data-source for MYSQL - CLASSPATH PROBLEM --

Hi guys,

 Well i am trying to deploy a struts app using mysql as db. Well iam getting following error..

org.apache.commons.dbcp.SQLNestedException: Cannot load JDBC driver class 'com.mysql.jdbc.Driver'

I did following steps..
 Unzip Tomcat ---- result ... C:\jakarta-tomcat-5.0.27\
  has mysql -- in localhost with username="root" password=""


Placed my My Struts.War file in web apps folder which as Web-Inf\lib folder (has all files related to struts.jar file...(i.e, commons-digester.jar, struts.jar, mysql-connector-java-3.0.16-ga-bin.jar, etc..., )

Which means i placed mysql connector .jar file in WEB-INF/lib folder...

I set the classpath as
C:\jakarta-tomcat-5.0.27>SET PATH=%PATH%;C:\Program Files\Java\j2sdk1.5.0\bin
C:\jakarta-tomcat-5.0.27>SET JAVA_HOME=C:\Program Files\Java\j2sdk1.5.0
C:\jakarta-tomcat-5.0.27>SET PATH=%PATH%;C:\jakarta-tomcat-5.0.27\bin
C:\jakarta-tomcat-5.0.27>SET CATALINA_HOME=C:\jakarta-tomcat-5.0.27


Now when i start tomcat i get the error

org.apache.commons.dbcp.SQLNestedException: Cannot load JDBC driver class 'com.mysql.jdbc.Driver'
Caused by: java.lang.ClassNotFoundException: com.mysql.jdbc.Driver
at org.apache.commons.dbcp.BasicDataSource.createDataSource(BasicDataSource.java:766)
at org.apache.commons.dbcp.BasicDataSource.createDataSource(BasicDataSource.java:766)

I know if i keep the mysql connector .jar in common/lib it will work.. BUT I DONT WANT TO KEEP in coomon/lib as my hosting provider may not accept to keep it in his lib directory...

So iam trying to place in MY-WEB-APP/WEB-INF/LIB directory..

SO I GET THIS ERROR..

MY STRUTS-CONFIG FILE has data source like this

<data-sources>
            <data-source type="org.apache.commons.dbcp.BasicDataSource">
                  <set-property property="driverClassName" value="com.mysql.jdbc.Driver" />
                  <set-property property="url" value="jdbc:mysql://localhost:3306/mail" />
                  <set-property property="username" value="root" />
                  <set-property property="password" value="" />
            </data-source>
      </data-sources>

Regards

Lenin Chalasani




0
lensi
Asked:
lensi
  • 7
  • 6
  • 3
1 Solution
 
fargoCommented:
"org.gjt.mm.mysql.Driver" is the correct mysql driver class name
0
 
fargoCommented:
which mysql database version u are using? mysql 5 or 4 ?
0
 
lensiAuthor Commented:
Dude i hope o got it..

Well iam using mysql-4.1.8-win .. Morever as u said i tried using that class also.. Anyway the one you have present is just an alternative for com.mysql.jdbc.Driver.. They are used for backward compatibility with third party vendors liek Eclipse plusgins etc..,

Anyway i tried in that way i got the same error..

I was reading apache tomcat documentation here

http://tomcat.apache.org/tomcat-5.0-doc/jndi-datasource-examples-howto.html

SPECIFICALLY  SAYS THE BELOW LINES........

These jar files along with your the jar file for your JDBC driver should be installed in $CATALINA_HOME/common/lib.

    NOTE:Third Party drivers should be in jarfiles, not zipfiles. Tomcat only adds $CATALINA_HOME/common/lib/*.jar to the classpath.

    NOTE: Do not install these jarfiles in your /WEB-INF/lib, or $JAVA_HOME/jre/lib/ext, or anywhere else. You will experience problems if you install them anyplace other than $CATALINA_HOME/common/lib.

0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
lensiAuthor Commented:
SO NOW AY YOU CANNOT KEEP Third Part MYSQL -JAVA Drivers in WEB-INF Folder.. It works only if you keep it in commons/lib folder..


I HOPE IAM Right and continuing my work.. If anybody has a solution which works mysql-java driver  mysql-connector-java-3.0.16-ga-bin.jar in WEB-INF/lib folder..  THEN DO REPLY .......


REGARDS

LENIN CHALASANI

0
 
fargoCommented:
It's not at all required to place them in common/lib. The way you are doing is absoloutely fine.
just for cross check...is  Web-Inf\lib the directory OR WEB-INF/lib ??

be careful...it's case sensitive for WEB-INF.

I agree with driver class name.
0
 
lensiAuthor Commented:

  ITS WEB-INF

0
 
fargoCommented:
it seems to be a classpath issue OR the connector jar. Have u downloaded the connector from the mysql site?
Moreover, as the docs said..if u keep the connector*.jar in common/lib does it work??
0
 
TimYatesCommented:
>> "org.gjt.mm.mysql.Driver" is the correct mysql driver class name

No, both are valid

http://dev.mysql.com/downloads/connector/j/3.1.html

Have a look inside the mysql-connector-java-3.1.12-bin.jar file inside that tgz

Tim
0
 
TimYatesCommented:
Don't use the ga jar file:

    mysql-connector-java-3.0.16-ga-bin.jar

use

    mysql-connector-java-3.1.12-bin.jar

from that link
0
 
lensiAuthor Commented:
If i keep connector.jar in common/lib it works.. BUT NOT IN WEB-INF/lib...
0
 
TimYatesCommented:
  mysql-connector-java-3.1.12-bin.jar

not

   mysql-connector-java-3.0.16-ga-bin.jar

right?
0
 
lensiAuthor Commented:
GOT THE SAME ERROR EVEN AFTER DOWNLOADING NEW VERSION of MYSQL Connector... from teh given site http://dev.mysql.com/downloads/connector/j/3.1.html.

I KNew both are same.. even then i tried with "org.gjt.mm.mysql.Driver" and "com.mysql.jdbc.Driver" which i got same error...

f i keep connector.jar in common/lib it works.. BUT NOT IN WEB-INF/lib...

Well even i assume its classpath problem....

Where should i set classpath problem.. Since iam running through command prompt i use following statements...

SET PATH=%PATH%;C:\Program Files\Java\j2sdk1.5.0\bin
SET JAVA_HOME=C:\Program Files\Java\j2sdk1.5.0
SET PATH=%PATH%;C:\jakarta-tomcat-5.0.27\bin
SET CATALINA_HOME=C:\jakarta-tomcat-5.0.27
SET CLASSPATH=%CLASSPATH%;%CATALINA_HOME%\webapps\mailroom\WEB-INF\lib

If i dont use the last line also o get same error..

Well i use
1) jakarta-tomcat-5.0.27
2) mysql-4.1.8-win
3) mysql-connector-java-3.1.12-bin.jar

WEB-INF\lib has

C:\jakarta-tomcat-5.0.27\webapps\mailroom\WEB-INF\lib\activation.jar
C:\jakarta-tomcat-5.0.27\webapps\mailroom\WEB-INF\lib\antlr.jar
C:\jakarta-tomcat-5.0.27\webapps\mailroom\WEB-INF\lib\commons-beanutils.jar
C:\jakarta-tomcat-5.0.27\webapps\mailroom\WEB-INF\lib\commons-digester.jar
C:\jakarta-tomcat-5.0.27\webapps\mailroom\WEB-INF\lib\commons-fileupload.jar
C:\jakarta-tomcat-5.0.27\webapps\mailroom\WEB-INF\lib\commons-logging.jar
C:\jakarta-tomcat-5.0.27\webapps\mailroom\WEB-INF\lib\commons-validator.jar
C:\jakarta-tomcat-5.0.27\webapps\mailroom\WEB-INF\lib\jakarta-oro.jar
C:\jakarta-tomcat-5.0.27\webapps\mailroom\WEB-INF\lib\mail.jar
C:\jakarta-tomcat-5.0.27\webapps\mailroom\WEB-INF\lib\mysql-connector-java-3.1.12-bin.jar
C:\jakarta-tomcat-5.0.27\webapps\mailroom\WEB-INF\lib\startup.jar
C:\jakarta-tomcat-5.0.27\webapps\mailroom\WEB-INF\lib\struts.jar

COMMON/LIB has

C:\jakarta-tomcat-5.0.27\common\lib\ant-launcher.jar
C:\jakarta-tomcat-5.0.27\common\lib\ant.jar
C:\jakarta-tomcat-5.0.27\common\lib\commons-collections-2.1.1.jar
C:\jakarta-tomcat-5.0.27\common\lib\commons-dbcp-1.2.1.jar
C:\jakarta-tomcat-5.0.27\common\lib\commons-el.jar
C:\jakarta-tomcat-5.0.27\common\lib\commons-pool-1.2.jar
C:\jakarta-tomcat-5.0.27\common\lib\jasper-compiler.jar
C:\jakarta-tomcat-5.0.27\common\lib\jasper-runtime.jar
C:\jakarta-tomcat-5.0.27\common\lib\jsp-api.jar
C:\jakarta-tomcat-5.0.27\common\lib\naming-common.jar
C:\jakarta-tomcat-5.0.27\common\lib\naming-factory.jar
C:\jakarta-tomcat-5.0.27\common\lib\naming-java.jar
C:\jakarta-tomcat-5.0.27\common\lib\naming-resources.jar
C:\jakarta-tomcat-5.0.27\common\lib\servlet-api.jar

MY STruts-Config data-sources element as

<data-sources>
      <data-source type="org.apache.commons.dbcp.BasicDataSource">
            <set-property property="driverClassName" value="com.mysql.jdbc.Driver" />
            <set-property property="url" value="jdbc:mysql://localhost/mail" />
             <set-property property="username" value="root" />
              <set-property property="password" value="" />
      </data-source>
</data-sources>

WELL With ABOVE CONFIGURATION IAM GETTING an error called as

SEVERE: Initializing application data source org.apache.struts.action.DATA_SOURCE
org.apache.commons.dbcp.SQLNestedException: Cannot load JDBC driver class 'com.mysql.jdbc.Driver'
        at org.apache.commons.dbcp.BasicDataSource.createDataSource(BasicDataSource.java:766)
        at org.apache.commons.dbcp.BasicDataSource.setLogWriter(BasicDataSource.java:598)
        at org.apache.struts.action.ActionServlet.initModuleDataSources(ActionServlet.java:808)
        at org.apache.struts.action.ActionServlet.init(ActionServlet.java:335)
        at javax.servlet.GenericServlet.init(GenericServlet.java:211)
        at org.apache.catalina.core.StandardWrapper.loadServlet(StandardWrapper.java:1029)
        at org.apache.catalina.core.StandardWrapper.load(StandardWrapper.java:862)
        at org.apache.catalina.core.StandardContext.loadOnStartup(StandardContext.java:4013)
        at org.apache.catalina.core.StandardContext.start(StandardContext.java:4357)
        at org.apache.catalina.core.ContainerBase.addChildInternal(ContainerBase.java:823)
        at org.apache.catalina.core.ContainerBase.addChild(ContainerBase.java:807)
        at org.apache.catalina.core.StandardHost.addChild(StandardHost.java:595)
        at org.apache.catalina.core.StandardHostDeployer.install(StandardHostDeployer.java:277)
        at org.apache.catalina.core.StandardHost.install(StandardHost.java:832)
        at org.apache.catalina.startup.HostConfig.deployWARs(HostConfig.java:617)
        at org.apache.catalina.startup.HostConfig.deployApps(HostConfig.java:431)
        at org.apache.catalina.startup.HostConfig.start(HostConfig.java:968)
        at org.apache.catalina.startup.HostConfig.lifecycleEvent(HostConfig.java:349)
        at org.apache.catalina.util.LifecycleSupport.fireLifecycleEvent(LifecycleSupport.java:119)
        at org.apache.catalina.core.ContainerBase.start(ContainerBase.java:1091)
        at org.apache.catalina.core.StandardHost.start(StandardHost.java:789)
        at org.apache.catalina.core.ContainerBase.start(ContainerBase.java:1083)
        at org.apache.catalina.core.StandardEngine.start(StandardEngine.java:478)
        at org.apache.catalina.core.StandardService.start(StandardService.java:480)
        at org.apache.catalina.core.StandardServer.start(StandardServer.java:2313)
        at org.apache.catalina.startup.Catalina.start(Catalina.java:556)
        at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
        at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
        at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
        at java.lang.reflect.Method.invoke(Method.java:494)
        at org.apache.catalina.startup.Bootstrap.start(Bootstrap.java:284)
        at org.apache.catalina.startup.Bootstrap.main(Bootstrap.java:422)
Caused by: java.lang.ClassNotFoundException: com.mysql.jdbc.Driver
        at org.apache.catalina.loader.StandardClassLoader.loadClass(StandardClassLoader.java:854)
        at org.apache.catalina.loader.StandardClassLoader.loadClass(StandardClassLoader.java:721)
        at java.lang.ClassLoader.loadClassInternal(ClassLoader.java:302)
        at java.lang.Class.forName0(Native Method)
        at java.lang.Class.forName(Class.java:155)
        at org.apache.commons.dbcp.BasicDataSource.createDataSource(BasicDataSource.java:760)
        ... 31 more


0
 
fargoCommented:
i m having the same setup and i do keep the jars in WEB-INF/lib  and it works like charm. The only difference is i don't use WAR file deployment. It's definitely a classpath issue...hmm....

try this // we keep this jar at the start
SET CLASSPATH=%CATALINA_HOME%\webapps\mailroom\WEB-INF\lib;%CLASSPATH%

0
 
lensiAuthor Commented:
Do i need to set classpath in struts-config or any file in my struts application
0
 
fargoCommented:
Do i need to set classpath in struts-config or any file in my struts application
>> NO
0
 
fargoCommented:
Ok. The reason why it is not working for you and working for me is as follows. I am not using the dbcp configuration with tomcat...it means, i just get the connection parameters and handle the datasource in my coding instead of using the tomcat provided dbcp connection pooling. For using Tomcat's DBCP connection pooling, the JDBC driver classes *must* go into common/lib. Putting them in the WEB-INF/lib is not good enough then. Tomcat uses Jakarta Commons DBCP libraries to instantiate the jdbc driver class and the Tomcat Classloader hierarchy won't find the driver file unless it's loaded by the same classloader.

Only way to put it in WEB-INF/lib, is to create the datasource with your code and be independent of tomcat connection pooling.



0

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

  • 7
  • 6
  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now