0

I am trying to use JDBC to access the MySql database on my computer. I get this error message

java.lang.ClassNotFoundException: com.mysql.jdbc.Driver
at java.net.URLClassLoader$1.run(URLClassLoader.java:366)
at java.net.URLClassLoader$1.run(URLClassLoader.java:355)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:354)
at java.lang.ClassLoader.loadClass(ClassLoader.java:425)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:308)
at java.lang.ClassLoader.loadClass(ClassLoader.java:358)
at java.lang.Class.forName0(Native Method)
at java.lang.Class.forName(Class.java:191)
at FunctionClass.special_function(FunctionClass.java:72)
at FunctionClass.<init>(FunctionClass.java:25)
at EvaluateFunctions.main(EvaluateFunctions.java:12)

I've seen on the other posts that this is due to the fact that the driver is not in the lib directory of the JDK. I tried that and it still doesn't work. I added the *.jar file to my "/usr/lib/default-java/lib/" folder on Xubuntu. I am not using any kind of servers. Here is my code that connects to the database:

Class.forName("com.mysql.jdbc.Driver");

Does anyone know what I am missing here? Thanks for all the help in advance.

3
  • Have you added the driver in your build path Commented Nov 24, 2014 at 2:20
  • check this link stackoverflow.com/questions/27085364/… Commented Nov 24, 2014 at 2:23
  • @johny Thanks. Now it works when I do it on eclipse. However, do you know a link where I can do this through the command line? I tried some and they just didn't work. Commented Nov 24, 2014 at 2:48

1 Answer 1

1

If you are using eclipse or some ide you can add the jar files to your build path.

It can be done by checking this link for eclipse. https://stackoverflow.com/a/27085441/4083590

If you are using command prompt you should add the jar files to your CLASSPATH. It can be done as shown below.

  • Linux:

    CLASSPATH=$CLASSPATH;{Current working directory};{Direct Path to .jar file}

    CLASSPATH=$CLASSPATH;/home/users/xyz/workspace/;/home/users/xyz/workspace/xyz.jar

    Note: ./ can be used to signify current working directory

  • Windows:

    set CLASSPATH=%CLASSPATH%:{Current working directory}:{Direct path to .jar file}

    set CLASSPATH=%CLASSPATH%:C:\users\xyz\workspace:C:\users\xyz\workspace\xyz.jar

After doing this you would be able to compile your program.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.