Results 1 to 8 of 8
Like Tree1Likes
  • 1 Post By goldest

Thread: Problem with loading driver

  1. #1
    b01g is offline Member
    Join Date
    Jan 2011
    Posts
    7
    Rep Power
    0

    Default Problem with loading driver

    Hello,
    Trying to load Connector/J driver for accesing MySQL server. JDBC
    # import java.sql.Connection;
    # import java.sql.DriverManager;
    # import java.sql.SQLException;
    #
    # public class LoadDriver {
    # public static void main(String[] args) {
    # try {
    #
    # Class.forName("com.mysql.jdbc.Driver").newInstance ();
    # } catch (Exception ex) {
    # ex.printStackTrace();
    # }
    # }
    # }


    i get this error :

    Exception in thread "main" java.lang.NoClassDefFoundError: LoadDriver
    Caused by: java.lang.ClassNotFoundException: LoadDriver
    at java.net.URLClassLoader$1.run(URLClassLoader.java: 202)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.net.URLClassLoader.findClass(URLClassLoader.j ava:190)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:3 07)
    at sun.misc.Launcher$AppClassLoader.loadClass(Launche r.java:301)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:2 48)
    Could not find the main class: LoadDriver. Program will exit.

    I already created a environment variable named CLASSPATH and give it value = C:\mysql-connector-java-5.1.14\mysql-connector-java-5.1.14-bin.jar.
    What can be the problem. Please help. Thnx in advance.

  2. #2
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,050
    Rep Power
    20

    Default

    It's not the mysql it can't find.
    It's your LoadDriver class it can't find.

    How are you executing this, in what directory, and where is the LoadDriver.class file?

  3. #3
    b01g is offline Member
    Join Date
    Jan 2011
    Posts
    7
    Rep Power
    0

    Default

    I keep my LoadDriver.java file in C:\Java\src.
    I can't figure out what value should i put in CLASSPATH - the folder location of conncetor/j or the jar file? Should I put the class that i created in that folder too? I'am totaly wracked. Please don't leave me without an answer. I had been on this since yesterday and without results. Thnx in advance for your patience.

  4. #4
    Petr's Avatar
    Petr is offline Senior Member
    Join Date
    Jan 2011
    Location
    Russia
    Posts
    620
    Rep Power
    4

    Default

    Hi,
    You have mistake in classpath. You can run the class as follows.
    src/LoadDriver.java
    src/mysql-connector-java-5.1.8-bin.jar
    Java Code:
    javac LoadDriver.java
    java -cp mysql-connector-java-5.1.8-bin.jar: LoadDriver
    Last edited by Petr; 01-17-2011 at 05:49 PM.

  5. #5
    b01g is offline Member
    Join Date
    Jan 2011
    Posts
    7
    Rep Power
    0

    Default

    The same thing:(

  6. #6
    goldest's Avatar
    goldest is offline Senior Member
    Join Date
    Oct 2009
    Location
    Pune, India
    Posts
    469
    Rep Power
    5

    Wink

    OK. Let's say your code file LoadDriver.java is in the directory C:\Java\src

    You can navigate from the command prompt to that directory and use the javac to compile the java file. Up to here, the things must work fine.

    Java Code:
    C:\Java\src>javac LoadDriver.java
    After running above command, the LoadDriver.class file would get generated at the same location.

    Now, suppose that the jar file that contains your mysql driver, is in the C:\Java\lib directory. Now, while running your program, you need to make sure that your compiler will see both the .class files, the one generated above and the one which is inside the jar file.

    For this purpose, you need to use the -cp option of the java command. By using this, you can provide the class path where compiler will search for the .class files required.

    So, try following command,

    Java Code:
    C:\Java\src>java -cp C:\Java\lib\mysql-connector-java-5.1.8-bin.jar;. LoadDriver
    See the class path above carefully. The portion before the semicolon (;) gives the full path to the jar file and portion after it, which is a dot (.), tells the compiler to look into the current directory for class files.

    Keep it in mind that, when you are referring to the jar file that contains the required class files, always mention the .jar file name in the command and not the directory path till that jar file.

    Hope that helps,

    Goldest
    Java Is A Funny Language... Really!
    Click on * and add to member reputation, if you find their advices/solutions effective.

  7. #7
    goldest's Avatar
    goldest is offline Senior Member
    Join Date
    Oct 2009
    Location
    Pune, India
    Posts
    469
    Rep Power
    5

    Wink

    In case if you are interested to know more about java and javac command line options, refer to below links.

    For java: java

    For javac: javac

    Hope that helps,

    Goldest
    yazen likes this.
    Java Is A Funny Language... Really!
    Click on * and add to member reputation, if you find their advices/solutions effective.

  8. #8
    yazen is offline Member
    Join Date
    Jul 2011
    Posts
    1
    Rep Power
    0

    Default

    Quote Originally Posted by goldest View Post
    In case if you are interested to know more about java and javac command line options, refer to below links.

    For java: java

    For javac: javac

    Hope that helps,

    Goldest


    Your answer is very much appreciated Mr. Goldest.


    I finally was able to run my sample without errors.

    Got the answer I was looking for.

Similar Threads

  1. Loading of JDBC Driver
    By Riyaz.hk77 in forum Advanced Java
    Replies: 11
    Last Post: 11-11-2010, 08:53 AM
  2. ODBC driver problem windows 7
    By dewitrydan in forum New To Java
    Replies: 0
    Last Post: 11-07-2010, 11:01 AM
  3. driver problem
    By arjun_duddu in forum JDBC
    Replies: 3
    Last Post: 08-24-2010, 08:44 AM
  4. mysql driver problem
    By mokingsu in forum JDBC
    Replies: 4
    Last Post: 01-17-2008, 05:27 PM
  5. JDBC Driver problem
    By Swamipsn in forum New To Java
    Replies: 3
    Last Post: 08-09-2007, 03:55 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •