1

I am in the process of learning Java and PostgreSQL. I am running Windows 10 with JRE 10, my IDE being Eclipse Oxygen 3a. I had written the following code:

import java.sql.Connection;
import java.sql.DriverManager;

public class PostgreSQLJDBC {
   public static void main(String args[]) {
      Connection c = null;
      try {
         Class.forName("org.postgresql.Driver");
         c = DriverManager
            .getConnection("jdbc:postgresql://localhost:5432/testdb",
            "postgres", "123");
      } catch (Exception e) {
         e.printStackTrace();
         System.err.println(e.getClass().getName()+": "+e.getMessage());
         System.exit(0);
      }
      System.out.println("Opened database successfully");
   }
}

It successfully compiles. However, it gives me the following error

java.lang.ClassNotFoundException: org.postgresql.Driver

In following the advice given in Stack Overflow, I have downloaded postgresql-42.2.2.jar and placed within C:\Program Files\Java\jre-10.0.1

I then edited my system variables and added "C:\Program Files\Java\jre-10.0.1" into it. However I still get the same error. Please help

3
  • Are you using any IDE(Netbeans or eclipse) ? , If yes , add that pg.jar to libraries and it should work. Commented Apr 30, 2018 at 4:58
  • Update: I imported the postgresql-42.2.2.jar file into the src folder but I noticed that among all the classes present, org.postgresql.Driver was not present. This is very strange. Commented Apr 30, 2018 at 8:09
  • The driver doesn't belong in C:\Program Files\Java\jre-10.0.1, it also doesn't belong in the src folder, don't put it there. Commented Apr 30, 2018 at 8:50

1 Answer 1

0

Adding a .jar to your JRE is like forcing a DLL to load into every Windows application you run. There's almost never a good reason to do so.

Add the Postgres driver jar to the project's Java Build Path.

http://help.eclipse.org/oxygen/topic/org.eclipse.jdt.doc.user/reference/ref-properties-build-path.htm?cp=1_4_3_1

Maybe go through the tutorial and learn to use your tools. http://help.eclipse.org/oxygen/topic/org.eclipse.jdt.doc.user/gettingStarted/intro/overview.htm?cp=1_0

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

1 Comment

Thank you nitind. It worked.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.