Daniel C. Deng
Daniel C. Deng

Reputation: 1589

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.

My servlet code:

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

Anybody has a clue?

Upvotes: 0

Views: 1118

Answers (2)

sendon1982
sendon1982

Reputation: 11314

You just config that for Build Path in Eclipse for compiling. When running it in Servlet Container like Tomcat, you need to put in WEB-INF/lib.

Putting jar file under Tomcat lib folder will also work.

Upvotes: 0

Scary Wombat
Scary Wombat

Reputation: 44854

As the above method is protected void doGet(HttpServletRequest request, HttpServletResponse response) I think that we can assume that this is a web application.

When deploying the application to Tomcat (???) ensure that the jar is included either in tomcat/lib or in the deployed WEB-INF/lib directory

EDIT: as per @duffymo Tomcat 7.x requires that the JAR be in the Tomcat /lib directory or it won't find it.

Upvotes: 1

Related Questions