2

I Installed postgresql and its driver which is "postgresql-9.3-1102.jdbc41.jar".

And my directory seems like :

> SQL
>     myJDBC.java
>     postgresql-9.3-1102.jdbc41.jar

I have tried :

javac -cp postgresql-9.3-1102.jdbc41.jar myJDBC.java
java -cp postgresql-9.3-1102.jdbc41.jar myJDBC

It compiles fine but when I try to execute it, I get error which is :

Error: Could not find or load main class myJDBC

What am I missing ?

Vivian Maya
  • 498
  • 1
  • 4
  • 11

1 Answers1

2

The current directory needs to be added to the runtime classpath

java -cp .:postgresql-9.3-1102.jdbc41.jar myJDBC
         ^
Reimeus
  • 158,255
  • 15
  • 216
  • 276
  • When I do that I get : java.lang.ClassNotFoundException: org.postgreql.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) – Vivian Maya Nov 07 '14 at 16:38
  • There ought to be a letter 's' in org.postgreql.Driver, check your code – mwarren Nov 07 '14 at 16:43
  • Sorry, my edit and comment had become obsolete in light of your `ClassNotFoundException`. – VH-NZZ Nov 07 '14 at 16:47
  • @okiharaherbst I'm truly sorry I'm kind of newbie I didn't get it what do you mean, The Jar file and myJDBC in same directory already ? how can use fully qualified package name ? – Vivian Maya Nov 07 '14 at 16:48
  • @VivianMaya change `org.postgreql.Driver` `->` `org.postgresql.Driver` (postgre*S*ql) in your code. And try again. – VH-NZZ Nov 07 '14 at 16:49
  • @okiharaherbst I fixed as reimeus answer,and I got error which are shared on first my comment ? – Vivian Maya Nov 07 '14 at 16:52
  • @VivianMaya Okay. Now prefix `MyJDBC` with the fully qualified package name, eg.: `com.example.subpackage.MyJDBC`. If it still doesn't work, then please edit your question and post the relevant portions of your code. – VH-NZZ Nov 07 '14 at 16:53
  • 1
    @okiharaherbst oh my god, I get it now what a mistake,I'm grateful,thank you for your attention... – Vivian Maya Nov 07 '14 at 16:55
  • Looks like the lesson to learn is never type a package name. Either copy-paste or use an IDE that provides a type-ahead feature to auto-suggest the parts of the package name. (See Intellij, NetBeans, Eclipse, JDeveloper.) – Basil Bourque Nov 07 '14 at 19:12
  • I'm using vim but I create a dictionary for these stuff at the end :) . – Vivian Maya Nov 07 '14 at 19:37
  • @BasilBourque I don't know, but I like vim and wouldn't trade it for Eclipse. Imo, string literals such as JDBC driver name, URI, user/pass credentials and queries are best passed as arguments anyway instead of having them compiled (just my 2¢). – VH-NZZ Nov 08 '14 at 12:52