tomcat classloader

Discussion in 'Java' started by Pif - 34, Jun 3, 2010.

  1. Pif - 34

    Pif - 34 Guest

    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.
     
    Pif - 34, Jun 3, 2010
    #1
    1. Advertising

  2. Pif - 34

    Arne Vajhøj Guest

    On 03-06-2010 17:29, Pif - 34 wrote:
    > 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
     
    Arne Vajhøj, Jun 3, 2010
    #2
    1. Advertising

  3. Pif - 34

    Pif Guest

    On 4 juin, 00:22, Arne Vajhøj <> wrote:
    > On 03-06-2010 17:29, Pif - 34 wrote:
    >
    > > 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


    I don't understand also. I've restarted tomcat several times after
    copying the file.
     
    Pif, Jun 4, 2010
    #3
  4. Pif - 34

    Arne Vajhøj Guest

    On 04-06-2010 06:07, Pif wrote:
    > On 4 juin, 00:22, Arne Vajhøj<> wrote:
    >> On 03-06-2010 17:29, Pif - 34 wrote:
    >>> 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?

    >
    > 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
     
    Arne Vajhøj, Jun 5, 2010
    #4
  5. Pif - 34

    Pif - 34 Guest

    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 !?

    Arne Vajhøj wrote:

    > Exact exception text?
    >
    > Listing of content of webapps/yourwebapp?
    >
    > Arne
     
    Pif - 34, Jun 5, 2010
    #5
  6. "Pif - 34" <> wrote in message
    news:4c0ab15e$0$32742$...
    > 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.
     
    Mike Schilling, Jun 9, 2010
    #6
  7. On Jun 9, 5:05 am, "Mike Schilling" <>
    wrote:
    > "Pif - 34" <> wrote in messagenews:4c0ab15e$0$32742$...
    >
    > > 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..


    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.
     
    Alessio Stalla, Jun 9, 2010
    #7
  8. Pif - 34

    Lew Guest

    Alessio Stalla wrote:
    > 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.

    --
    Lew
     
    Lew, Jun 9, 2010
    #8
  9. "Alessio Stalla" <> wrote in message
    news:...
    > On Jun 9, 5:05 am, "Mike Schilling" <>
    > wrote:
    >> "Pif - 34" <> wrote in
    >> messagenews:4c0ab15e$0$32742$...
    >>
    >> > 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.

    >
    > 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
     
    Mike Schilling, Jun 9, 2010
    #9
  10. Pif - 34

    Lew Guest

    "Pif - 34" wrote:
    >>>> 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 !?

    >


    Whatever the problem is, it's not an error with Tomcat's classloader.

    --
    Lew
     
    Lew, Jun 9, 2010
    #10
  11. Pif - 34

    Lew Guest

    Pif - 34 wrote:
    >>>> 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.

    >


    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.

    Arne Vajhøj wrote:
    > 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?

    --
    Lew
     
    Lew, Jun 9, 2010
    #11
  12. Pif - 34

    Lew Guest

    Pif wrote:
    >>>> 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.

    >


    Arne Vajhøj wrote:
    > 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 ..."'

    --
    Lew
    Note the two different meanings of the word "may" in that quote from
    the Javadocs.
     
    Lew, Jun 9, 2010
    #12
  13. On 9 Giu, 14:27, Lew <> wrote:
    > Alessio Stalla wrote:
    > > 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.


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

    Alessio
     
    Alessio Stalla, Jun 9, 2010
    #13
  14. "Alessio Stalla" <> wrote in message
    news:...
    > On 9 Giu, 14:27, Lew <> wrote:
    >> Alessio Stalla wrote:
    >> > 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.

    >
    > 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.
     
    Mike Schilling, Jun 10, 2010
    #14
  15. On Jun 10, 4:23 am, "Mike Schilling" <>
    wrote:
    > "Alessio Stalla" <> wrote in message
    >
    > news:...
    >
    > > On 9 Giu, 14:27, Lew <> wrote:
    > >> Alessio Stalla wrote:
    > >> > 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.

    >
    > > 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.


    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
     
    Alessio Stalla, Jun 10, 2010
    #15
  16. Pif - 34

    Arne Vajhøj Guest

    On 05-06-2010 16:19, Pif - 34 wrote:
    > Arne Vajhøj wrote:
    >> Exact exception text?
    >>
    >> Listing of content of webapps/yourwebapp?

    > 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
     
    Arne Vajhøj, Jun 13, 2010
    #16
  17. In article <4c0ab15e$0$32742$>,
    Pif - 34 <> wrote:

    > 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 !?
    >
    > Arne Vajhøj wrote:
    >
    > > Exact exception text?
    > >
    > > Listing of content of webapps/yourwebapp?
    > >
    > > Arne


    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 won't see Google Groups replies because I must filter them as spam
     
    Kevin McMurtrie, Jun 13, 2010
    #17
  18. Pif - 34

    Lew Guest

    Pif - 34 wrote:
    >> I've replaced " Class.forName("oracle.jdbc.driver.OracleDriver"); "
    >> by oracle.jdbc.driver.OracleDriver oracleDriver = null;
    >> and this works perfectly.


    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 McMurtrie wrote:
    > 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!

    --
    Lew
     
    Lew, Jun 14, 2010
    #18
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Mr. Miguel

    classloader in tomcat

    Mr. Miguel, Jul 11, 2003, in forum: Java
    Replies:
    2
    Views:
    601
    Miguel De Anda
    Jul 14, 2003
  2. Replies:
    5
    Views:
    694
    Nigel Wade
    Jul 15, 2003
  3. Mikito Harakiri

    Tomcat ClassLoader/Compile nightmare

    Mikito Harakiri, Nov 6, 2003, in forum: Java
    Replies:
    0
    Views:
    333
    Mikito Harakiri
    Nov 6, 2003
  4. Mark F

    Tomcat classloader problem

    Mark F, Mar 5, 2004, in forum: Java
    Replies:
    0
    Views:
    483
    Mark F
    Mar 5, 2004
  5. Moiristo

    Tomcat classloader

    Moiristo, Jun 21, 2006, in forum: Java
    Replies:
    0
    Views:
    329
    Moiristo
    Jun 21, 2006
Loading...

Share This Page