Working with Java SE projects (Should know)

Exclusive offer: get 50% off this eBook here
Instant JRebel [Instant]

Instant JRebel [Instant] — Save 50%

Accelerate your code development dramatically with this practical guide book and ebook.

$14.99    $7.50
by Jonathan Lermitage | November 2013 | Open Source

In this article by Jonathan Lermitage, the author of the book, Instant JRebel, we'll see how to work with JRebel on the Ant Java SE projects: a simple graphical application will be built and modified without any additional compilation, thanks to the JRebel live code injection. You will also get some words about Maven Java SE projects support. NetBeans will be used as the main IDE.

(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:


Instant JRebel [Instant] Accelerate your code development dramatically with this practical guide book and ebook.
Published: November 2013
eBook Price: $14.99
See more
Select your format and quantity:

About the Author :


Jonathan Lermitage

Jonathan Lermitage, is a 29-year old programmer and French national. Jonathan has worked for five years with the Java SOA and BPM ecosystem, and the NetBeans IDE for a medium-sized company. Now, he is working for one of the European e-business leaders helping them to modernize and optimize their products. Also, he is trying to return to school in order to become an Engineer in Scientific Computing and Mathematics, his first passion and wish.

He has worked as a Technical Reviewer on Instant NetBeans IDE How-to, Atul Palandurkar, Packt Publishing.

Books From Packt


Java EE 7 First Look
Java EE 7 First Look

 Java EE 7 Developer Handbook
Java EE 7 Developer Handbook

Java EE 6 Cookbook for Securing, Tuning, and Extending Enterprise Applications
Java EE 6 Cookbook for Securing, Tuning, and Extending Enterprise Applications

 Java EE 6 with GlassFish 3 Application Server
Java EE 6 with GlassFish 3 Application Server

 Java EE Development with Eclipse
Java EE Development with Eclipse

 Java EE 5 Development with NetBeans 6
Java EE 5 Development with NetBeans 6

Java EE 5 Development using GlassFish Application Server
Java EE 5 Development using GlassFish Application Server

Java EE 6 Development with NetBeans 7
Java EE 6 Development with NetBeans 7


No votes yet

Post new comment

CAPTCHA
This question is for testing whether you are a human visitor and to prevent automated spam submissions.
w
B
h
c
3
U
Enter the code without spaces and pay attention to upper/lower case.
Code Download and Errata
Packt Anytime, Anywhere
Register Books
Print Upgrades
eBook Downloads
Video Support
Contact Us
Awards Voting Nominations Previous Winners
Judges Open Source CMS Hall Of Fame CMS Most Promising Open Source Project Open Source E-Commerce Applications Open Source JavaScript Library Open Source Graphics Software
Resources
Open Source CMS Hall Of Fame CMS Most Promising Open Source Project Open Source E-Commerce Applications Open Source JavaScript Library Open Source Graphics Software