Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
127 views
in Technique[技术] by (71.8m points)

java - 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

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...