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:
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)
