Reputation: 117
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
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");
}
}
Upvotes: 1
Views: 3546
Reputation: 2058
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.
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
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>
Upvotes: 1