File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
Win a copy of Clojure in Action this week in the Clojure forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

classNotFoundException for Driver

 
Branko Santo
Ranch Hand
Posts: 72
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 166
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
jay vas
Ranch Hand
Posts: 407
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 72
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 13459
Android Eclipse IDE Ubuntu
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 72
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Sheriff
Pie
Posts: 20177
25
MySQL Database
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Of course. Anything you type at the command line, you can type into a batch file.
 
Branko Santo
Ranch Hand
Posts: 72
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
How?

Thanks!
 
Branko Santo
Ranch Hand
Posts: 72
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Sheriff
Pie
Posts: 20177
25
MySQL Database
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 13459
Android Eclipse IDE Ubuntu
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 72
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 13459
Android Eclipse IDE Ubuntu
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Good job, I'm glad you got it working
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic