Overloading CLASS files

Discussion in 'Java' started by gwoodhouse@gmail.com, Sep 27, 2010.

  1. Guest

    Hello again,

    In my application i'm using a .jar file to load classes of my webapp.
    I assumed that i could drop a class file in the WEB-INF/classes/
    directory using the same package structure and the webapp would
    instead use the class file over the older version present in the jar.

    It doesn't. Do any of you fine people know how to make it use the one
    in classes over the one in the Jar? (And would this practice be
    "overriding" the class in the Jar - is there a technical term for
    this?)

    Graeme
    , Sep 27, 2010
    #1
    1. Advertising

  2. Guest

    On Sep 27, 2:21 pm, "" <>
    wrote:
    > Hello again,
    >
    > In my application i'm using a .jar file to load classes of my webapp.
    > I assumed that i could drop a class file in the WEB-INF/classes/
    > directory using the same package structure and the webapp would
    > instead use the class file over the older version present in the jar.
    >
    > It doesn't. Do any of you fine people know how to make it use the one
    > in classes over the one in the Jar? (And would this practice be
    > "overriding" the class in the Jar - is there a technical term for
    > this?)
    >
    > Graeme


    As an addition to above -

    I was thinking this may be to do with the order in which classes are
    loaded? (Not sure on that point.)

    If I added the location of the class file to the begining of the
    CLASSPATH, would it mean it get's loaded first, stopping the one found
    in the JAR being loaded?

    Or would i add it to the end of the CLASSPATH, making sure that its
    the last class to be looked at - therefore overwriting the namespace
    of the class which is in the JAR file?

    I've read here: http://www.onkarjoshi.com/blog/133/...erride-existing-java-class-files-temporarily/
    you can place the class in the %JAVA_HOME%\lib\ext directory to make
    this work, (I'd rather not), but is this a "special case" of where the
    JVM looks for "higher priority" class files?

    Graeme
    , Sep 27, 2010
    #2
    1. Advertising

  3. Guest

    Sorry,

    This is turning into a running argument of solving my own problem it
    seems as i've found this:

    http://tomcat.apache.org/tomcat-4.1-doc/class-loader-howto.html
    "Therefore, from the perspective of a web application, class or
    resource loading looks in the following repositories, in this order:

    * /WEB-INF/classes of your web application
    * /WEB-INF/lib/*.jar of your web application
    * Bootstrap classes of your JVM
    * System class loader classses (described above)
    * $CATALINA_HOME/common/classes
    * $CATALINA_HOME/common/endorsed/*.jar
    * $CATALINA_HOME/common/lib/*.jar
    * $CATALINA_BASE/shared/classes
    * $CATALINA_BASE/shared/lib/*.jar"

    So, from my understanding, having a file here:
    WEB-INF/classes/uk/co/site/www/File.class

    should load before:
    WEB-INF/lib/jar.jar which contains uk/co/site/www/File.class

    .... Shouldn't it? ARG!
    , Sep 27, 2010
    #3
  4. markspace Guest

    On 9/27/2010 6:46 AM, wrote:

    > So, from my understanding, having a file here:
    > should load before:
    > .... Shouldn't it? ARG!



    I've seen some containers retain classes in memory between deployments.
    For example, when debugging I like to configure local loggers myself
    in code and add a ConsoleHandler so I can see output. Well after
    redeploy the app I noticed that the console debug lines doubled, then
    after the next deployment they tripled, etc.

    Turns out the Logger was being held in memory between deployments, and
    each time I ran I added a ConsoleHandler to a logger that already had
    one, so I was seeing additional lines of text from the extra
    ConsoleHandlers.

    Not sure if your problem is related. Just saying to consider all
    possibilities. Can you verify a new class (the other one) is loaded
    fresh each time? Are you sure you've spelled the name and path
    correctly in WEB-INF/classes/... correctly? Etc.

    Good luck, I haven't tried to do exactly what you're doing myself, but I
    do read the documentation the same as you.
    markspace, Sep 27, 2010
    #4
  5. Arne Vajhøj Guest

    On 27-09-2010 09:21, wrote:
    > In my application i'm using a .jar file to load classes of my webapp.
    > I assumed that i could drop a class file in the WEB-INF/classes/
    > directory using the same package structure and the webapp would
    > instead use the class file over the older version present in the jar.
    >
    > It doesn't. Do any of you fine people know how to make it use the one
    > in classes over the one in the Jar? (And would this practice be
    > "overriding" the class in the Jar - is there a technical term for
    > this?)


    You will need to restart the app anyway so why not just replace
    the jar file itself?

    Arne
    Arne Vajhøj, Sep 27, 2010
    #5
  6. Arne Vajhøj Guest

    On 27-09-2010 13:35, markspace wrote:
    > I've seen some containers retain classes in memory between deployments.
    > For example, when debugging I like to configure local loggers myself in
    > code and add a ConsoleHandler so I can see output. Well after redeploy
    > the app I noticed that the console debug lines doubled, then after the
    > next deployment they tripled, etc.
    >
    > Turns out the Logger was being held in memory between deployments, and
    > each time I ran I added a ConsoleHandler to a logger that already had
    > one, so I was seeing additional lines of text from the extra
    > ConsoleHandlers.


    Stuff like that does not happen by magic. There must be a reason.

    Most likely in this case that some of the involved classes were
    loaded by a server classloader instead of the app classloader, so
    static stuff were not unloaded with the app.

    Arne
    Arne Vajhøj, Sep 27, 2010
    #6
  7. Lew Guest

    On 09/27/2010 06:36 PM, Arne Vajhøj wrote:
    > On 27-09-2010 09:21, wrote:
    >> In my application i'm using a .jar file to load classes of my webapp.
    >> I assumed that i could drop a class file in the WEB-INF/classes/
    >> directory using the same package structure and the webapp would
    >> instead use the class file over the older version present in the jar.
    >>
    >> It doesn't. Do any of you fine people know how to make it use the one
    >> in classes over the one in the Jar? (And would this practice be
    >> "overriding" the class in the Jar - is there a technical term for
    >> this?)

    >
    > You will need to restart the app anyway so why not just replace
    > the jar file itself?


    Having more than one source of the same class in the same app is a recipe for
    disaster, especially for web and enterprise apps. Don't do it.

    I've been on several projects where multiple versions of the same class, or
    even the same JAR, or even the identical JAR more than once, got loaded. It
    causes interesting consequences, like class-cast exceptions when assigning an
    instance of a type to a variable of its supertype.

    Then you get into production and code that depends on the offending class or
    JAR starts fubaring because it can't get to the version it expects, or they're
    coming in via different classloaders.

    Just don't do it.

    --
    Lew
    Lew, Sep 28, 2010
    #7
    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. Iyer, Prasad C

    Overloading __init__ & Function overloading

    Iyer, Prasad C, Sep 30, 2005, in forum: Python
    Replies:
    3
    Views:
    6,406
    Fredrik Lundh
    Sep 30, 2005
  2. Fredrik Lundh
    Replies:
    0
    Views:
    449
    Fredrik Lundh
    Sep 30, 2005
  3. Steve Holden
    Replies:
    0
    Views:
    428
    Steve Holden
    Sep 30, 2005
  4. Iyer, Prasad C
    Replies:
    4
    Views:
    576
    John J. Lee
    Sep 30, 2005
  5. Fredrik Lundh
    Replies:
    0
    Views:
    398
    Fredrik Lundh
    Sep 30, 2005
Loading...

Share This Page