25

When I use the follow code in eclipse Dynamic Web Project inside servlet , like this :

@WebServlet("/CreateCustomerServlet")
public class CreateCustomerServlet extends HttpServlet {
    private static final long serialVersionUID = 1L;

    private void test() throws SQLException, ClassNotFoundException {
        // use the JAR ...
        Class.forName("com.mysql.jdbc.Driver");//here the exception
    }
}

it throw the exception:

java.lang.ClassNotFoundException: com.mysql.jdbc.Driver

in the line:

Class.forName("com.mysql.jdbc.Driver");

In the Build path of the dynamic web project I added the jar:

mysql-connector-java-5.1.21-bin.jar

I add snapshot of the folder , you can see where it is the required jar -

enter image description here

URL87
  • 10,667
  • 35
  • 107
  • 174

6 Answers6

39

As your screenshot shows, the mysql jar doesn't appear under the Web App Libraries node. Paste the jar in WebContent/WEB-INF/lib, and it will be automatically added to the build path and to the runtime classpath of the webapp (and it will appear under Web App Libraries).

JB Nizet
  • 678,734
  • 91
  • 1,224
  • 1,255
  • 2
    Thank you very much. You saved us on Hackathon – gkiko Dec 22 '13 at 00:58
  • That works ,I was also facing the same problem , is there some issue with build path of dynamic web project ? – Praveen Kumar Jul 19 '14 at 11:59
  • No issue. The build path is used to build your project, but it can contain libraries that must not be in the runtime classpath (like testing libraries, for example). The runtime libraries must be placed in WEB-INF/lib. – JB Nizet Jul 19 '14 at 12:38
  • I have the JDBC connector into the Web App Libraries, but I still receive the same exception **No suitable driver found** – giliev May 22 '15 at 14:18
  • please read answer of @Nasruddin down there - it's more accurate IMHO – Błażej Kocik Dec 10 '16 at 07:56
27

Just now I troubleshoot this issue.

  • Just go to properties of your project.
  • Select Deployment Assembly -> add -> Java Build Path Entries.

And you are done !

Pshemo
  • 122,468
  • 25
  • 185
  • 269
Nasruddin
  • 1,640
  • 14
  • 20
2

Try doing this:

Make a directory named lib in the root directory of the proyect and put the .jar in it.

In the WEB-INF directory, make a link to lib.

Now, the jar would appear under WebbApp Libraries.

j0k
  • 22,600
  • 28
  • 79
  • 90
Kaditx
  • 21
  • 1
1

Compile time & runtime are two different things. What ever you added right now is just for compile time & build purpose only. Runtime server tries to load the class. Add same jar to project lib folder also (Which will be packaged with WAR and available for the server at runtime).

kosa
  • 65,990
  • 13
  • 130
  • 167
0

The solution is quite straightforward:

 1. Right click on the your web project and choose PROPERTIES

 2. Choose the J2EE Module Dependencies

 3. Click on the Add External JARs… and then point on your library file (ZIP or JAR)

 4. Click on Apply then OK.

 5. Restart your Apache Tomcat

The added library should be seen under: ProjectName > Java Resources: src > Libraries > WebApp Libraries.

jmail
  • 5,944
  • 3
  • 21
  • 35
  • 1
    It seems that in newer version of Eclipse `J2EE Module Dependencies` was replaced with `Deployment Assembly`. – Pshemo Feb 19 '14 at 12:13
  • @Pshemo, yes buddy. Your right, that was in newer version. But, this one as also answer buddy. – jmail Feb 19 '14 at 12:29
  • 1
    I wasn't saying that this is not an answer because it can be usefull for many peoples. My comment was meant only as little improvement for those with newer versions of Eclipse. Also probably not many peoples here are your buddies so try to avoid this word. – Pshemo Feb 19 '14 at 13:31
0

if you already added mysql-connector.jar file, then you also need to add it into project's web deployment assembly. For this -

  1. In eclipse - go to project properties by right clicking on it.
  2. You will see web deployment assembly option.
  3. Add your jar file here, using Add button.