java.lang.NoClassDefFoundError:com/google/cloud/sql/core/CoreSocketFactory [英] java.lang.NoClassDefFoundError: com/google/cloud/sql/core/CoreSocketFactory
问题描述
当我尝试运行通过jdbc连接到Google云实例的脚本时,我一直收到此错误.完全不确定该如何解决:
I keep getting this error when I try to run a script that connects to a google cloud instance via jdbc. Not really sure at all how to fix it:
Exception in thread "main" java.lang.NoClassDefFoundError: com/google/cloud/sql/core/CoreSocketFactory
at com.google.cloud.sql.postgres.SocketFactory.createSocket(SocketFactory.java:73)
at org.postgresql.core.PGStream.<init>(PGStream.java:73)
at org.postgresql.core.v3.ConnectionFactoryImpl.tryConnect(ConnectionFactoryImpl.java:93)
at org.postgresql.core.v3.ConnectionFactoryImpl.openConnectionImpl(ConnectionFactoryImpl.java:197)
at org.postgresql.core.ConnectionFactory.openConnection(ConnectionFactory.java:49)
at org.postgresql.jdbc.PgConnection.<init>(PgConnection.java:211)
at org.postgresql.Driver.makeConnection(Driver.java:459)
at org.postgresql.Driver.connect(Driver.java:261)
at java.sql.DriverManager.getConnection(DriverManager.java:664)
at java.sql.DriverManager.getConnection(DriverManager.java:270)
at App.main(App.java:12)
Caused by: java.lang.ClassNotFoundException: com.google.cloud.sql.core.CoreSocketFactory
at java.net.URLClassLoader.findClass(URLClassLoader.java:382)
at java.lang.ClassLoader.loadClass(ClassLoader.java:418)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:355)
at java.lang.ClassLoader.loadClass(ClassLoader.java:351)
... 11 more
正在连接到db的脚本(".."引用出于隐私原因未包含的实例名称和密码):
Script that's connecting to db (".." references instance name and password that haven't been included for privacy reasons):
import java.sql.Connection;
import java.sql.DriverManager;
public class App
{
public static void main(String args[])
{
Connection c = null;
try
{
Class.forName("org.postgresql.Driver");
c = DriverManager.getConnection("jdbc:postgresql:///<postgres>?cloudSqlInstance=<...:database>&socketFactory=com.google.cloud.sql.postgres.SocketFactory&user=<postgres>&password=<...>");
}
catch (Exception e)
{
e.printStackTrace();
System.err.println(e.getClass().getName()+": "+e.getMessage());
System.exit(0);
}
System.out.println("Opened database successfully");
}
}
推荐答案
您正在尝试将socketFactory=com.google.cloud.sql.postgres.SocketFactory
参数传递给JDBC连接字符串,而在类路径中找不到类com.google.cloud.sql.core.CoreSocketFactory
.
You're trying to pass socketFactory=com.google.cloud.sql.postgres.SocketFactory
parameter to JDBC connection string, while class com.google.cloud.sql.core.CoreSocketFactory
is not found in your classpath.
您似乎尚未将必需的依赖项添加到classpath.看看GCP github手册: https://github.com/GoogleCloudPlatform/cloud-sql-jdbc-socket-factory
It looks you have not added a required dependency to classpath. Take a look at GCP github manual: https://github.com/GoogleCloudPlatform/cloud-sql-jdbc-socket-factory
例如,如果您使用的是Maven,请添加此依赖项
For example, add this dependency if you're using Maven
<dependency>
<groupId>com.google.cloud.sql</groupId>
<artifactId>postgres-socket-factory</artifactId>
<version>1.0.15</version>
</dependency>
这篇关于java.lang.NoClassDefFoundError:com/google/cloud/sql/core/CoreSocketFactory的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!