JDBC ClassNotFoundException (NetBeans, Classpath, Java)

If you get a java.lang.ClassNotFoundException error when loading a database driver using the statement:

Class.forName({nameOfYourDriverWhateverItIs}).newInstance();

You can either:

Set CLASSPATH in DOS

Go into DOS (Start/Run/cmd.exe):

set CLASSPATH=.;{pathToYourJarFile}

For example, if your jar file is at: C:/Program
Files/java/jdk1.6.0_01/lib/mysql-connector-java-5.0.6-bin.jar,

Type:

set CLASSPATH=.;C:/Program Files/java/jdk1.6.0_01/lib/mysql-connector-java-5.0.6-bin.jar

Now,

javac YourJavaFile.java
java YourJavaFile

That’s all. But it won’t work if you’re trying to run code in Netbeans.

Set Your Project’s Classpath in Netbeans

If you’re using netbeans, set your project’s classpath.

1. Right-click on your project.
2. Select “Properties.”
3. Click “Libraries.”
4. Click “Add JAR/Folder”
5. Choose your MYSQL driver JAR file.

HEY. Did you find this post useful as hell? I know you did. Then link to this page, Stumble it, Del.icio.us it, do whatever you can to pump this page up the Google SERPs so other people looking for this info can find it easier. Thanks.

Why This Has Anything to Do With SEO

Java netbeans classnotfoundexception

It took me over 30 minutes to find this solution. I was this close to throwing in the towel. So once I figured out the solution, I wrote a page I wish I found at the top of Google’s search results when I looked for “jdbc classnotfoundexception.” That way, other people looking for the same information won’t be frustrated like I was. Because I’m working in a long tail space, to get this page to rank high I just optimized the content. But I didn’t keyword spam or pepper my H tags with related terms. Instead, I optimized for you.

Yep, you.

Ok, so it’s not perfectly optimized for you if you’re an RSS subscriber of mine, since you’re interested in SEO, not Java. But if you found this page through Google, you’re thanking me now because:

  • I give you exactly what you were looking for.
  • I get straight to the point. Instead of starting off with a long irrelevant opening paragraph, I explain the problem and give you the answer. Instant gratification baby.
  • I keep it short. I use as few words as I can to save you time.
  • I keep it simple. Instead of trying to crack you up with dumb jokes or titilate you with fancy metaphors, I use simple words so even a caveman can “get it.”

Some people say ranking is about links, not content. Is it better to be tall and rich, or loyal and charismatic? Why are marketers trying to convince you its all about looks? (Yeah, just think about that for a minute :D)

Given two websites with equal visibility, the site that publishes the most compelling content will always win, showering the site with even more visibility. If marketing is all it takes, Paris Hilton would have sold more CDs.

Related Articles:

A few articles that both helped me and frustrated me (hint: natural outbound links to relevant, authoritative pages will help you win Google’s trust):

  • Establishing a Connection (The Java Tutorials)
  • Basic JDBC Concepts
  • Lesson: JDBC Basics

Related Posts

  • No related posts.

