gpt4 book ai didi

java - 如何解决 java.lang.NoClassDefFoundError

转载 作者:行者123 更新时间:2023-11-30 21:40:58 26 4
gpt4 key购买 nike

我正在开发一个应用程序,用户可以在其中输入他/她的信息,并将该表单的信息插入到 Google Cloud 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 Cloud Engine,我的应用程序在 JDK 1.8.0_131 上运行。我已经包含了“mysql-socket-factory-connector-j-8-1.0.10.jar”,“mysql-connector-java -8.0.11.jar”和“jetty-unixsocket-9.4.8.v20171121.jar”在我的应用程序的构建路径中。
请帮助我理解并解决这个问题。
提前谢谢你。

最佳答案

有关错误的一些信息Why am I getting a NoClassDefFoundError in Java?

至于我,我已经通过将 mylib.jar 添加到服务器上的“tomcat 9 dir/lib”文件夹和本地类路径(在 Eclipse Properties > Java Build Path > Libraries->Add External JARs 中)解决了这个问题...)。你能描述一下你是如何添加 jar 的吗?

关于java - 如何解决 java.lang.NoClassDefFoundError,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51623105/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com