ClassNotFoundException with PostgreSQL and JDBC

The driver name is OK. It is the same as mentioned in the official docs of the driver. Therefore the driver is just not in the classpath.

You say:

I […] properly set the application ClassPath

On the other hand you start the program by just calling:

java JavaPostGreSQLConnectivity

In that case no PG driver is on the classpath. You have to add it by hand using someting like

java -cp postgresql-jdbc4.jar JavaPostGreSQLConnectivity

EDIT The question has been changed while typing, hence the duplication.

You added the jar only in you IDE. This helps the IDE to compile your code. If you start the program using you IDE then the IDE will also set the classpath for you. But if you don’t start via the IDE then nobody knows the correct classpath and it has to be set by hand.

Your options are:

  • start always via IDE
  • make some batch script which hides the setting of the classpath (common solution)
  • set the CLASSPATH environment variable (does not scale with other Java applications)
  • make an “Executable Jar” and set the classpath there. (Search this site using that term).
  • put the jar into a place where the JVM picks it up automatically (e.g. in the lib/ext directory of the JRE). But polluting the JRE/JDK libs is the worst option.

Note: This is all basic Java knowledge and has nothing to do with PostgreSQL.

Leave a Comment