0

I compiled my java project in netbeans this is my connection function:

public void connection() {
    try {
        Class.forName("com.mysql.jdbc.Driver");
        con = DriverManager.getConnection("jdbc:", "", "");
    } catch (Exception ex) {
        ex.printStackTrace();
    }
}

(i removed the username and password)

and after i compiled and run jar file i got error:

java.lang.ClassNotFoundException: com.mysql.jdbc.Driver
        at java.net.URLClassLoader.findClass(URLClassLoader.java:381)
        at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
        at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:331)
        at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
        at java.lang.Class.forName0(Native Method)
        at java.lang.Class.forName(Class.java:264)

It is important to note that in my classpath there is the mysql-connector-java-5.1.38-bin.jar jar libary

and i compiled it in netbeans. (actually in eclipse after compiled the jar file open successfully without errors...)

tnx a lot

OneCricketeer
  • 179,855
  • 19
  • 132
  • 245
user165210
  • 292
  • 4
  • 18

2 Answers2

2

The mysql driver might be on your classpath at compile time (as you're tagging netbeans), but it definitely is not on your classpath at runtime

You don't state what kind of application this is (standalone Java, Webapplication, Webservice, whatever) and how you start it, but you should check the way you run this application and add the jar to it according to the way you're starting it

Olaf Kock
  • 46,930
  • 8
  • 59
  • 90
-1

try using this

Class.forName("com.mysql.jdbc.Driver");
swipeales
  • 127
  • 1
  • 2
  • 12