*
The moose likes JDBC and the fly likes classNotFoundException for Driver Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Databases » JDBC
Bookmark "classNotFoundException for Driver" Watch "classNotFoundException for Driver" New topic
Author

classNotFoundException for Driver

Branko Santo
Ranch Hand

Joined: Oct 15, 2005
Posts: 72
This is crazy! I have been through all the forums and a lot of books.
I use NetBeans 5.0 and I have an app accessing MySQL database through mysql-connector-java-3.1.12 and when I use it from NetBeans it works ok, but after I build it and attempt to use it than it shows a classNotFoundException and names the driver I instantiated with

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

So is there any help?
Please?
Jeremy Wilson
Ranch Hand

Joined: Feb 18, 2003
Posts: 166
what type of application is it. j2ee or se. Either way the jar that contains the driver must be on the classpath. How you accomplish this depends on the type of application.


Jeremy Wilson
jay vas
Ranch Hand

Joined: Aug 30, 2005
Posts: 407
Maybe youre IDE hasnt added the connector to the classpath yet. Youre IDE should allow you to add a jar to the classpath.
Sometimes, you can include a jar in a project but unless you actually
instruct the ide to add that jar to the build path, it wont include it.

In any case...this always works for me (in Eclipse) :

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


//blah is the database name
Connection con=
DriverManager.getConnection("jdbc:mysql://localhost/blah", "user", "1234");
Branko Santo
Ranch Hand

Joined: Oct 15, 2005
Posts: 72
It is an J2SE application, and I do attach the .jar with the connector to the project and when I just run it from IDE it runs ok, but if I build and than run it, it does not work.


Class.forName("com.mysql.jdbc.Driver");
//blah is the database name
Connection con=
DriverManager.getConnection("jdbc:mysql://localhost/blah", "user", "1234");


This is the same code I use.
David O'Meara
Rancher

Joined: Mar 06, 2001
Posts: 13459

Ah, you still need to specify the jar containing the driver class on the classpath when running the application.
(assuming windows)

java -classpath %CLASSPATH%;\path\to\driver.jar com.your.Class
Branko Santo
Ranch Hand

Joined: Oct 15, 2005
Posts: 72
Thanks I will try that out.
Is there a possibility to make a batch file so that it is always started up with that invocation? Since I cant have users typing that all the time
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18541
    
    8

Of course. Anything you type at the command line, you can type into a batch file.
Branko Santo
Ranch Hand

Joined: Oct 15, 2005
Posts: 72
How?

Thanks!
Branko Santo
Ranch Hand

Joined: Oct 15, 2005
Posts: 72
Damn I tried the code David but it does not work for me
I type


mysql.jar being the jar with the drivers and admin.jar the program
Geeze and I really want this to work, I been trying to fix it for more than a week.
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18541
    
    8

Originally posted by Branko Santo:
How?

Thanks!
Was that question meant to be "How do I type commands into a batch file?" Surely it wasn't, the answer to that is "With a keyboard and a text editor program". I must not be understanding your problem correctly.
David O'Meara
Rancher

Joined: Mar 06, 2001
Posts: 13459

Assuming that your program needs just the admin.jar and mysql.jar, put the two jar files in the same directory, open a command prompt in that directory and type:

java -classpath admin.jar;mysql.jar -jar admin.jar

Then assuming that works, create a new text file in the same directory, paste in the command above, then rename the file to admin.bat. Be careful that windows doesn't rename it to windows.bat.txt, it does that some times. Double clicking the batch file should run your program
Branko Santo
Ranch Hand

Joined: Oct 15, 2005
Posts: 72
Paul yes actually I did not know how to make a batch file

David thanks a million, batch file works, program runs but still gives the esception. But I hacked it just took the source code of the Driver class and copied the whole class in my program. It works that way.

Thanks everybody once more it has been a pleasure
Hopefully I will be able to help all you one day
David O'Meara
Rancher

Joined: Mar 06, 2001
Posts: 13459

Good job, I'm glad you got it working
 
 
subject: classNotFoundException for Driver
 
Similar Threads
Null pointer exception in con.createStatement(); (Struts 2 using netbeans6.1 and MYSQL)
No suitable driver using msql
Accessing MySQL from NetBeans
NetBeans and MySQL
Accessing MySQL from NetBeans