java.lang.ClassNotFoundException: org.postgresql.Driver - Cause and Solution

java.lang.ClassNotFoundException: org.postgresql.Driver error comes when you are trying to connect to a PostgreSQL database from Java program but Java ClassLoader is not able to find the Driver class "org.postgresql.Driver" required to make the connection. Usually this class is find in PostgreSQL JDBC driver JAR e.g. postgresql-9.4-1201.jdbc41.jar, which is required to connect PostgreSQL server version greater than 9.3 from JDK 1.7 or JDK 1.8, the exact JAR depends upon your PostgreSQL server version, the Java version you are running and JDBC version your are using. Now your problem could be either you don't have that PostgreSQL JDBC driver JAR in your machine or the JAR is not in your classpath, or you might be battling with some classpath intricacies. If you don't have this JAR, then solution of "java.lang.ClassNotFoundException: org.postgresql.Driver" is simple, just download it from PostgreSQL site. Make sure you download the correct version of JDBC driver based upon the PostgreSQL server you are connecting and JVM version of your machine. Once you download that, just put in the lib directory of your Java application e.g. WEB-INF/lib if you are connecting to PostgreSQL from Java Web Application. If you are running in Tomcat, then you can alternatively also put this in tomcat/lib directory, but beware of the difference in the application dependency in WEB-INF/lib or in tomcat/lib. You can also take a look at Practical database programming with Java book by Ying Bai. This books is a comprehensive guide of how to use JDBC in Java to connect to different databases. You will learn right ways of doing things with respect to Java and database.

Cause of java.lang.ClassNotFoundException: org.postgresql.Driver

In order to connect to PostgreSQL database from Java, first step is to register the driver with DriverManager. Generally, Class.forName() method is used to find, load and register JDBC driver. So if your application executes Class.forName("org.postgresql.Driver"), then Java's class loader tries to find this class in all the JAR files included in CLASSPATH. If they are able to find then they load, instantiate and register the driver otherwise it throws java.lang.ClassNotFoundException: org.postgresql.Driver. Many times, what will happen that the JAR file will be their but you will still be getting java.lang.ClassNotFoundException: org.postgresql.Driver because of classpath mysteries, this is why I suggest you to read my post, how ClassPath works in Java. Sometimes, you add this JAR into CLASSPATH environment variable, but your application would be running using -cp or -classpath option, so it will ignore the CLASSPATH environment variable. If that's the case then you must include it in the -cp or -classpath option. Some other times, like in case of Servlet JSP based application, not putting the   JAR postgresql-9.4-1201.jdbc41.jar in WEB-INF/lib folder.This error is also exactly similar to java.lang.classnotfoundexception oracle.jdbc.driver.oracledriver, which comes when you try to connect to Oracle database and driver's JAR is not in CLASSPATH. Steps and approach mentioned there also application to solve this problem.

Solution of java.lang.ClassNotFoundException: org.postgresql.Driver

Depending upon your setup, solution could be any of these :

1) You need PostgreSQL JDBC driver to connect from Java program e.g. postgresql-9.4-1201.jdbc41.jar. Actual JDBC JAR could be different, depending upon the PostgreSQL server you are connecting and Java version your are running. You can also download PostgreSQL driver from

2) If you already have this JAR and your Java program is not running with -cp or -classpath option, then edit the CLASSPATH environment variable and add the directory, where you have put this JAR file. You can edit CLASSPATH in windows as set CLASSPATH = %CLASSPATH%; (location of PostgreSQL JDBC driver) and export CLASSPATH = ${CLASSPATH};postgresql-9.2-1002.jdbc3.jar

3) If your Java program is running with -cp or -classpath option then add the location of postgresql-9.2-1002.jdbc3.jar (PostgreSQL version 9.2 for Java version 1.6 or lesser with JDBC 3.0) as well e.g.
java -cp .;postgresql-9.2-1002.jdbc3.jar JavaToPostgreSQL, where JavaToPostgreSQL is name of your Java program. Remember separator in Windows is ; (Semi colon ) and separator in Linux is : (colon)

4) If you are connecting PostgreSQL db from Java Web application then put postgresql-9.4-1201.jdbc41.jar into your WEB-INF/lib folder, if your tomcat is running on Java 1.7 or 1.8 version.

5) If you are using a java version older than 1.6 then you will need to use a JDBC3 version of the driver i.e. postgresql-9.3-1103.jdbc3.jar. You can find the right version of JDBC driver from PostgreSQL site, depending upon the factors e.g. PostgreSQL version or Java version.

How to fix java.lang.ClassNotFoundException: org.postgresql.Driver in Java

That's all about how to fix java.lang.ClassNotFoundException: org.postgresql.Driver error in Java. As I said, this error is very straight forward to solve, you just need the PostgreSQL JDBC driver in your CLASSPATH. Difficult thing is to understand how Classpath works, so make sure you read that tutorial. If you already have postgresql-9.2-1002.jdbc3.jar (or any other PostgreSQL JDBC driver) in classpath but still getting this error then please post in comment what did you try and your setup e.g. CLASSPATH and we will try to troubleshoot together. In almost all cases its the CLASSPATH intricacies which cause "java.lang.ClassNotFoundException: org.postgresql.Driver".

Couple of other common errors and exceptions which comes while connecting to other popular database from Java programs e.g. MySQL, Oracle and Microsoft SQL Server
  • How to connect to MySQL database from Java Program [steps]
  • General Guide to solve java.lang.ClassNotFoundException in Java [guide]
  • How to solve java.lang.ClassNotFoundException: oracle.jdbc.driver.OracleDriver in Java? [solution]
  • How to fix java.lang.ClassNotFoundException: org.postgresql.Driver error in Java? [solution]
  • How to solve java.lang.ClassNotFoundException:org.Springframework.Web.Context.ContextLoaderListener [solution]
  • How to solve java.lang.ClassNotFoundException: com.mysql.jdbc.Driver in Java MySQL? [solution]
  • java.lang.ClassNotFoundException: org.apache.commons.logging.LogFactory? [solution]
  • How to fix java.lang.ClassNotFoundException: in Java? [solution]

No comments :

Post a Comment