Search code examples
postgresqljdbcjarcommandconnector

Postgres : No suitable Driver found for jdbc JAR File


My eclipse program is working fine. However, when I try to compile it into a JAR file, I get this error:

java.sql.SQLException: No suitable driver found for jdbc:postgresql://10.0.0.19: 5432/fussballverein

I added the connector to my java files and exported them to a jar file, is this right?

This is how my jar file looks like:

MyModel.class

MyModel.java

MyControll.class

MyControll.java

Mypanel.class

Mypanel.java

postgresql-9.3-1101.jdbc3.jar

Myframe.class

Myframe.java

When I try to run it from my command prompt, it looks like this:

java -jar Gebauer_Laurenz.jar 10.0.0.19 username password fussballverein

I am getting the Error from before. Here is my Code; it would be great if someone could help.

public void auführen () throws SQLException, ClassNotFoundException{
    //Connection Objekt
    Connection con = null;
    System.out.println(server+""+db+""+user+""+pw);
    try {
        //Class.forName("org.postgresql.Driver");
    //In DriverManager werden die Verbindungsdaten eingetragen so das eine Verbindung hergestellt werden kann.
            con = DriverManager.getConnection("jdbc:postgresql://"+server+":5432/"+db+"",""+user+"",""+pw+"");

Solution

  • There are two potential problems here: Incorrect classpath, and using a JDBC 3 driver (instead of a JDBC 4 driver) without explicitly loading the Driver-class.

    Problem 1: classpath

    Dependent Jars should not be included inside your own jar, they should be placed next to your own jar and then be specified in the Class-Path in the MANIFEST.MF.

    Eg Your layout could be:

    your.jar
    postgresql-9.3-1101.jdbc3.jar
    

    Then the MANIFEST.MF of your.jar should have an entry:

    Class-Path: postgresql-9.3-1101.jdbc3.jar
    

    See this tutorial for details.

    Problem 2: JDBC 3 driver

    The driver you are using is a JDBC 3 driver. Driver auto-loading was only introduced with JDBC 4 (Java 6). JDBC 4 autoloading works by drivers declaring what their driver class(es) are, presumably a JDBC 3 driver does not have that file. Your code has the Class.forName... commented out and the driver won't be loaded.

    If you are using Java 6 or higher, upgrade to the JDBC 4 (Java 6) or JDBC 4.1 (Java 7) driver.