tags:

views:

669

answers:

3

I tried several different ways such that Tomcat loads the MySQL drivers when running my web application. I'm using Ubuntu 8.04, and the libraries come from the libmysql-java package. They are located in the directory shown below:

~$ ls /usr/share/java/mysql*
/usr/share/java/mysql-connector-java-5.1.5.jar

My CLASSPATH includes this file:

~$ echo $CLASSPATH
.:/usr/lib/jvm/java-6-sun/bin:/usr/local/tomcat/lib/servlet-api.jar:/usr/share/java/mysql-connector-java-5.1.5.jar

I even put a copy of the .jar file in my WEB-INF/lib/ directory in my web app:

/usr/local/tomcat/webapps/ohms/WEB-INF/lib$ ls
mysql-connector-java-5.1.5.jar

After making these changes, I restart Tomcat, re-compile my classes, restart Tomcat again. Also, I am importing the necessary libraries using import java.sql.*;

However I am still getting the java.lang.ClassNotFoundException: com.mysql.jdbc.Driver error when it runs the line Class.forName("com.mysql.jdbc.Driver").newInstance();

What am I doing wrong here?

+2  A: 

Put it in TOMCAT_HOME/lib. (I seem to recall that on older versions of Tomcat, it's TOMCAT_HOME/server/lib?)

It's not going to work in WEB-INF/lib since the container needs access to the library, and, that is putting it in the classloader of one web app, not the container. While I would have imagined the CLASSPATH approach would work, it's not the standard way to do this. Perhaps there is some other snag that's preventing it from working.

Sean Owen
Added `java/mysql-connector-java-5.1.5.jar` to `/usr/local/tomcat/lib/`, still no dice.
Murat Ayfer
Not java/mysql-connector-java-5.1.5.jar. Just add the mysql-connector-java-5.1.5.jar directly under /usr/local/tomcat/lib.
Avi
that's what i meant, it was a typo
Murat Ayfer
also, i even tried downloading the latest version of mysql-connector frome the official site, putting it under tomcat/lib, and removing any other references to the library.
Murat Ayfer
A: 

I guess we should also ask, why are you trying to instantiate this class? I don't even know that this exists in MySQL Connector/J anymore as it is the very old way to use a database with JDBC.

The only class I can imagine you want to instantiate is their DataSource implementation, MysqlDataSource. But even that's unlikely. Usually you configure the DataSource in conf/server.xml, then look it up with JNDI. That way your app itself has no dependency on MySQL.

Sean Owen
-1 from me. Of course it exists in MySQL Connector-J - open up the JAR and see. And DataSource still uses a JDBC connection.
duffymo
+1  A: 

Tomcat ignores any CLASSPATH environment variable, as do all Java EE app servers and IDEs. That does you no good at all. I don't set one on any machine that I use.

The JAR needs to go in Tomcat server/lib for Tomcat 5.x and /lib for 6.x.

You don't need to call newInstance(); Class.forName("com.mysql.jdbc.Driver") is sufficient to register the driver class.

duffymo
To be precise, it's default startup scripts (e.g. `catalina.sh`) that ignore (or, rather, override) CLASSPATH rather than Tomcat itself. I've seen RPMs where that was not the case; don't know whether one that comes with Ubuntu 8.04 distribution is one of those. The resulting classpath is used by Tomcat's system class loader.
ChssPly76
Correct. That's been my experience with Tomcat, WebLogic, JBOSS, IntelliJ, and Eclipse. None take a system CLASSPATH into consideration.
duffymo