16 Responses to “JDBC ClassNotFoundException (NetBeans, Classpath, Java)”

  1. Thanks a lot. I was Scratching head for one hour about the ”java.lang.ClassNotFoundException’ error. Your article is very good one & provides one step solution.

    Please put other similiar errors also, which will help the beginners/ practitioners very much.
    Thanks Again

  2. Hey Rajeev,

    I’m glad you found the info useful.

  3. ‘’java.lang.ClassNotFoundException’ error
    thanks a lot man..i was breaking my head for so long…thanks a lot again..it helped me a lot…

  4. I’ve just spent 2 and a half days trying to figure this one out. A lifesaver !! I was so close to trashing my computer!

  5. Thank you, you solved my problem. I came upon your page through a Google hit. I had tried looking through the project’s properties but as luck would have it, the dialog has no maximise button and the button to add jar/folder was not visible without increasing the dialog’s size.

    Cheers

  6. This helped me, but I also had to try out adding a few different derby jar files to my project library, and I had to try out a couple variations of jdbc url.

    I created a database (COFFEEBREAK in Sun’s JDBC Tutorial) using Tools -> java DB database -> create Java DB database in the Netbeans menu, and then I wanted to reach that database from a java class, running from the IDE as usual. I wound up adding three jar files to my project libraries - ~\.netbeans\5.5.1\jdbc-drivers\derbyclient.jar, C:\Sun\SDK\javadb\lib\derby.jar, and C:\Sun\SDK\javadb\lib\derbytools.jar. (I’m not sure I needed all of these, but definitely the first of the three did not do it for me).

    I experimented with jdbc.derby://localhost:1527/COFFEEBREAK vs. jdbc:derby:COFFEEBREAK (I needed the localhost:1527), and I experimented with org.apache.derby.jdbc.EmbeddedDriver vs. org.apache.derby.jdbc.ClientDriver. (I needed the ClientDriver, not the EmbeddedDriver.)

  7. Hey this (adding derby.jar etc) worked but when I tried it on command prompt it still gave me errors. I’ve been through all the tutorials they got me even more confused.

    My homework involves using Java DB & RMI
    To run I typed this:

    C:\Documents and Settings\andy\Desktop\uni stuff\cmp416\A3\sqlsample\build\classes> rmic ServiceProvider

    (works without errors)

    C:\Documents and Settings\andy\Desktop\uni stuff\cmp416\A3\sqlsample\build\classes> start rmiregistry

    (works without errors)

    C:\Documents and Settings\andy\Desktop\uni stuff\cmp416\A3\sqlsample\build\classes> start java StudServer

    (starts server without errors but then gives this database error :

    cannot connect to database

    java.lang.ClassNotFoundException: org.apache.derby.jdbc.ClientDriver )

  8. K I much appreciate this post, it helped but not immediatly. I have 6.0 and 5.5.1 Netbeans installed. The derby seems to have been installed with 5.5.1 and I expected 6.0 to install a new Jar. However I could only find the old one under a slightly unexpected path of: C:\Documents and Settings\jkl\.netbeans\5.5.1\db-derby-10.1.3.1-bin\lib\derbyclient.jar . I expected to find it in the Netbeans fold. Anyways once adding this jar to my libraries the exception was no longer thrown and the driver loaded successfully. Awesome stuff.

    Thanks

  9. Thank you so much. I was pulling my hair out trying to figure out this error message. I’m using NetBeans6 and and JDK 6. For me I had a little different squence of events to do, but the basic theme was the same.

    1. Right-click on your project.
    2. Select “Properties.”
    3. Click “Add Library…”
    4. Choose “Java DB Driver”
    5. Click “OK”.

    Here’s a sample of the code I use to create the database from the Java book I’m reading.

    public void createDatabase() {
    // create the database
    String data = “jdbc:derby:presidents;create=true”;
    try {
    // load the driver
    Class.forName(”org.apache.derby.jdbc.EmbeddedDriver”);
    // create the connection
    Connection conn = DriverManager.getConnection(data);

  10. I wish to include the package org.apache.http.* in my project but finding it difficult..

    Even after finding the classpath and copying the HHTPclient in it I get a error that the package does not exist..

    Can you please help me??

  11. lol, Thank you Very much. just what i was looking for!

    perfect!

  12. THANK YOU SO MUCH. I too spent 2-3 hours on Google trying various options including:
    - adding the JAR to Environment Variables
    - adding it to the classpath in Netbeans under Options
    - adding it to the jdk/lib/ext folder

    Then I tried your suggestion by adding it to my project’s library too and it worked.

  13. Thanks a lot! I was searching for a way to add jar files to netbeans for more than two hours :-)

    Regards,
    D

  14. maaan maaan u’re the one amigo u’re the one
    i was getting sooooo frustrated with it
    thx a lot bro…………EUUUUURRREKAAAAAA!!!!!!!

  15. i am soooo greateful to u for a solution for this nagging problem that has been killing me for quite some time.thanx friend !

  16. Set Your Project’s Classpath in Netbeans…

    THANK YOU. Yes, useful indeed. I’d been trying to figure it out for hours…hours!! Anything to get your answer boosted is good on my end.

What's Your Take?