I am trying to connect to mysql database using java on windows7. Inspite of adding the complete url of jdbcdriver jar file in CLASSPATH, java.lang.ClassNotFoundException: com.mysql.jdbc.Driver is thrown. Could anyone tell me what i am missing here?? it works if i add the jar file in project library but i want to do it by CLASSPATH itself. My classpath looks like this- C:\jython2.5.1\javalib\mysql-connector-java-5.1.12-bin.jar

I want to make it clear that this is not the actual project i am working on.Actually i am using django with jython which requires jdbc driver to access db. that is the reason why i have to do it using CLASSPATH only.

share|improve this question
with what are you building your project? – ant Apr 7 '10 at 10:26
...and if you're doing this from a command prompt, can you also show the exact command-line you're using? – Ash Apr 7 '10 at 10:30
i am using netbeans ide. Actually i was getting this error while using django with jython, so i thought i should try connecting to mysql database from plain java. – Nitin Garg Apr 7 '10 at 11:12

5 Answers

up vote 11 down vote accepted

The CLASSPATH environment variable is only used by the java.exe command and even then only when used without any of the -cp, -classpath, -jar arguments. It is ignored by IDE's.

That environment variable is in real world also considered a poor practice since it breaks portability. It's only "useful" for Sun to prevent that starters get tired of typing the same classpath again and again in the -cp or -classpath arguments. In real world, batch/shell files are preferred.

In your case you're using an IDE. The classpath is called the "build path" (it represents both compiletime and runtime classpath). You can configure it in the project's properties. You can add a complete folder, you can add individual/external JAR files, you can link projects, etcetera. Make use of it. Forget the whole CLASSPATH environment variable.

share|improve this answer
Thanx for you reply, Actually my project is on django and jython. jython runs on jvm and it needs jdbc drivers to access db. I dnt know of any other way to include a jar file. – Nitin Garg Apr 7 '10 at 13:03
The detailed answer depends on the runtime environment. At least, you can use sys.path environment variable (which is by the way similar to CLASSPATH and thus also unportable), or the command argument -Dpython.path. – BalusC Apr 7 '10 at 13:30

What finally helped me out was to copy the mysql-connector-java-5.1.15-bin.jar to \jre\lib and to \jre\lib\ext both(!) even though I did all the classpathing circus Java offers :) Environment was pure notepad/commandline though.

share|improve this answer

What worked with me using Netbeans was: Run > Set Project Configuration > Customize. Under Libraries > Add Library. Added MySQL JDBC Driver (I assume it appeared in list because I copied the jar file to the jre\lib\ext folder. And it worked seamlessly.

I tried setting classpath but that did not work. I am using Netbeans 7.0

share|improve this answer
Thank god for this answer. I've been fighting with this problem for the last 2 1/2 hours, and you fixed it for me. – mikeTheLiar Sep 23 '12 at 1:03

simply do a right click on your project in "Netbeans" select properties then click on "libraries " then click on "add library..." button then select "MySQL JDBC Driver" and click on "add library" button then on "OK" button

share|improve this answer

In Netbeans IDE just Check the properties of Project on which you working on,in properties window go to 'library' tag, in diolog box just add your mysql-connector-java-**.jar file.

share|improve this answer

Your Answer

 
or
required, but never shown
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.