4 min read

(For more resources related to this topic, see here.)

How to do it…

The following steps will show you how to experiment with JRebel on a Java SE application:

  1. Create a simple Swing application and test it.
  2. Enable JRebel on your project.
  3. Try live code injection with JRebel.

How it works…

We will create a simple Swing application, a frame that contains a label and a button. The action associated with the button will update the label. We will use JRebel to change the button’s action without recompiling or restarting the application.

Start NetBeans and create a new Java application project. Create a package, delete the default main class, and use the NetBeans assistant to create a new JFrame object by navigating to File | New File | Swing GUI Forms | JFrame Form, and then choose a name and validate.

The Projects view

Use the Palette tab on your right to drag-and-drop a JLabel and a JButton to your JForm (these two components are located in the Swing Controls section, under the Label and Button names).

The Palette tab

Now, double-click on the button you have generated. You will be redirected to the jButton1ActionPerformed method of your JFrame object.

private void jButton1ActionPerformed (java.awt.event.ActionEvent evt) { // TODO add your handling code here: }

Insert a code to update JLabel of your form, as shown in the following code:

private void jButton1ActionPerformed (java.awt.event.ActionEvent evt) { // TODO add your handling code here: jLabel1.setText("Hello World"); }

The application is now ready for testing. You can test it by pressing F6 key. NetBeans will ask you for the name of the main class. Select the JFrame form and validate. Use the jButton1 button to update the label.

The first Hello World window

Do not close the application immediately, and return to the code editor to update the jButton1 button’s action in order to display a new message and save the new code.

private void jButton1ActionPerformed (java.awt.event.ActionEvent evt) { // TODO add your handling code here: // jLabel1.setText("Hello World"); // previous message jLabel1.setText("Hello Planet"); // new message }

Hit the jButton1 button in your application again; the change is not effective. Actually, your application hasn’t been updated and it can’t reflect code changes immediately. You will have to restart your application to see the new behavior, and this is normal. Now, let’s see how JRebel will accelerate the development.

You may have noticed the presence of a JRebel node in the Projects view. Right-click on it and choose Generate rebel.xml.

The JRebel XML

The rebel.xml file will contain the path of the compiled classes. JRebel will use these compiled classes to update your running application. Also, that means we have to ensure the Compile on Save feature is turned on for your project. Every time you apply changes to a Java class, NetBeans will recompile it and JRebel will update your running application with it. To proceed, go to the Projects Properties panel and enable the Compile on Save feature.

Enabling the Compile on Save feature

To finish, you have to activate JRebel (locate the JRebel button on the NetBeans toolbar).

Enabling the JRebel button

Now, restart your application and hit the jButton1 button a first time. Return to the code editor, modify the button’s action to display a new message, save, and carefully observe the NetBeans console; you will see two interesting messages:

  • Firstly, JRebel indicates if your license is active
  • Secondly, you can see a message that indicates a class which has been reloaded, as follows:

    2013-07-28 15:08:03 JRebel: Reloading class 'demo.ant.OurJFrame'. 2013-07-28 15:08:03 JRebel: Reloading class 'demo.ant. OurJFrame$2'. 2013-07-28 15:08:03 JRebel: Reloading class 'demo.ant. OurJFrame$1'.

Hit the jButton1 button again; the message changes without restarting the application. It works! You can now continue to update and save code in order to see the changes. Try to update the displayed message again, it works again; you don’t need to restart your application again (except for some changes that are not supported by JRebel).

There’s more…

Ant is not the only build system, so you may want to use JRebel with Maven-based projects.

Maven- and Ant-based projects are handled the same way: generate a rebel.xml file, activate the Compile on Save feature, enable JRebel, and simply update your code without restarting your application. You don’t have to deal with the Maven pom.xml file.

Also, you will still see the two same JRebel messages in the NetBeans console. They are useful to check the JRebel license and class reloading.

Last but not least, to activate the Compile On Save feature, the Compile On Save menu should be set to For application execution only (or better).

Maven’s Compile On Save feature

Summary

In this article, we learnt how to work with JRebel on the Ant Java SE projects. We also learnt the about Maven Java SE projects support.

Resources for Article:


Further resources on this subject:


LEAVE A REPLY

Please enter your comment!
Please enter your name here