5

My java file jdbc11.java was compiled successfully with javac jdbc11.java command in cmd, after that when I tried to to run java jdbc11 I got this exception:

java.lang.ClassNotFoundException: com.mysql.jdbc:Driver

refering to this code in the file

Class.forName("com.mysql.jdbc.Driver");

, when I tried it in eclipse ,I added to "Java Build Path" the external jar : mysql-connector-java-5.1.20-bin.jar and it run successfully . in case I didn't fix it with eclipse what should I done in the first try with java jdbc11 command in order to make work ? note: the jar in the same dir with the jdbc11.java

Bhavik Ambani
  • 6,557
  • 14
  • 55
  • 86
nabil
  • 904
  • 3
  • 12
  • 28

3 Answers3

5

Try adding the mysql-connector jar to the classpath when you execute your command-line code:

java -cp mysql-connector-java-5.1.20-bin.jar;. jdbc11

http://docs.oracle.com/javase/1.5.0/docs/tooldocs/windows/classpath.html

the_marcelo_r
  • 1,847
  • 22
  • 35
  • `Error: Could not find or load main class jdbc11` – nabil Dec 24 '12 at 17:39
  • I have a feeling that you trying this on a Windows machine so: `java -cp mysql-connector-java-5.1.20-bin.jar;. jdbc11` – the_marcelo_r Dec 24 '12 at 17:57
  • I mentioned in the question cmd, thanks man it worked now can you explain why please ? – nabil Dec 24 '12 at 18:10
  • Of Course! The dot (.) means "this location/directory" so you have to include the current location within the classpath to load everything that is around. I've also mentioned "Windows" because the classpath delimiter is not the same for UNIX(:) & Windows(;) systems. – the_marcelo_r Dec 24 '12 at 18:33
1

You have to add -classpath in the execute command

java -classpath mysql-connector-java-5.1.20-bin.jar jdbc11

Weibo
  • 1,042
  • 8
  • 18
  • Error: Could not find or load main class jdbc11 – nabil Dec 24 '12 at 17:40
  • Do you have package for jdbc11.java, if so, add the package name in front of jdbc11 – Weibo Dec 24 '12 at 17:44
  • Could you please share your jdbc11.java? – Weibo Dec 24 '12 at 17:53
  • the file in this page: http://www.developer.com/java/data/jdbc-and-mysql-run-the-programs.html Please look in the page for "/*File Jdbc11.java" – nabil Dec 24 '12 at 17:59
  • if you are working on unix-like platform, please add ":" at the end of above command like this: **java -classpath mysql-connector-java-5.1.20-bin.jar: jdbc11** if you are working on windows, please add ";" at the end of the command like this: **java -classpath mysql-connector-java-5.1.20-bin.jar; jdbc11** This is to set the classpath for class jdbc11 during the runtime; BTW, -classpath is the same as -cp – Weibo Dec 24 '12 at 18:34
0

If I'm understanding you allright, what you are missing is a -cp option in your java command line, which is what eclispe would do internally if you were to put it in the build path.

Miquel
  • 15,405
  • 8
  • 54
  • 87
  • `java -cp mysql-connector-java-5.1.20-bin.jar jdbc11` tells me "couldn't find the main class jdbc11. – nabil Dec 24 '12 at 17:37