开发者

Tomcat does not recognize the MySQL .jar library

开发者 https://www.devze.com 2022-12-09 09:51 出处:网络
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. T

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?


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.


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.


Copy mysql connector to /usr/share/tomcat7/lib


One other thing you might want to do, if you don't want to put a file into the Tomcat lib directory. It states in catalina.sh that it will check setenv.sh for a variable set to include outside jars. So for example, as I did it, you may create this file:

$CATALINA_HOME/bin/setenv.sh

with the contents:

CLASSPATH=lib/mysql-connector-java-5.1.33-bin.jar

The path to the jar will be relative to where you start up Tomcat, also known as your working directory. So, for example, in my case I start up Catalina from an Ant script two levels up from $CATALINA_HOME and my lib directory is directly underneath that point.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号