22

I am trying to connect to mysql database using java on windows7. In spite 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. I am actually using Django with Jython, which requires the JDBC driver to access the database. That is the reason why I have to do it using CLASSPATH only.

BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
Nitin Garg
  • 2,069
  • 6
  • 25
  • 50
  • with what are you building your project? – ant Apr 07 '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 07 '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 07 '10 at 11:12

9 Answers9

16

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 IDEs like Eclipse, Netbeans and IDEA.

That environment variable is in real world also considered a poor practice since it breaks portability. I.e. program X will run successfully while program Y won't run without altering the CLASSPATH. It's only "useful" for Sun Oracle to prevent that starters get tired of typing the same classpath again and again in the -cp or -classpath arguments when following Java tutorials. In real world, batch/shell files are preferred where just the entire command with -cp/-classpath argument is specified.

In your case you're using an IDE. The classpath is there called the "Build Path". In plain Java projects, it represents both the 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 about using the CLASSPATH environment variable. It was a mistake by Sun Oracle. They thought to convince starters, but it ended up to be only more confusing to starters as they incorrectly interpret that environment variable as the classpath.

See also:

Community
  • 1
  • 1
BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
  • 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 07 '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 07 '10 at 13:30
12

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.

ValGe
  • 339
  • 4
  • 11
  • Here is a link as of today, you can download the platform independent one from here: http://dev.mysql.com/downloads/connector/j/#downloads – SoluableNonagon Oct 07 '13 at 19:15
  • Complete path is yout actual java platform. For example in Linux amd64 can be /usr/lib/jvm/java-7-openjdk-amd64/jre/lib/ – snovelli Nov 07 '14 at 15:32
9

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

nandan
  • 686
  • 8
  • 11
  • 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. – Mike G Sep 23 '12 at 01:03
  • 3
    OMG you're the best! Setting classpath didn't work for me either! I'm using eclipse but it's similar. So for those of you that are using eclipse: right click on the project -> Run Configuration -> Classpath -> add it there. – Dao Lam May 12 '13 at 03:59
  • Thanks @DaoLam that worked for me. Up Vote for you! and you too, nandan – Abu Sulaiman Apr 16 '16 at 13:16
3

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

omid
  • 31
  • 1
3

I also had this problem before, but after I put/added mysql-connector-java-5.1.34-bin.jar (Download it from here) into the apache-tomcat-8.0.15\lib folder, and then ran my project, it really did work.

Note : Even after adding the jar file the error persists, then restart the Tomcat server and rerun you project again.

Rahul Gupta
  • 9,775
  • 7
  • 56
  • 69
Kyle
  • 81
  • 1
  • 3
2
  1. Open Netbeans IDE
  2. Right-click your Project.
  3. Select Properties.
  4. On the left-hand side click Libraries.
  5. Under "Compile" tab - click Add Jar/Folder button.
  6. Select Downloaded "mysql-connector-java-5.1.25-bin.jar" file (Download Connector/J from dev.mysql.com)
  7. Click OK
  8. Run Again... Its work.
Jaykumar Patel
  • 26,836
  • 12
  • 74
  • 76
2

If you are using maven, add the dependency to pom.xml should solve the problem.

<dependency>
    <groupId>mysql</groupId>
    <artifactId>mysql-connector-java</artifactId>
    <version>5.1.36</version>
</dependency>
Searene
  • 25,920
  • 39
  • 129
  • 186
1

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.

1

I had this same problem in Netbeans. Because I was using a tomcat connection pool as defined in context.xml I needed to add the jdbc jar to both the project (Properties->Libraries) and to the lib/ folder within my Tomcat server so it could be seen on startup.

JPB
  • 11
  • 2