Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

classpath JDBC driver

 
Onslow McCann
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 20669
65
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Onslow McCann wrote:should I add a -classpath instruction in this batch maybe so that the application can find the .jar file?

Definitely.
 
Onslow McCann
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 20669
65
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 18
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 20669
65
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 18
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Yes!!!
Thanks a lot for your help.
Came close to quitting there for a while.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic