1

I'm getting this weird exception when trying to create a MysqlDataSource object (or at least I think this is the root of the problem).

Let me first describe exactly what I have so far:
I'm using Tomcat7 as a container, and Eclipse as IDE, in order to create a JSP login form, that takes a username and a password, and then invokes a Servlet called "LoginController". whenever you click the "Login" button, the LoginController verifies the input (user name and password) against the MySql database.
Well, at least this is the plan...

Everything seems OK, until it gets to the part where it needs to instantiate the com.mysql.jdbc.jdbc2.optional.MysqlDataSource object.
I get this nasty error:

HTTP Status 500 - Servlet execution threw an exception

type Exception report

message Servlet execution threw an exception

description The server encountered an internal error that prevented it from fulfilling this request.

exception

javax.servlet.ServletException: Servlet execution threw an exception
    org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)

root cause

java.lang.NoClassDefFoundError: com/mysql/jdbc/jdbc2/optional/MysqlDataSource
    model.Authenticator.authenticateLogin(Authenticator.java:13)
    controller.LoginController.doPost(LoginController.java:52)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:646)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:727)
    org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)

root cause

java.lang.ClassNotFoundException: com.mysql.jdbc.jdbc2.optional.MysqlDataSource
    org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1720)
    org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1571)
    model.Authenticator.authenticateLogin(Authenticator.java:13)
    controller.LoginController.doPost(LoginController.java:52)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:646)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:727)
    org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)

note The full stack trace of the root cause is available in the Apache Tomcat/7.0.55 logs.

Now, I understand that the server "can't find the MysqlDataSource", I just don't see why, because I DID add the needed jar file to the project, through "Project Properties->Java Build Path->Libraries->Add External Jar...", and you can also see that it shows it in the Project Explorer:

enter image description here

What even more surprising is, that when I open a regular java application project (as opposed to Dynamic Web Project), I'm able to connect to the Mysql database and run queries with no problems.
It just with Tomcat that it doesn't work...

I looked around online and saw that some suggested to "Attach the source..." through the "Java Source Attachment", but couldn't figure out how to do it...

Any help would be greatly appreciated!

2 Answers 2

4

When you add file in dependencies as external jar, it does not get included in the war file when building the app. You need to either copy the jar to the WEB-INF/lib directory in the project or put it in the Tomcat lib directory

Sign up to request clarification or add additional context in comments.

4 Comments

So... do I just drag the mysql-connector-java-5.1.28.jar file to the WEB-INF/lib directory?
In your case, you most likely have to add to the Tomcat lib directory
Where is this lib directory? do you mean: workspace/MyProject/WebContent/WEB-INF/lib/?
It should be tomcatInstallDirectory/lib
0

You have to put mysql-connector-java.jar to /lib folder in Tomcat directory

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.