2

I have simple class. Then I try to compile and run it I see ClassNotFoundException.

import java.sql.*;

public class DBProcessor{

private static String serverAdres = "127.0.0.1:5432";

private static String DBname = "dota";
private static String clientName = "postgres";
private static String password = "master";
private static Connection connection;

public static void connect() {
    try {
        Class.forName("org.postgresql.Driver");
    } catch (ClassNotFoundException e) {
        System.err.println("Where is your PostgreSQL JDBC Driver? "
                + "Include in your library path!");
        e.printStackTrace();
    }

    try {
        connection = DriverManager.getConnection("jdbc:postgresql://" + serverAdres + "/" + DBname, clientName, password);

    } catch (SQLException e) {
        System.err.println("Connection Failed! Check output console");
        e.printStackTrace();
    }

    if (connection == null) {
        System.err.println("Failed to make connection!");
    }
}

public static void main(String[] args){
    DBProcessor db = new DBProcessor();
    db.connect();
}
}

I'm using Windows cmd:

C:\rmi2>javac -classpath postgresql.jar DBProcessor.java
C:\rmi2>java DBProcessor
Where is your PostgreSQL JDBC Driver? Include in your library path!
java.lang.ClassNotFoundException: org.postgresql.Driver
at java.net.URLClassLoader$1.run(Unknown Source)
at java.net.URLClassLoader$1.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
at java.lang.Class.forName0(Native Method)
at java.lang.Class.forName(Unknown Source)
at DBProcessor.connect(DBProcessor.java:12)
at DBProcessor.main(DBProcessor.java:34)
Connection Failed! Check output console
java.sql.SQLException: No suitable driver found for jdbc:postgresql://127.0.0.1:
5432/dota
at java.sql.DriverManager.getConnection(Unknown Source)
at java.sql.DriverManager.getConnection(Unknown Source)
at DBProcessor.connect(DBProcessor.java:20)
at DBProcessor.main(DBProcessor.java:34)
Failed to make connection!

What I'm doing wrong? So, this is postgrsql library https://www.dropbox.com/s/idx5l0kub5rn1b8/postgresql.jar?dl=0

6
  • 3
    To paraphrase: Where is your PostgreSQL JDBC Driver? Did you include in your library path? Commented Nov 5, 2014 at 20:51
  • -classpath postgresql.jar it is library path? Commented Nov 5, 2014 at 20:54
  • @otopba Don't know. What is the result of jar tvvf postgresql.jar? Commented Nov 5, 2014 at 20:56
  • The -classpath directive will only help your code compile, not help it run Commented Nov 5, 2014 at 20:56
  • 1
    @ElliottFrisch it's library from official site. jdbc.postgresql.org/download.html Commented Nov 5, 2014 at 20:59

3 Answers 3

6

On the command line, this

java DBProcessor

should be

java -cp .;postgresql.jar DBProcessor

And, because it includes java.sql.Driver, when you have it working you could remove

// try {
//    Class.forName("org.postgresql.Driver");
// } catch (ClassNotFoundException e) {
//    System.err.println("Where is your PostgreSQL JDBC Driver? "
//            + "Include in your library path!");
//    e.printStackTrace();
// }

Per the DriverManager Javadoc,

The DriverManager methods getConnection and getDrivers have been enhanced to support the Java Standard Edition Service Provider mechanism. JDBC 4.0 Drivers must include the file META-INF/services/java.sql.Driver. This file contains the name of the JDBC drivers implementation of java.sql.Driver. For example, to load the my.sql.Driver class, the META-INF/services/java.sql.Driver file would contain the entry:

 my.sql.Driver

Applications no longer need to explictly load JDBC drivers using Class.forName(). Existing programs which currently load JDBC drivers using Class.forName() will continue to work without modification.

Sign up to request clarification or add additional context in comments.

Comments

0

You need to link the driver to your IDE. if you're using Netbeans, right click on Libraries, Add jar/folder then browse to the driver.

Comments

0

If the postgresql.jar file is not accessible to the Java runtime environment you will get a 'java.lang.ClassNotFoundException: org.postgresql.Driver' exception.

The easiest way to make the postgresql jdbc driver accessible to your applications is to install it as a Java Extension. The only thing you need to do is cp, mv or ln the postgresql.jar file into the Java extension directory. There is no need to set CLASSPATH if you do this. The Java extension directory is $JAVA_HOME/jre/lib/ext ($JAVA_HOME on my system is /usr/java/jdk1.3) or for Windows C:\Program Files\Java\jdk1.8.0_25\jre\lib\ext

This makes life REALLY easy; I didn't have to set CLASSPATH at all in my environment again!

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.