0

I'm trying to connect to MySQL using JDBC connection in a Servlet.I'm using Eclipse and added the mysql-connector-java-5.1.20-bin.jar in /WEB_INF/lib folder.

Also the same jar is also added into Java Build Path.After running the project I'm getting the following error

java.lang.ClassNotFoundException: com.mysql.jdbc.Driver
at org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1678) 
at org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1523)
at java.lang.Class.forName0(Native Method)
at java.lang.Class.forName(Class.java:186)
at com.shubh.Download.doGet(Download.java:42)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:621)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:722)


Although I've added the required jar in the classpath. Why is the ClassNotFoundException still generating ? Here is my code.

2
  • 1
    I'm guessing that just dropping the jar on the WEB-INF/lib folder is not enough to make eclipse actually add it to the compiled version of your app, can you try putting your jar on a common dir (like the base of your workspace) and then right click your project->properties->java build path->add external jar, also try right click your server on the servers view and select "Clean" to be sure Eclipse is updating the build
    – ilcavero
    Jun 24, 2012 at 1:05
  • Your code compiles ok for me. Been a while since I used Eclipse but I think you can look in .classpath to see if there is a classpathentry for the jar. See if you can export an ant build file which will tell you one way or another if the directory which contains the jar is in the compile classpath.
    – Sridhar
    Jun 24, 2012 at 15:14

1 Answer 1

1

Usually, dropping .jar in WEB-INF/lib should work. eclipse is smart enough to recognize files added in WEB-INF/lib.

Just to check you can look in .classpath file to see whether it contains an entry like <classpathentry kind="lib" path="WebContent/WEB-INF/lib/mysql-connector-java-5.1.20-bin.jar"/>

But, It is advised not to hassle with classpath entry. Remove all unnecessary reference from Java Build Path and then clean and build again should work.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.