0

I am trying to use the MySQL driver for java and I keep getting a ClassNotFoundException. I am not using an IDE at all, the program is being written in Vim and compiled at the Window's command line. It's for a class and the professor has told us we cannot use an IDE at all. I added the JAR file to the Window's PATH System Environment Variable but I'm still getting the error. I must be doing something wrong but most of the research I've done just pulls up that you need to add the driver to your IDE's classpath but I'm not using an IDE, just Windows.

Here's my code:

try {
    Class.forName("com.mysql.jdbc.Driver");
} 

catch (ClassNotFoundException e) {
    System.out.println("Where is your MySQL JDBC Driver?");
    e.printStackTrace();
    return;
}

And the error:

java.lang.ClassNotFoundException: com.mysql.jdbc.Driver
    at java.net.URLClassLoader$1.run(Unknown Source)
    at java.net.URLClassLoader$1.run(Unknown Source)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.net.URLClassLoader.findClass(Unknown Source)
    at java.lang.ClassLoader.loadClass(Unknown Source)
    at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
    at java.lang.ClassLoader.loadClass(Unknown Source)
    at java.lang.Class.forName0(Native Method)
    at java.lang.Class.forName(Unknown Source)
    at sqlAccess.<init>(sqlAccess.java:21)
    at Tester.DoOther(Tester.java:44)

etc

And this is what I added to my PATH variable under System Variables:

C:\Program Files\Java\jdk1.7.0_11\bin\mysql-connector-java-5.1.24-bin.jar

What am I missing?

Eric Leschinski
  • 146,994
  • 96
  • 417
  • 335
Kathryn Sager
  • 43
  • 2
  • 4

4 Answers4

5

You must add path to jar to CLASSPATH, not PATH

set "CLASSPATH=.;F:\Software Tech II\FinalProject\lib\mysql-connector-java-5.1.24-bin.jar"
java -cp %CLASSPATH% Tester
djangofan
  • 28,471
  • 61
  • 196
  • 289
infthi
  • 537
  • 1
  • 5
  • 19
2

Create a lib directory, put all your jars there (specially the jdbc connector http://dev.mysql.com/downloads/connector/j/)

then add it to your class path: http://www.ibm.com/developerworks/library/j-classpath-windows/ Good luck

royB
  • 12,779
  • 15
  • 58
  • 80
0

Add your mysql-connector-java-5.1.24-bin.jar file to C:\Program Files\Java\jdk1.7.0_11\jre\lib\ext and restart.

randomgood
  • 546
  • 1
  • 6
  • 19
0

Okay, this is what worked for me finally. Thanks to this thread

java programs not running due to seting classpath

I didn't know that classpath was best set through the -cp switch at the command line so running the command

 java -cp .;"F:\Software Tech II\FinalProject\lib\mysql-connector-java-5.1.24-bin.jar" Tester

stopped my errors. Thanks guys.

Community
  • 1
  • 1
Kathryn Sager
  • 43
  • 2
  • 4