skip to Main Content

I am a beginner in Java coming from four years in JavaScript and Python. My goal is to run my Java program that connects to Postgres and create a table. My expected result is that java Main will output this line: System.out.println("Started...");

My actual result is that I get:

org.postgresql.Driver
java.lang.ClassNotFoundException: org.postgresql.Driver
    at java.base/jdk.internal.loader.BuiltinClassLoader.loadClass(BuiltinClassLoader.java:641)
    at java.base/jdk.internal.loader.ClassLoaders$AppClassLoader.loadClass(ClassLoaders.java:188)
    at java.base/java.lang.ClassLoader.loadClass(ClassLoader.java:521)
    at java.base/java.lang.Class.forName0(Native Method)
    at java.base/java.lang.Class.forName(Class.java:383)
    at java.base/java.lang.Class.forName(Class.java:376)
    at SQLQueriesTool.connect(main.java:94)
    at Main.main(main.java:20)

I saw some posts talking about pom.xml so I made one:

<project>
    <modelVersion>4.0.0</modelVersion>
    
    <groupId>com.pluto.app</groupId>
    <artifactId>library-app</artifactId>
    <version>1</version>

    <dependencies>
        <dependency>
            <groupId>org.postgresql</groupId>
            <artifactId>postgresql</artifactId>
            <version>42.2.23</version>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-compiler-plugin</artifactId>
            <version>3.8.1</version>
            <configuration>
                <source>7</source>
                <target>7</target>
            </configuration>
            </plugin>
        </plugins>
    </build>
</project>

Then I do mvn clean install and get [INFO] BUILD SUCCESS

Then I do mvn compile, same result, "BUILD SUCCESS"

Then I type java Main and expect the program to run with the postgres thing installed.

Here’s the program’s code

 private static final String DATABASE_DRIVER = "org.postgresql.Driver";


    public Connection connect() {
        boolean noConnectionYet = connection == null;
        if (noConnectionYet)
        {         
            try
            {
                Class.forName(DATABASE_DRIVER);
                    System.out.println("Dev db connection established");
                    connection = (Connection) DriverManager.getConnection(url, username, password);
                }
            catch (ClassNotFoundException | SQLException e)
            {
                System.out.println("PROBLEMn########");
                System.out.println(e.getMessage());
                e.printStackTrace();
                System.out.println("########n");
            }
        }
        return connection;
    }

My main.java file entrypoint is at src/main/java/com/main/main.java and I’m out of ideas about what to say here

I’m sure there is nothing special going on, and that the solution is to complete some obvious step I left out because I’m a beginner in this language & environment.

2

Answers


  1. maven is just a build tool. You’ve told it to fetch postgres (so it is now downloaded and unless you messed with settings, in your ~/.m2 folder), and it has put that on the classpath when it built your app.

    When you then type java Main, you’re not using maven and therefore, java has absolutely no idea you want postgres on the classpath.

    You have to either [A] add that yourself, or [B] run your app via maven (which knows that needs to be added), or [C] ask maven to make a distributable version of your application (either have it add a classpath to the jar’s manifest, or, pack your postgres dep into the jar which is slow and results in a big jar that contains all your dependencies in one large file), then run that.

    Can be as simple as java -cp .:/path/to/your/home/.m2/path/to/postgres-driver.jar, but in general if you’re going to use maven to download the dependencies, it’s a better idea to also use maven to run your app. The maven tutorials and documentation have plenty on how to use maven to run your app (and how to tell maven which class contains your main method, and so on).

    Login or Signup to reply.
  2. The issue may be one of these:

    1. Check if you are using a maven project. Do update the project. To do so, you may take the following steps.
      right-click on project-> maven-> update project.
    2. Postgres drivers are not in the classpath.
      However, using your code I have created a small project that connects to the Postgres database. You may check these codes, set the database name, username, and password, and then check.

    `

    package connection;    
    import java.sql.Connection;
    import java.sql.DriverManager;
    import java.sql.SQLException;
    
    public class DatabaseConnection {
        
        private static final String DATABASE_DRIVER = "org.postgresql.Driver";
    
      private static Connection connection= null;
        public static Connection connect() {
            boolean noConnectionYet = connection == null;
            if (noConnectionYet)
            {         
                try
                {
                    Class.forName(DATABASE_DRIVER);
                        // keep databasename, username and password
                        connection = (Connection) DriverManager.getConnection("jdbc:postgresql://localhost:5432/datbasename", "username", "userpassword");
                    }
                catch (ClassNotFoundException | SQLException e)
                {
                    System.out.println("PROBLEMn########");
                    System.out.println(e.getMessage());
                    e.printStackTrace();
                    System.out.println("########n");
                }
            }
            return connection;
        }
    
    }
    

    `

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