1

I went through a lot of questions like this and this which were almost same as mine ,but couldn't help me in solving my problem. I am hence posting it for help.
I was trying to use the JDBC driver with PostgreSQL and followed this tutorial. I tried the same Java program on Eclipse as well as trying to compile from the terminal. After putting the postgresql-9.4-1206-jdbc4.jar in the library folder in Eclipse, it compiles and runs perfectly in the IDE. Now I have put my JDBCExample.java and postgresql-9.4-1206-jdbc4.jar in the same folder test. But when I try in my terminal with this,

cd test
javac JDBCExample.java
java JDBCExample -cp postgresql-9.4-1206-jdbc4.jar

I get

-------- PostgreSQL JDBC Connection Testing ------------
Where is your PostgreSQL JDBC Driver? Include in your library path!
java.lang.ClassNotFoundException: org.postgresql.Driver
    at java.net.URLClassLoader$1.run(URLClassLoader.java:366)
    at java.net.URLClassLoader$1.run(URLClassLoader.java:355)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.net.URLClassLoader.findClass(URLClassLoader.java:354)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:425)
    at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:308)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:358)
    at java.lang.Class.forName0(Native Method)
    at java.lang.Class.forName(Class.java:191)
    at JDBCExample.main(JDBCExample.java:14)

I also tried rewriting the second command as java JDBCExample -cp .:postgresql-9.4-1206-jdbc4.jar but there is no change. What is wrong here?

6
  • 3
    reverse the order, -cp ... before JDBCExample; you are currently passing the parameters -cp and postgresql-9.4-1206-jdbc4.jar to your application instead of to Java. Commented Dec 12, 2015 at 10:24
  • When I do that java -cp postgresql-9.4-1206-jdbc4.jar JDBCExample , I am getting Could not find or load main class JDBCExample error. Commented Dec 12, 2015 at 10:33
  • 2
    @zorro_blue Because the -cp option replaces the classpath, so you also have to specify where to find JDBCExample.class, e.g. -cp .;postgresql-9.4-1206-jdbc4.jar to include the local directory. Commented Dec 12, 2015 at 10:39
  • @Andreas Like this java -cp .;postgresql-9.4-1206-jdbc4.jar ~/test/JDBCExample ? That gives me a command not found error :( Commented Dec 12, 2015 at 10:49
  • 2
    @zorro_blue: Replace the semicolon with a colon if you are not under Windows. Commented Dec 12, 2015 at 11:10

1 Answer 1

0

As discussed in the comments, setting up the call to be

java -cp .:postgresql-9.4-1206-jdbc4.jar ~/test/JDBCExample

worked.

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.