3

I am trying to load a JDBC postgreSQL driver for a Java program. I know this is all over the Internet. I have tried many solutions, but none of them have worked for me.

The problem is that I get this error:

Exception in thread "main" java.lang.NoClassDefFoundError:    
classes/com/freire/test/JDBCExample/class
Caused by: java.lang.ClassNotFoundException: classes.com.freire.test.JDBCExample.class
at java.net.URLClassLoader$1.run(URLClassLoader.java:202)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:190)
at java.lang.ClassLoader.loadClass(ClassLoader.java:306)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:301)
at java.lang.ClassLoader.loadClass(ClassLoader.java:247)

And my code looks like this:

package com.freire.test;
import java.sql.DriverManager;
import java.sql.Connection;
import java.sql.SQLException;

public class JDBCExample 
{
    public static void main(String[] argv) 
    {
        System.out.println("JDBC Connection Testing");
        try {
            Class.forName("org.postgresql.Driver");
        } catch (ClassNotFoundException e) {
            System.out.println("PostgreSQL JDBC Driver not included!");
        }
    }
}

And the structure of my project looks like this:

myProject
 src
   com
     freire
       test
         JDBCExample.java
 classes
   com
     freire
       test
         JDBCExample.class
 lib
   postgresql-9.2-1003.jdbc3.jar

Compiling works fine:

java -d classes/ src/com/freire/test/JDBCExample.java

But executing produces the error:

java classes/com/freire/test/JDBCExample

Worth to say that I am working on a OS X Mountain Lion.

Any help will be appreciated.

1
  • You need to ensure that the postgresql-9.2-1003.jdbc3.jar is within the class path when you compile and run the program Commented Oct 7, 2013 at 2:02

3 Answers 3

4

Firstly you need to mention the package names using . instead of / while running the java program:

Go to your classes directory and run JDBCExample as :

java com.freire.test.JDBCExample

But it will now cry for the postgres driver class not found because postgres jar is missing in the classpath.So you need to use the classpath option while running the program and add your postgres jar to the classpath:

for windows:

java -cp .;../lib/postgresql-9.2-1003.jdbc3.jar com.freire.test.JDBCExample

for linux:

java -cp .:../lib/postgresql-9.2-1003.jdbc3.jar com.freire.test.JDBCExample
Sign up to request clarification or add additional context in comments.

6 Comments

I have been trying to do so but unsuccessfully for a long time so far. How can I do that?
@ivantxo I have mentioned everything in my answer. Just use the last option mentioned in my answer and you should be good.
Thanks but this gives me a list of options of the usage for java command. Then several lines of postgres-9.2.jdbc3.jar errors, like this one: ../lib/postgresql-9.2-1003.jdbc3.jar: line 1: PK: command not found or ../lib/postgresql-9.2-1003.jdbc3.jar: command substitution: line 9: syntax error: unexpected end of file
@ivantxo what operating system are u using?
OS X Mountain Lion. I think you have edited your answer. Will try now. Thanks
|
1

On Linux execute following:

javac -cp '.:postgresql-9.1-901.jdbc4.jar' postgresjavatest.java

java -cp '.:postgresql-9.1-901.jdbc4.jar' postgresjavatest

It will jdbc drivers for you. make sure jar file is on same location

Comments

0

You need to ensure that the postgresql-9.2-1003.jdbc3.jar is within the class path when you compile and run the program

Try using

javac -cp lib/postgresql-9.2-1003.jdbc3.jar -d classes/ src/com/freire/test/JDBCExample.java

to compile the application and

java -cp lib/postgresql-9.2-1003.jdbc3.jar;./classes com.freire.test.JDBCExample

to run it...

nb As Juned has pointed, technically, you don't need the lib/postgresql-9.2-1003.jdbc3.jar references in the classpath, but consider it an demonstration of how to included compile time dependencies within the complication process ;)

7 Comments

jar is not required in classapth during compilation as the driver class is loaded only at the run time.
@JunedAhsan And it's going to hurt how? (And yes, you are right...consider it an extended exercise in compilation ;))
It is not going to hurt that is why I have not hurt you with a negative vote ;-)
@JunedAhsan Trying this gives me: -bash: ./classes: is a directory and doesn't do anything. Getting rid of the ./classes gives me: ClassNotFoundException again.
Sorry, Windows :P, try using -cp lib/postgresql-9.2-1003.jdbc3.jar:./classes
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.