Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Update Java code during runtime

Tags:

java

runtime

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?

like image 775
Glader Avatar asked Jun 20 '10 12:06

Glader


People also ask

What is dynamic code in Java?

The Dynamic Code Evolution Virtual Machine (DCE VM) is a modification of the Java HotSpot™ VM that allows unlimited redefinition of loaded classes at runtime. The current hotswapping mechanism of the HotSpot™ VM allows only changing method bodies.


2 Answers

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.

like image 95
Jon Skeet Avatar answered Sep 23 '22 12:09

Jon Skeet


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:

  • Towards Flexible and Safe Technology for Runtime Evolution of Java Language Applications

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:

  • Dynamic Class Loading in the Java Virtual Machine

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!

like image 29
ewernli Avatar answered Sep 20 '22 12:09

ewernli