class not found exception Class.forName("com.mysql.jdbc.Driver") in webservice

28,431

If this code works in your J2SE it means you need to have a JAR file somewhere containing com.mysql.jdbc.Driver class (so called JDBC driver). This JAR needs to be visible in Tomcat. So, I would suggest to place mysql-jdbc.jar at physical location to /WEB-INF/lib directory of your project.

Many times in the past, I have experienced ClassNotFoundException if jar is not at physical location. Then restarting Tomcat should work.

Share:
28,431
Basant
Author by

Basant

senior android developer

Updated on May 30, 2020

Comments

  • Basant
    Basant almost 4 years

    I have a problem and need help in Class.forName("com.mysql.jdbc.Driver") that throw class not found exception when i run webservice from eclipse ,but when I create new java project its run perfectly.

    I add the mysql-connector-java-5.1.19-bin.jar in the build path for both projects but I don't know what is the problem in the webservice.

      public String insertOrder(
             int current_id, 
            int table_id) 
            {
                try {
          Class.forName("com.mysql.jdbc.Driver");
          Connection con = 
        DriverManager.getConnection("jdbc:mysql://localhost:3306/myhoteldb", "root", "mypassword");
          PreparedStatement st = 
        con.prepareStatement("insert into orders(orders.current_id,orders.table_id) values(?,?)");
          st.setInt(1, current_id);
          st.setInt(2, table_id);
    
          st.executeUpdate();
          } catch (Exception e) {
          System.out.println(e.getMessage());
          }
          return "record inserted";
          }
        }
    

    and this the error log

      java.lang.ClassNotFoundException: com.mysql.jdbc.Driver
    at org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1680)
    at org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1526)
    at java.lang.Class.forName0(Native Method)
    at java.lang.Class.forName(Unknown Source)
    at pkg.MyServices.insertOrder(MyServices.java:124)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
    at java.lang.reflect.Method.invoke(Unknown Source)
    at org.apache.axis2.rpc.receivers.RPCUtil.invokeServiceClass(RPCUtil.java:194)
    at org.apache.axis2.rpc.receivers.RPCMessageReceiver.invokeBusinessLogic(RPCMessageReceiver.java:102)
    at org.apache.axis2.receivers.AbstractInOutMessageReceiver.invokeBusinessLogic(AbstractInOutMessageReceiver.java:40)
    at org.apache.axis2.receivers.AbstractMessageReceiver.receive(AbstractMessageReceiver.java:114)
    at org.apache.axis2.engine.AxisEngine.receive(AxisEngine.java:173)
    at org.apache.axis2.transport.http.HTTPTransportUtils.processHTTPPostRequest(HTTPTransportUtils.java:173)
    at org.apache.axis2.transport.http.AxisServlet.doPost(AxisServlet.java:144)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:637)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:717)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:290)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
    at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:233)
    at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:191)
    at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:127)
    at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:102)
    at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:109)
    at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:298)
    at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:859)
    at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:588)
    at org.apache.tomcat.util.net.JIoEndpoint$Worker.run(JIoEndpoint.java:489)
    at java.lang.Thread.run(Unknown Source)
    
    • Michael
      Michael about 12 years
      When you export your .war file, is the .jar included?
    • Nathaniel Ford
      Nathaniel Ford about 12 years
      @Michael's comment is why it's good during dev to use exploded war files, so you have visibility into what is actually there.
    • Logan
      Logan about 12 years
      Hardik is right. Basically the issue is that even though the Jar is there in the class path, the server is not able to find and access it and hence the excpetion.
  • Basant
    Basant about 12 years
    Mishar ,Thanks for your answer this solve my problem :)) many thanks for all
  • shareef
    shareef over 11 years
    thanks it worked by copying on web-inf /lib and this probelm on web service on eclipse but on netbeans it worked just fine i used axis2 on java file
  • Simmant
    Simmant about 10 years
    great one bro solve my issue to ..