0

I am using Eclipse 2020-06 with Tomcat 8.5 and have written a Java servlet to connect to a MySQL 8.0.21 db. Despite adding the JDBC driver to the build path, I get a ClassNotFoundException when trying to make the connection. However, when I copied the driver JAR file into the Tomcat lib folder, everything works like a charm. This is clearly a hack and I believe I am missing some config tweak. Any thoughts on what I am missing?

Nagen
  • 21
  • 7
  • This is not a hack; rather, the jar (MySQL Connector/J) must be there in Tomcat lib. Either you do it manually or you do it using Maven script or Gradle script, the JDBC driver has to be put into the Tomat lib. – Arvind Kumar Avinash Jul 30 '20 at 10:52
  • @ArvindKumarAvinash The JDBC driver only has to be put in the Tomcat lib directory if you are going to define a data source in the Tomcat config. If the app defines its own datasource in code or - for example - spring config, then presence of the driver library in the WAR is sufficient. – Mark Rotteveel Jul 30 '20 at 11:31
  • You say the driver is on the build path, but does it also end up in the `WEB-INF/lib` of your WAR? Where did you define the connection, in the tomcat configuration or in code? – Mark Rotteveel Jul 30 '20 at 11:31
  • @MarkRotteveel - Yes, I missed to mention that clearly. Basically, this is what I meant by Maven or Gradle scripts. Thanks for clarifying it. – Arvind Kumar Avinash Jul 30 '20 at 11:50

0 Answers0