No suitable driver found for jdbc:postgresql://localhost:5432/gis

SitephenD77 :

I've spent the last 3 hours trying to get my Java program to interface with my Postgres server. I cannot get past the error message "No suitable driver found for jdbc:postgresql://localhost:5432/gis". It is a Bukkit plugin, and I am using IntelliJ IDEA.

The code:

try
{
    //Class.forName("org.postgresql.Driver");
    c = DriverManager.getConnection("jdbc:postgresql://localhost:5432/gis");
}
catch(Exception e)
{
        getLogger().info(e.getMessage());
}

Things I have tried:

  • java -cp ./path/to/postgresjdbc.jar -jar spigot-1.15.2.jar

  • adding the jdbc file internals directly to my jar file

  • adding the jdbc file as a dependency within the IntelliJ project

  • switching to maven, and putting the following in pom.xml:

    <dependencies>
        <dependency>
            <groupId>org.postgresql</groupId>
            <artifactId>postgresql</artifactId>
            <version>42.2.1</version>
        </dependency>
    </dependencies>

I am unable to get past the error I posted. At this point, it has taken over my entire evening.

PauMAVA :

I've stumbled with this issue several times when developing Bukkit/Spigot plugins that make use of MySQL databases. The process for Postgres should be the same.

Usually, this error happens when your plugin can't find the PostgresqlJDBC driver. You have two workarounds:

Option 1. Adding the .jar to the plugin's classpath:

It's recommended that you set the libraries inside plugins/lib as then your server won't try to load the libraries as a Bukkit plugin. Add the prefix lib/ to your classpath by adding this configuration in your pom.xml:

<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-jar-plugin</artifactId>
    <configuration>
        <archive>
            <manifest>
                <addClasspath>true</addClasspath> <-- don't know if this is needed -->
                <classpathPrefix>lib/</classpathPrefix>
            </manifest>
        </archive>
    </configuration>
</plugin>

Then make sure to put your postgresjdbc.jar inside a folder called lib inside your plugin's folder.

Option 2. Add dependencies directly in your jar:

Note that this option will increase your plugin's jar size.

This can be done via Maven's assembly plugin:

<plugin>
    <artifactId>maven-assembly-plugin</artifactId>
    <executions>
        <execution>
            <phase>package</phase>
            <goals>
                <goal>single</goal>
            </goals>
        </execution>
    </executions>
    <configuration>
        <descriptorRefs>
            <descriptorRef>jar-with-dependencies</descriptorRef>
        </descriptorRefs>
        <archive>
            <manifest>
                <mainClass>your.main.class</mainClass>
            </manifest>
        </archive>
    </configuration>
</plugin>

If you open your plugin's jar with a file compressor like 7-zip you should there should the driver's classes in it apart from your plugin ones.

Let me know if this solved your issue.

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=407551&siteId=1