1

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");
     }
}
CodeMonkey
  • 97
  • 2
  • 9

1 Answers1

1

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>
Mikhail Kopylov
  • 2,008
  • 5
  • 27
  • 58
  • When I recompile using the command mvn -P jar-with-driver-and-dependencies clean package -DskipTests I get this error: 'dependencies.dependency. com.google.cloud.sql:postgres-socket-factory:1.0.15' for com.google.cloud.sql:postgres-socket-factory:1.0.15 is referencing itself. @ line 34, column 17 – CodeMonkey Apr 19 '20 at 04:46
  • I've just created an empty Spring Boot project with this dependency https://gist.github.com/mih-kopylov/98c29173e00e8d30924cad7e96dc8577 it's successfully compiled and packaged with `-P jar-with-driver-and-dependencies` – Mikhail Kopylov Apr 19 '20 at 05:02
  • And here's the source of this artifact: https://github.com/GoogleCloudPlatform/cloud-sql-jdbc-socket-factory/blob/master/postgres/pom.xml - it doesn't contain any circular references as well – Mikhail Kopylov Apr 19 '20 at 05:03
  • when i run the code in your xml file with -P jar-with-driver-and-dependencies I get the following error: No goals have been specified for this build. You must specify a valid lifecycle phase or a goal in the format : or :[:]:. Available lifecycle phases are: validate, initialize, generate-sources, process-sources, generate-resources, process-resources, compile, process-classes, generate-test-sources, process-test-sources, generate-test-resources, process-test-resources, test-compile, process-test-classes, test – CodeMonkey Apr 19 '20 at 05:13
  • It seems you have typed a wrong command. `mvn package` works fine for it. – Mikhail Kopylov Apr 19 '20 at 09:28