Over a million developers have joined DZone.

Who Said Runtime Class Reloading Is Hard in Java?

DZone's Guide to

Who Said Runtime Class Reloading Is Hard in Java?

· Java Zone ·
Free Resource

Download Microservices for Java Developers: A hands-on introduction to frameworks and containers. Brought to you in partnership with Red Hat.

Are you sometimes frustrated spending precious development time restarting your Java application all the time? If so, read on!

If you haven’t already stumbled across Javeleon, I would like to introduce it to you and give you a sneak peak of some of the big changes now supported in the new version 1.5. You can hear more about Javeleon at this year’s JavaOne, in a session entitled “Who Said Runtime Class Reloading Is Hard in Java?”.

Javeleon is a small tool that boosts productivity by allowing you to see code changes in a running application while preserving all of the application's state. So, basically, developers can use Javeleon to speed up development by skipping the restart phase completely. Not only will this save you time immediately, it will also help you to keep focus on development task at all times.

Changing the Java Component Inheritance Hierarchy

While there are tools out there already that do most of what Javeleon does, especially JRebel, Javeleon has some nice additional features that may be of interest to you. Most importantly, Javeleon supports changes to the inheritance hierarchy; that is you are free to change the set of implemented interfaces as well as the super class as defined by the "extends" clause. While not all changes require us to change inheritance, this feature really comes in handy if you, e.g., want to abstract reusable stuff away to a new super class. Also, changing the set of implemented interfaces is in fact a quite common change as well. Just look at the example below taken from a NetBeans Platform application below.

public final class EditorTopcomponent extends TopComponent 
/* Add this by a dynamic update:*/
implements LookupListener */ { public EditorTopcomponent () { name = "something else"; initComponents(); setName(NbBundle.getMessage(testTopComponent.class, "CTL_testTopComponent")); setToolTipText(NbBundle.getMessage(testTopComponent.class, "HINT_testTopComponent")) } }

Typically, when we want different components to interact with each other in a loosely-coupled way we use the Lookup pattern in the NetBeans Platform. We simply need to implement the LookupListener interface, right? Indeed, this change is possible with Javeleon at runtime. Indeed, Javeleon can change the inheritance hierarchy at runtime, but there is more to it than that. Javeleon actually turns all of the objects that were already created before the update into correctly behaving objects reflecting the new type of the declaring class, even when used in reflective invocations.

Support for Standard Java SE Applications

Previous releases of Javeleon came with out-of-the-box support for NetBeans Platform applications only. But, in the new 1.5 release, we’ve added beta support for standalone Java applications as well.

And best of all, it is almost too simple to setup. Just add a few arguments to your JVM configuration and you are ready to apply dynamic code updates.

While the Javeleon NetBeans plug-in has built-in support for adding these arguments by a mouse click in your project, it takes only seconds to setup in, e.g., Eclipse as shown in the screenshot (click to enlarge it).

In your lanch configuration for your project add the Javeleon VM argument.

Then save the configuration and enjoy automatic class redefinitions when you modify your Java files. Remember to have ’Compile on save’ switched on.

Download Building Reactive Microservices in Java: Asynchronous and Event-Based Application Design. Brought to you in partnership with Red Hat


Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}