tomcat classloader

P

Pif - 34

Hello, I encounter a ClassNotFoundException when loading Oracle driver
in a servlet (Tomcat 5.5 JDK1.4.2). But my JAR is well placed in the
WEBINF/lib folder.

When I move the Class.forname from my servlet to a JSP page (wich calls
the JSP this works perfectly.

So Tomcat seems to have class loader in servlet or JSP that is different.

Can somebody help me ?

Thanks a lot.
 
A

Arne Vajhøj

Hello, I encounter a ClassNotFoundException when loading Oracle driver
in a servlet (Tomcat 5.5 JDK1.4.2). But my JAR is well placed in the
WEBINF/lib folder.

When I move the Class.forname from my servlet to a JSP page (wich calls
the JSP this works perfectly.

So Tomcat seems to have class loader in servlet or JSP that is different.

Can somebody help me ?

It should work with servlet as well.

Did you by any chance copy the JDBC driver jar file to WEB-INF/lib
after starting the web app?

Arne
 
P

Pif

It should work with servlet as well.

Did you by any chance copy the JDBC driver jar file to WEB-INF/lib
after starting the web app?

Arne

I don't understand also. I've restarted tomcat several times after
copying the file.
 
A

Arne Vajhøj

I don't understand also. I've restarted tomcat several times after
copying the file.

Exact exception text?

Listing of content of webapps/yourwebapp?

Arne
 
P

Pif - 34

I've replaced " Class.forName("oracle.jdbc.driver.OracleDriver"); "
by oracle.jdbc.driver.OracleDriver oracleDriver = null;

and this works perfectly. So my Jar is well placed, this seems to be an
error with the ClassLoader of tomcat !?
 
M

Mike Schilling

Pif - 34 said:
I've replaced " Class.forName("oracle.jdbc.driver.OracleDriver"); "
by oracle.jdbc.driver.OracleDriver oracleDriver = null;

and this works perfectly. So my Jar is well placed, this seems to be an
error with the ClassLoader of tomcat !?

If that's a local variable, I'm not sure it causes the class to be loaded.
 
A

Alessio Stalla

If that's a local variable, I'm not sure it causes the class to be loaded..

If I'm not mistaken, the class should be loaded because it's
referenced in the constant pool, but it will be "initialized" only
when you access it in some way (create an instance, access a static
member). "Initialized" is not the correct term, by that I mean that
static code blocks are executed and static fields are initialized.
 
L

Lew

Alessio said:
If I'm not mistaken, the class should be loaded because it's
referenced in the constant pool, but it will be "initialized" only
when you access it in some way (create an instance, access a static
member). "Initialized" is not the correct term, by that I mean that
static code blocks are executed and static fields are initialized.

You're not mistaken and "initialized" is the correct term.
 
M

Mike Schilling

Alessio Stalla said:
If I'm not mistaken, the class should be loaded because it's
referenced in the constant pool, but it will be "initialized" only
when you access it in some way (create an instance, access a static
member). "Initialized" is not the correct term, by that I mean that
static code blocks are executed and static fields are initialized.

I just tried it.

A.java

class A
{
public static void main(String[] arrs)
{
B b = null;
System.out.println("Hello, world");
}
}

Runs fine after deleting B.class
 
L

Lew

That's "WEB-INF/lib".

Did you misspell it that way in your actual project?
When I move the Class.forname [sic] from my servlet to a JSP page (wich calls
the JSP this works perfectly.

So Tomcat seems to have class loader in servlet or JSP that is different.

JSPs *are* servlets.
Exact exception text?

Listing of content of webapps/yourwebapp?

You should answer Arne's questions. For one thing, if there was a
build problem and your JAR didn't copy properly to the "WEB-INF/lib/"
directory, you could have a problem.

Consider providing an SSCCE. Talking around your problem doesn't give
enough information to really draw conclusions or provide advice.
<http://sscce.org/>

Just out of curiosity, why are you stuck on such an obsolete version
of Java?
 
L

Lew

Pif said:
I encounter a ClassNotFoundException when loading Oracle driver
in a servlet (Tomcat 5.5 JDK1.4.2). But my JAR is well placed in the
WEBINF/lib [sic] folder.
Exact exception text?

Listing of content of webapps/yourwebapp?

Also, what is the exception's cause ('getCause()') and its (exact)
message?

From the Javadocs for 'ClassNotFoundException':
'The "optional exception that was raised while loading the class" that
may be provided at construction time ... may be accessed via the
Throwable.getCause() method ..."'
 
M

Mike Schilling

Alessio Stalla said:
OK, thanks. I wasn't 100% sure and, yes - I was lazy and I didn't look
it up :)


But, terminology aside, you are mistaken. See my previous post.
 
A

Alessio Stalla

But, terminology aside, you are mistaken.  See my previous post.

Right. I tried it myself, too. I suspect it's a compiler optimization
that removes the local variable altogether because it's never used.
Although the compiler seems pretty smart - even with

B b = (1 == (0 + 1)) ? null : new B();
if(b == null) { ... }

there's no trace of B in the bytecode. So, I stand corrected.

Cheers,
Alessio
 
A

Arne Vajhøj

> I've replaced " Class.forName("oracle.jdbc.driver.OracleDriver"); "
> by oracle.jdbc.driver.OracleDriver oracleDriver = null;
>
> and this works perfectly. So my Jar is well placed, this seems to be an
> error with the ClassLoader of tomcat !?

Not likely.

Both Tomcat and Oracle are very widely used products.

Arne
 
K

Kevin McMurtrie

Pif - 34 said:
I've replaced " Class.forName("oracle.jdbc.driver.OracleDriver"); "
by oracle.jdbc.driver.OracleDriver oracleDriver = null;

and this works perfectly. So my Jar is well placed, this seems to be an
error with the ClassLoader of tomcat !?

Are you compiling against one version of the driver but running with a
different? I could be wrong, but I think
'oracle.jdbc.driver.OracleDriver' is sometimes a private class. Try
'oracle.jdbc.OracleDriver'.
 
L

Lew

As pointed out upthread, this does not. Something else must "work", whatever
"work" means to you, Pif, but of course, you refuse to show us any code so we
really cannot tell.
So my Jar [sic] is well placed, [sic] this seems to be an
error with the ClassLoader of tomcat [sic] !? [sic]

Tchyaah, right!

Kevin said:
Are you compiling against one version of the driver but running with a
different? I could be wrong, but I think
'oracle.jdbc.driver.OracleDriver' is sometimes a private class. Try
'oracle.jdbc.OracleDriver'.

I believe that 'oracle.jdbc.driver.OracleDriver' is public in all the JARs
Oracle distributes, but that's moot anyway. Plus I could be wrong also.

Using the 'DriverManager' way to load the driver, you are supposed to use
'oracle.jdbc.OracleDriver' in preference to the other as you say. However,
that whole technique, according to Oracle, and to Sun even before Oracle
acquired it, is deprecated.

<http://download.oracle.com/docs/cd/E11882_01/appdev.112/e12137/getconn.htm#CACCCIIC>
"Note:
"The use of the DriverManager class to establish a connection to a database is
deprecated."

They tell you to use 'oracle.jdbc.pool.OracleDataSource' instead.

I go a tad further and suggest following Tomcat's instructions on how to set
up a data source.

However, "Pif - 34" has shown no interest in Arne's or my suggestions,
responding to our questions or providing an SSCCE, so I suppose I'm just
shouting in the wilderness.

Come to think of it, we haven't heard from "Pif" at all in over a week. I
hope they're all right!
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,995
Messages
2,570,225
Members
46,815
Latest member
treekmostly22

Latest Threads

Top