简体   繁体   English

无法加载该类:com.mysql.jdbc.Driver

[英]Can't load the class: com.mysql.jdbc.Driver

I have read all the prior answers and followed (on Eclipse) Build Path -> Configure Build Path to add the external JAR: mysql-connector-java-5.1.20.jar, however, it's still can not load the class. 我已经阅读了所有先前的答案,然后(在Eclipse上)遵循Build Path-> Configure Build Path添加外部JAR:mysql-connector-java-5.1.20.jar,但是,它仍然无法加载该类。

My servlet code: 我的servlet代码:

protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
  PrintWriter out = response.getWriter();
  try {
    Class.forName("com.mysql.jdbc.Driver");
    out.println("<br>Class loaded: com.mysql.jdbc.Driver<br>");
  } catch (ClassNotFoundException e) {
    out.println("<br>Can't load class: com.mysql.jdbc.Driver<br>");
    return;
  }
}

My Eclipse verson: Luna Release (4.4.0) Ny OSX: 10.9.4 我的Eclipse版本:Luna版本(4.4.0)Ny OSX:10.9.4

Anybody has a clue? 有人知道吗?

As the above method is protected void doGet(HttpServletRequest request, HttpServletResponse response) I think that we can assume that this is a web application. 由于上面的方法是protected void doGet(HttpServletRequest request, HttpServletResponse response)我认为我们可以假定这是一个Web应用程序。

When deploying the application to Tomcat (???) ensure that the jar is included either in tomcat/lib or in the deployed WEB-INF/lib directory 将应用程序部署到Tomcat(???)时,请确保jar包含在tomcat / lib或已部署的WEB-INF / lib目录中

EDIT: as per @duffymo Tomcat 7.x requires that the JAR be in the Tomcat /lib directory or it won't find it. 编辑:根据@ duffymo,Tomcat 7.x要求JAR位于Tomcat / lib目录中,否则将找不到它。

You just config that for Build Path in Eclipse for compiling. 您只需在Eclipse中为Build Path进行配置即可进行编译。 When running it in Servlet Container like Tomcat, you need to put in WEB-INF/lib. 在像Tomcat这样的Servlet容器中运行它时,您需要放入WEB-INF / lib。

Putting jar file under Tomcat lib folder will also work. 将jar文件放在Tomcat lib文件夹下也将起作用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM