0
votes

When I copy mysql jdbc driver to JDK's\jre\lib\ext, it execute perfectly well. Now, I want to use the jdbc by specifying its classpath to environment variable. But, after doing so, my program throws exception :

"java.sql.SQLException: No suitable driver found for jdbc:mysql://localhost/books"

How do I set the classpath?

2

2 Answers

4
votes

You should not be putting ANY JARs in the jre/lib/ext folder.

You set the CLASSPATH using the -classpath option on javac.exe when you compile and java.exe when you run. Make sure that your code and all 3rd party JARs are in the CLASSPATH when you compile and run. Windows uses the semi-colon as the separator; Linux uses colon.

Maybe you need to start here:

http://docs.oracle.com/javase/tutorial/getStarted/cupojava/win32.html

2
votes

You can include any jar files you need by specifying them in the java command with the -cp switch (which is identical to -classpath. For example if the JDBC driver's name is 'myjdbc.jar' then you would execute your program as follows:

java -cp myjdbc.jar your.package.YourClass

If you have more jar files, you can separate them with a semi-colon on Windows or colon on Linux/Unix. Commonly the current directly is also included, and we put all needed jar files in a /lib folder, so it would look something like this (on Windows):

java -cp .;lib/myjdbc.jar your.package.YourClass

Also, if you have lots of jar files, it would be more convenient to put them all in the /lib folder and have something like this:

java -cp .;lib/* your.package.YourClass