• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Dynamic Class [Un]loading

 
Ranch Hand
Posts: 44
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Suppose I have a Circle class which has already been loaded via the default ClassLoader instance. Now suppose that my application needs are such that I have made a change to the Circle source file (e.g. added a new public method) and wish to recompile it and have the new Circle.class as part of my runtime environment.
A call to Class.forName("Circle"); will succeed, but the new version of the bytecode now present in Circle.class is not in my runtime environment, the previous version remains. This appears to be solely due to the fact that that ClassLoader recognized that Circle has already been loaded, but does not bother to check that its underlying .class file is different than what it has loaded.
It looks like I could simply subclass from java.lang.ClassLoader and implement exactly what I need (e.g. findLoadedClass() could return false), and my defineClass() implementation could be tailored to suite my needs, but how then do I know that the newly loaded class is part of my runtime environment and not simply some additional Class instance that has been created, but is NOT part of the runtime environment?
Anybody have any ideas on how to essentially get the previous version of the byte code unloaded/replaced with the new byte code?
 
Ranch Hand
Posts: 165
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
By default you can't replace or unload any class from RE.
There is definetely a security reasons for not doing that, I
don't remember exact description.
One more reason is static initializers. Imagine you have :
static int a = Class.method()
this method can do things that can't be undone or redone. Also,
one can change static fields inside any class and reloading of this class (even if we suppose there are no active instances of that class) will reset all changes and we would not know how to apply this changes to new static pool of newly loaded class.

------------------
With best of best regards, Pawel S. Veselov ( aka Black Angel )
 
Sheriff
Posts: 7001
6
Eclipse IDE Python C++ Debian Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Dynamic reloading of changed classes is certainly possible. I routinely use two Open Source programs which do this: Apache JServ ( http://java.apache.org ) and Kent Beck and Erich Gamma's JUnit ( http://www.xprogrammer.com )
I suggest that if you are interested in this, you download the source code for either or both of the above and have a look at how they do it. It's getting better with the more recent Java runtimes, but beware that class unloading in particular is often not implemented very well.
 
Doug Gschwind
Ranch Hand
Posts: 44
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Frank,
Thanks for that info, although I could use some more details. Do you know of the package/class in the Apache JServ source where I should be looking? Does it matter if it is JServ 1.0 or 1.1?
Also, the xprogrammer.com site simply looks like register.com, so I don't know how to find JUnit within it. Could it be that their URL has changed?
Thanks,
Doug
 
Doug Gschwind
Ranch Hand
Posts: 44
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I think I found it actually in the Apache JServ source code. The class is AdaptiveClassLoader.java in the org.apache.java.lang package in the sources/java (JServ 1.1) or src/java (JServ 1.0) directory correct?
 
Frank Carver
Sheriff
Posts: 7001
6
Eclipse IDE Python C++ Debian Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Sorry, that should have been www.xprogramming.com (you can see why I mistyped it!) Follow the "download" link on the left, then choose "Java ver 3.2" from the box in the right pane.
I don't have the JServ source to hand at the moment, but AdaptiveClassLoader sounds about right.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic