views:

149

answers:

3

Hi, about a year ago I stumbled across a nice feature in Java that I cannot for the life of me find again.

Through some magic interface it was apparently possible to declare some classes or functions replaceable during runtime.
I found a nice example guide of someone who ran a simple little program that printed a certain message, he then updated the program using a method I cannot remember anymore and all of a sudden the program had replaced that old print function with a new one.

I've tried looking through the Java API to spark my memory as well as googling but without success. Can anyone here help?

+5  A: 

Various app containers can do this.

Basically you'd need to reload the class in a new ClassLoader (unless you're talking about doing this under the debugger, in which case there are completely different APIs available).

In my opinion, this kind of thing is rarely worth the hassle: designing everything so that it can be reloaded is considerably harder than designing it so it can be completely restarted in a new process. It's also easier to be sure exactly what code is running if there's only ever one version loaded in the process.

It's a neat thing to be able to demo, but for most applications it's not worth it. All in my opinion, of course :)

Note that one notable exception is the ability to reload web UI layers without restarting the container: that can make life much easier.

Jon Skeet
Careful, don't say that too loudly near a Lisp or Erlang programmer.
John D
Thanks for the help Jon. I'm interrested in the subject because of it's perverted nature and because I feel like it's something new worthy of understanding. After all, I'm not very likely do get to fiddle with it during any of my academic courses so when else am I going to learn about it?I'm not entirely sure I found the original awesome guide I found last year but it's atleast pretty decent:http://java.sun.com/developer/technicalArticles/Networking/classloaders/ So if anyone else was curious this is a pretty decent place to start.
Glader
+1  A: 

This is typically the kind of functionality I gladly leave to infrastructure as it is difficult to get right and easy to get wrong. As Jon mentioned above, most applications do not need it and for those that need it infrastructure is available.

Most application servers allow hot deployment nowadays, and equally most application servers are embeddable and allow them to be stripped down to remove features you do not need.

If it mainly for development, you should look a JRebel which provides this functionality transparently. I've heard they are working on a runtime solution, but I do not know if it is ready for primetime yet.

If you are really motivated to get this to work, then consider using OSGi. It has a steep learning curve, but once you grok it, it does most things right and works very well. I found the pax tools a good starting point but the eclipse toolchain also has good support for it.

Peter Tillemans
Thanks for the tips Peter but right now I'm not looking for already existing servers that implement the feature, but rather some insight into how it works and how to implement the basics myself. I thank you for the tip about OSGi, might be worth some reading.
Glader
A: 

The HotSwap technology was added to Java 1.4 and enable class file replacement at run-time. The feature is provide through the redefineClasses method of the instrumentation package. I think you can also do that through the JPDA interface.

Here is also a reference to what I believe is the research paper that describe the HotSwap mechanism first:

Otherwise you can use Classloader, as the other mentionned, but it only provides dynamic class loading, not replacement. The same class loaded twice will be considered as two different types. Combined with interface and/or a bit of reflection, it can however provide ways to update the application at run-time.

Here is a reference to an awesome paper about class loader, and there usage:

I won't expand on whether this is good or bad, because it was not your question, but I think it's great to have support for run-time software evolution -- too bad that JSR-117 never made it!

ewernli