Reputation: 2203
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.
Upvotes: 0
Views: 709
Reputation: 2188
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.
Upvotes: 1