1

I am developing an application in which the user enters his/her information in a form,and the information of that form is inserted into Google Cloud MySQL database.I have written the code to achieve the above task as below: `

package com.example.dao;

import java.sql.Connection;
import java.sql.DriverManager;

public class dao {
    private static Connection con;
    public static Connection getcon(){
        try{
            String instanceConnectionName = "edu-vitae1";
            String databaseName = "eduvitae";
             String username = "root";
             String password = "root";
             Class.forName("com.mysql.jdbc.Driver");
             String jdbcUrl = String.format(
             "jdbc:mysql://google/eduvitae?cloudSqlInstance=edu-vitae-211606:asia-south1:edu-vitae1&socketFactory=com.google.cloud.sql.mysql.SocketFactory&user=root&password=root&useSSL=false",
                  databaseName,
                  instanceConnectionName);
             //Connection con=DriverManager.getConnection("jdbc:mysql://google/eduvitae?cloudSqlInstance=edu-vitae-211606:asia-south1:edu-vitae1&socketFactory=com.google.cloud.sql.mysql.SocketFactory&useSSL=false&user=root&password=root");
             Connection con = DriverManager.getConnection(jdbcUrl, username, password);
             //con=DriverManager.getConnection("jdbc:mysql://35.200.134.221/edu","root","root");
        }
        catch (Exception e) {
            System.out.println(e);
        }
        return con;
    }

}

After executing the above code I am getting the following error:

java.sql.SQLException: java.lang.NoClassDefFoundError: com/mysql/cj/protocol/SocketFactory
at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:1055)
at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:956)
at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:926)
at com.mysql.jdbc.Util.handleNewInstance(Util.java:430)
at com.mysql.jdbc.ConnectionImpl.getInstance(ConnectionImpl.java:302)
at com.mysql.jdbc.NonRegisteringDriver.connect(NonRegisteringDriver.java:282)
at java.sql.DriverManager.getConnection(Unknown Source)
at java.sql.DriverManager.getConnection(Unknown Source)
at com.example.dao.dao.getcon(dao.java:20)
at com.example.model.register.doPost(register.java:105)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:660)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:741)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:231)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)
at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:53)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)
at com.googlecode.objectify.ObjectifyFilter.doFilter(ObjectifyFilter.java:48)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:199)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:96)
at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:491)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:139)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:92)
at org.apache.catalina.valves.AbstractAccessLogValve.invoke(AbstractAccessLogValve.java:668)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:87)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:343)
at org.apache.coyote.http11.Http11Processor.service(Http11Processor.java:408)
at org.apache.coyote.AbstractProcessorLight.process(AbstractProcessorLight.java:66)
at org.apache.coyote.AbstractProtocol$ConnectionHandler.process(AbstractProtocol.java:764)
at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1388)
at org.apache.tomcat.util.net.SocketProcessorBase.run(SocketProcessorBase.java:49)
at java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)
at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)
at java.lang.Thread.run(Unknown Source)

I am working with Google Cloud Engine and my application is running on JDK 1.8.0_131.I have included "mysql-socket-factory-connector-j-8-1.0.10.jar","mysql-connector-java-8.0.11.jar" and "jetty-unixsocket-9.4.8.v20171121.jar" in my application's build path.
Please help me to understand and solve this issue.
Thank You in advance.

Community
  • 1
  • 1
user10136991
  • 63
  • 1
  • 10

2 Answers2

0

some information about the error Why am I getting a NoClassDefFoundError in Java?

As for me I've solved the problem by adding mylib.jar to the "tomcat 9 dir/lib" folder on a server and to the classpath locally(in Eclipse Properties > Java Build Path > Libraries->Add External JARs...). Can you decribe how did you add your jars?

John
  • 446
  • 6
  • 16
  • Thank You for your suggestion.I have included the jar files in my project in the same way as you have described but still getting the error. – user10136991 Aug 01 '18 at 17:09
  • @user10136991, I've deleted my jar from WEB-INF/lib folder and I've got "Caused by: java.lang.ClassNotFoundException" on tomcat starting. But why you didn't get the error I can't say. – John Aug 02 '18 at 00:59
0

I solved this error by inserting the specified jar file in WEB-INF/lib folder.I had added the jar file in build path of the project but because it wasn't in WEB-INF/lib folder,jvm was unable to load the class.
So to summarize it,whenever you add a file in build path of a project,remember to copy the same file in WEB-INF/lib folder.

user10136991
  • 63
  • 1
  • 10