Tomcat 无法识别 MySQL .jar 库 [英] Tomcat does not recognize the MySQL .jar library
问题描述
我尝试了几种不同的方法,以便 Tomcat 在运行我的 Web 应用程序时加载 MySQL 驱动程序.我使用的是 Ubuntu 8.04,库来自 libmysql-java 包.它们位于如下所示的目录中:
<前>~$ ls/usr/share/java/mysql*/usr/share/java/mysql-connector-java-5.1.5.jar我的 CLASSPATH 包含这个文件:
<前>~$ 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我什至在我的 Web 应用程序的 WEB-INF/lib/目录中放置了 .jar 文件的副本:
<前>/usr/local/tomcat/webapps/ohms/WEB-INF/lib$ lsmysql-connector-java-5.1.5.jar进行这些更改后,我重新启动 Tomcat,重新编译我的类,再次重新启动 Tomcat.另外,我正在使用 import java.sql.*;
但是,当它运行 Class.forName("com.mysql.jdbc.Driver" 行时,我仍然收到
java.lang.ClassNotFoundException: com.mysql.jdbc.Driver
错误").newInstance();
我在这里做错了什么?
放在 TOMCAT_HOME/lib 中.(我似乎记得在旧版本的 Tomcat 上,它是 TOMCAT_HOME/server/lib?)
它不会在 WEB-INF/lib 中工作,因为容器需要访问库,也就是说,将它放在一个 Web 应用程序的类加载器中,而不是容器中.虽然我会想象 CLASSPATH 方法会起作用,但它不是执行此操作的标准方法.也许还有其他一些障碍阻止它工作.
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 无法识别 MySQL .jar 库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!