Compiling and debugging PostgreSQL’s PgJDBC under Eclipse

I’ve always worked on PgJDBC, the JDBC Type 4 driver for PostgreSQL, with just a terminal, ant and vim. I recently had occasion to do some PgJDBC debugging work on Windows specifics so I set up Eclipse to avoid having to work on the Windows command prompt.

As the process isn’t completely obvious, here’s how to set up Eclipse Luna to work with the PgJDBC sources.

If you don’t have it already, download JDK 1.8 and Eclipse Luna.

Now download JDK 1.6 from Oracle (or install the appropriate OpenJDK). You’ll need an older JDK because the JDK doesn’t offer a way to mask out new classes and interfaces from the standard library when compiling for backward compatibility – so you may unwittingly use Java 7 or Java 8-only classes unless you target the Java 6 VM.

(If you want to work with the JDBC3 driver you need Java 5 instead, but that’s getting seriously obsolete now).

Now it’s time to import the sources and configure Eclipse so it knows how to work with them:

  1. Register JDK 1.6 with Eclipse:
      • Window -> Preferences, Java, Installed JREs, Add…
      • Set JRE Name to “JDK 1.6”
      • Set JRE Home to the install directory of the JDK – not the contained JRE. It’ll be a directory named something like jdk1.6.0_45 and it’ll have a jre subdirectory. Select the jdk1.6.0_45 directory not the jre subdir.
      • Finish…

    and OK the preferences dialog.

  2. Import from git and create a project:
    • File -> Import, Git, Projects from Git, then follow the prompts for the repo
    • After the clone completes, Eclipse will prompt you to “Select a wizard for importing projects from git”. Choose Import as a General Project
    • Leave the project name as pgjdbc and hit Finish
  3. Configure ant
    • Get properties on the “pgjdbc” project
    • Go to the Builders tab
    • Press New…
    • In the resulting dialog’s Main tab set the Buildfile to the build.xml in the root of the PgJDBC checkout and set the base directory to the checkout root, e.g. buildfile = ${workspace_loc:/pgjdbc/build.xml} and base directory = ${workspace_loc:/pgjdbc}
    • In the Classpath tab, Add… the maven-ant-tasks jar, lib/maven-ant-tasks-2.1.3.jar from the PgJDBC lib dir
    • In the JRE tab, choose Separate JRE and then select the JDK 1.6 entry you created earlier
  4. Build the project – Project -> Build Project

Done! You can now modify and debug PgJDBC from within Eclipse, including setting breakpoints in your PgJDBC project and having them trap when debugging another project that’s using the same jar.

It isn’t completely automagic like with a native Eclipse project, though. To debug the driver from another project, you’ll need to add the driver from the jars/ directory produced by the build to your other project’s build path. It’ll be named something like postgresql-9.4-1200.jdbc4.jar. Add it under the project’s Properties, in Java Build Path -> Libraries -> Add External JARs.

Once you’ve added the driver JAR you can use your modified JAR, but breakpoints you set in the driver sources won’t get tripped when debugging another project. To enable that, tab open the newly added driver JAR in the Libraries tab and edit “Source attachment…”. Choose “Workspace Location” and, when prompted, your PgJDBC driver project.

The project sources are now linked. You can set breakpoints either by exploring the PgJDBC jar from the project that uses PgJDBC to find the package and source file required, or by opening the source file in the PgJDBC project. Either way it’ll work.

You’ll probably also want to set the loglevel=2 parameter to PgJDBC so that it emits trace logging information.

2 replies

Leave a Reply

Want to join the discussion?
Feel free to contribute!

Leave a Reply

Your email address will not be published. Required fields are marked *