skip to Main Content

I getting started on Java programming, and I have a CentOS 7.7.1908 VM with Java 1.8.0_242 OpenJDK and my IDE is Apache Netbeans 11.3. I am trying to develop a small program to read data from standard input and save it to a PostgreSQL database. In order to do that, I need to build my program into a .jar file and run it from the command line, since its intended usage is by piping the standard output of another program into my Java program. Apache Netbeans already comes with a bundled PostgreSQL driver, I could add properly a connection in the Services tab, Drivers section, and I could also properly browse my database schema in that tab.

However, when I try to include the PostgreSQL driver as a library of my project, I cannot see any “Library” section in the project properties window, as shown here. Apparently this is going to block me from continuing with my program, because I just tried setting up Apache Maven to add a Class-Path entry on my .jar’s manifest, manually passing a classpath by running my .jar with java -cp <directory of my PGSQL driver> -jar <JAR file>, setting a CLASSPATH environment variable, and none of them worked; my program keeps refusing to load the PostgreSQL JDBC driver.

This is what I see in the Properties dialog in my NetBeans:

enter image description here

This is my code, simplified as much as possible to show only the relevant sections.

  package com.example.pgsqlclient;

  import java.sql.*;
  import java.io.*;
  import java.util.Scanner;

  public class PGsqlClient
  {
     public static void main(String args[])
     {
        Connection PGSQL_connection = null;
        String PGSQL_URI = "jdbc:postgresql://192.168.1.74:5432/keypresses";
        PreparedStatement PGSQL_query = null;

        try
        {
           Class.forName("org.postgresql.Driver");
        } 
        catch (ClassNotFoundException ex)
        {
           System.err.println("PostgreSQL driver not found.");
           ex.printStackTrace();
           System.exit(1);
        }

        try { PGSQL_conexion.close();} 
        catch (SQLException ex) 
        { System.err.println("Failed to close the PostgreSQL connection."); System.exit(1); }
        System.exit(0);
     }
  }

And this is what I get when I run my .jar file

[centos@centos target]$ java -cp /home/centos/NetBeansProjects/pgsqlclient/target/lib/postgresql-42.2.10.jar -jar pgsqlclient-1.0-SNAPSHOT.jar 
PostgreSQL driver not found.
java.lang.ClassNotFoundException: org.postgresql.Driver
        at java.net.URLClassLoader.findClass(URLClassLoader.java:382)
        at java.lang.ClassLoader.loadClass(ClassLoader.java:419)
        at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:352)
        at java.lang.ClassLoader.loadClass(ClassLoader.java:352)
        at java.lang.Class.forName0(Native Method)
        at java.lang.Class.forName(Class.java:264)
        at com.example.pgsqlclient.PGsqlCliente.main(PGsqlCliente.java:28)

3

Answers


  1. It’s because you are using Maven … this stumped me for awhile too. That Libraries property is not there in a Maven project. You’re going to have to research how to make edits to your POM.xml file in your project, and add those dependencies as a Maven ‘goal’ in the pom.xml file (project files folder in your project file tree.)

    Example, for adding the Derby database in my projects.

        <dependency>
          <groupId>org.apache.derby</groupId>
          <artifactId>derby</artifactId>
          <version>10.8.3.0</version>
        </dependency>  
    

    As I’ve read, you either love Maven, or maybe don’t. I, love it. But it is a bit of a learning curve to find the edits to make in the file to include things. Google hepled me … add ‘so and so’ dependency in maven. Often, places even give you the lines to add to your pom.xml file while you’re collecting the library.

    Add … in Maven … to your searches online for adding libraries, etc.

    Edit … I found this:

    <dependency>
        <groupId>postgresql</groupId>
        <artifactId>postgresql</artifactId>
        <version>VERSION</version>
    </dependency>
    

    Because I’ve gotten to understand how this needs to be done in maven, I knew to search …

    “pom file edits to add PostgreSQL driver”

    Login or Signup to reply.
  2. If you use ant, you can add library from Project tab on the left side of the main screen. Under your project there is a Libraries file, over that file right click and choose Add JAR/Folder. Then choose your .jar file.

    If you use Maven, use Maven Repo to find proper library, then add dependency under dependencies tag in pom.xml. Then right click on your project and choose Build with dependencies and run your project.

    Login or Signup to reply.
  3. Nov,2021

    if your project is maven based :

    1. in your project go to Dependencies folder ==> Add Dependency

    2. You’d like name Artifact Id as my_lib

    3. You’d like name Group Id as my_lib

    4. You’ll probably name Version as 1

    5. leave remained empty

    6. Press Add

    Then new thing should appear in Dependencies folder

    7. Right click on that that

    8. Manually install artifact

    9. Click on Browse to look for JAR file

    10. Click on Install locally

    And you are done my friend

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search