This week's book giveaways are in the Java EE and JavaScript forums.
We're giving away four copies each of The Java EE 7 Tutorial Volume 1 or Volume 2(winners choice) and jQuery UI in Action and have the authors on-line!
See this thread and this one for details.
The moose likes JDBC and the fly likes classpath JDBC driver Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of The Java EE 7 Tutorial Volume 1 or Volume 2 this week in the Java EE forum
or jQuery UI in Action in the JavaScript forum!
JavaRanch » Java Forums » Databases » JDBC
Bookmark "classpath JDBC driver" Watch "classpath JDBC driver" New topic
Author

classpath JDBC driver

Onslow McCann
Greenhorn

Joined: Aug 29, 2007
Posts: 18
Hi,

I have installed C:\mysql-connector-java-5.1.6\mysql-connector-java-5.1.6-bin.jar containing the JDBC driver.class
When I run a certain java application I made (years ago) and press a button to submit a new record to a database I get...

Could not load JDBC driver: java.lang.ClassNotFoundException:com.mysql.jdbc.driver

Now I know I need to do something with classpath, but I don't have a CLASSPATH system variable, and I've read that I shouldn't make one either.
I've tried adding the .jar to User variable PATH but this doesn't seem to work either.

PATH C:\Program Files\Java\jdk1.6.0_23\bin;C:\mysql-connector-java-5.1.6\mysql-connector-java-5.1.6-bin.jar

NowI run the java program with a batch file which simply contains
java SchrijfNaarDatabase
should I add a -classpath instruction in this batch maybe so that the application can find the .jar file?

I've been searching for the solution on internet for many hours over many days and feel kinda foolish for not being able to work it out.
I know this app used to work in the past, but somehow it doesn't anymore.

I would greatly appreciate it if someone could give me some advice.





Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19672
    
  18

Onslow McCann wrote:should I add a -classpath instruction in this batch maybe so that the application can find the .jar file?

Definitely.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Onslow McCann
Greenhorn

Joined: Aug 29, 2007
Posts: 18
Thanks for your reply quick reply Rob,
I had tried some variations, but none seem to work really.

java -classpath C:\mysql-connector-java-5.1.6 SchrijfNaarDatabase

java -classpath C:\mysql-connector-java-5.1.6\mysql-connector-java-5.1.6-bin.jar SchrijfNaarDatabase

java SchrijfNaarDatabase -classpath C:\mysql-connector-java-5.1.6\mysql-connector-java-5.1.6-bin.jar

java SchrijfNaarDatabase -classpath C:\mysql-connector-java-5.1.6

I can't really figure out what I'm doing wrong.
versions 3 and 4 open my application allright, but still can't find the driver.


Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19672
    
  18

Versions 3 and 4 will pass the -classpath and folder / JAR file as arguments to the main method of your Java class.

Version 2 should actually work if the path is correct. Can you show us the error you're getting?
Onslow McCann
Greenhorn

Joined: Aug 29, 2007
Posts: 18
Microsoft Windows XP [versie 5.1.2600]
(C) Copyright 1985-2001 Microsoft Corp.

Map van C:\Documents and Settings\Chris\Mijn documenten\java_2\0001Movie_databa
se_005

20-12-2011 13:28 <DIR> .
20-12-2011 13:28 <DIR> ..
20-12-2011 13:19 107 compileren.bat
20-12-2011 13:28 3.160 Database.class
02-09-2010 10:39 5.499 Database.java
20-12-2011 13:28 2.170 InpoetScreen.class
02-09-2010 11:04 2.307 InpoetScreen.java
20-12-2011 13:28 523 SchrijfNaarDatabase.class
02-09-2010 10:42 477 SchrijfNaarDatabase.java
20-12-2011 13:50 101 start.bat
9 bestand(en) 15.373 bytes
2 map(pen) 1.926.512.640 bytes beschikbaar

C:\Documents and Settings\Chris\Mijn documenten\java_2\0001Movie_database_005>st
art.bat

C:\Documents and Settings\Chris\Mijn documenten\java_2\0001Movie_database_005>ja
va -classpath C:\mysql-connector-java-5.1.6\mysql-connector-java-5.1.6-bin.jar S
chrijfNaarDatabase
Exception in thread "main" java.lang.NoClassDefFoundError: SchrijfNaarDatabase
Caused by: java.lang.ClassNotFoundException: SchrijfNaarDatabase
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)
Could not find the main class: SchrijfNaarDatabase. Program will exit.
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19672
    
  18

Add ";." (without the quotes) to your class path. I forgot that if you explicitly set the class path, the current folder is no longer implicitly part of the class path. It needs to be a part of the explicit class path:
java -classpath C:\mysql-connector-java-5.1.6\mysql-connector-java-5.1.6-bin.jar;. SchrijfNaarDatabase
Onslow McCann
Greenhorn

Joined: Aug 29, 2007
Posts: 18

Yes!!!
Thanks a lot for your help.
Came close to quitting there for a while.
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: classpath JDBC driver