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?

Community
  • 1
  • 1
Rameshwar Bhaskaran
  • 345
  • 1
  • 4
  • 16
  • 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. – Mark Rotteveel Dec 12 '15 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. – Rameshwar Bhaskaran Dec 12 '15 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. – Andreas Dec 12 '15 at 10:39
  • @Andreas Like this `java -cp .;postgresql-9.4-1206-jdbc4.jar ~/test/JDBCExample` ? That gives me a command not found error :( – Rameshwar Bhaskaran Dec 12 '15 at 10:49
  • 2
    @zorro_blue: Replace the semicolon with a colon if you are not under Windows. – Chriki Dec 12 '15 at 11:10
  • @Chriki It worked!! Thanks !! :D – Rameshwar Bhaskaran Dec 12 '15 at 11:13

1 Answers1

0

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

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

worked.

Jan
  • 13,738
  • 3
  • 30
  • 55