如何解决java.lang.noclassdeffounderror

fnvucqvd  于 2021-06-20  发布在  Mysql
关注(0)|答案(2)|浏览(594)

我正在开发一个应用程序,用户在表单中输入自己的信息,并将该表单的信息插入到google云mysql数据库中,我编写了实现上述任务的代码如下:`

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;
    }

}

执行上述代码后,我得到以下错误:

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)

我正在使用google云引擎,我的应用程序运行在jdk1.8.0_上。我在应用程序的构建路径中包含了“mysql-socket-factory-connector-j-8-1.0.10.jar”、“mysql-connector-java-8.0.11.jar”和“jetty-unixsocket-9.4.8.v20171121.jar”。
请帮助我理解和解决这个问题。
先谢谢你。

yacmzcpb

yacmzcpb1#

关于这个错误的一些信息为什么我在java中得到noclassdeffounderror?
至于我,我已经通过将mylib.jar添加到服务器上的“tomcat9dir/lib”文件夹和本地的类路径(在eclipseproperties>java build path>libraries->addexternaljars…)解决了这个问题。你能描述一下你是怎么加jar的吗?

ftf50wuq

ftf50wuq2#

我通过在web inf/lib文件夹中插入指定的jar文件来解决这个错误。我在项目的生成路径中添加了jar文件,但由于它不在web inf/lib文件夹中,jvm无法加载该类。
总之,每当您在项目的生成路径中添加文件时,请记住在web inf/lib文件夹中复制同一个文件。

相关问题