Search code examples
javamysqlservletsjar

Eclipse - add .jar to Dynamic Web Project


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


Solution

  • 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).