aspose file tools*
The moose likes Servlets and the fly likes Why can I not Class.forName in doPost Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Servlets
Bookmark "Why can I not Class.forName in doPost" Watch "Why can I not Class.forName in doPost" New topic
Author

Why can I not Class.forName in doPost

Roger Graham
Greenhorn

Joined: Feb 04, 2005
Posts: 10
Hi,

I have a Class.forName("com.mysql.jdbc.Driver") inside a doPost of a servlet deployed in JBoss (know its not the correct place to put it, but I was trying to do something quick and am just curious about servlet classloading).

I get a ClassNotFoundException. I presume, although against best practices, I am allowed legally to do a Class.forName(...) here - why do I get this error. It might be because the javax.sql are not loaded. If I place a j2ee.jar there, what are the implications? Would this work?

Thanks,
Roger

ps- I assume that the classloader of the thread which is servicing my request is the same classloader as that my webapp used - i.e. everything I put in lib I should be able to reference?
Roger Graham
Greenhorn

Joined: Feb 04, 2005
Posts: 10
...also I seem to be doing the right thing according to the folks at MySQL:

http://dev.mysql.com/doc/connector/j/en/cj-installing-for-servlet-use.html

I don;t have a JBoss-specific DD and my web.xml DD doesn't mention the library - do I have to declare it or something in there?

Thanks,
Rog
Paul Sturrock
Bartender

Joined: Apr 14, 2004
Posts: 10336

Class.forName() just gets you a Class object for whatever class you have named. If the ClassLoader can't find that class, it can't load it. So the jar containing com.mysql.jdbc.Driver needs to be in the classpath for your webapp (i.e. in its WEB-INF/lib directory).


JavaRanch FAQ HowToAskQuestionsOnJavaRanch
Ben Souther
Sheriff

Joined: Dec 11, 2004
Posts: 13410

You need to put the classes/libs where your app can see them.
For classes, put them in
YOUR_APP/WEB-INF/classes/YOUR_PACAGE/YOUR_CLASSES

for jar files:
YOUR_APP/WEB-INF/lib


Java API J2EE API Servlet Spec JSP Spec How to ask a question... Simple Servlet Examples jsonf
Roger Graham
Greenhorn

Joined: Feb 04, 2005
Posts: 10
Hi, sorry I forgot to mention the most important point! I *have* placed the jar in the WEB-INF/lib directory... that's what's confusing me.

Interestingly, however, I've been reading and MySQL recommends:

Class.forName(..).newInstance() as a 'workaround' for some implementations, it also states you shouldn't place a direct import statement importing the same class you're doing a classload on.

Still confused however! Any ideas?
Roger Graham
Greenhorn

Joined: Feb 04, 2005
Posts: 10
This works:

Class.forName("com.mysql.jdbc.Driver").newInstance();
Connection conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/question_db?user=root&password=mypass");

*Provided* you do not have directly import com.mysql.jdbc.Driver *and* you have the lib in the WEB-INF/lib directory as per the spec.

Cheers,
Roger
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Why can I not Class.forName in doPost