Sign In Start Free Trial
Account

Add to playlist

Create a Playlist

Modal Close icon
You need to login to use this feature.
  • Book Overview & Buying Instant JRebel
  • Table Of Contents Toc
Instant JRebel

Instant JRebel

By : Jonathan Lermitage
1 (1)
close
close
Instant JRebel

Instant JRebel

1 (1)
By: Jonathan Lermitage

Overview of this book

JRebel is a JVM plugin that lets Java developers update code instantly and see their changes reflected in their application under development without restarting the application server. JRebel is a plugin for anything that is in Java and is also IDE neutral. If you want to update your code instantly without compiling and deploying the code repeatedly, this book is for you.This book will focus on the most important development steps and will also help you save time which is otherwise spent on compilation and deployment of code. You will learn how to use JRebel with the most popular IDEs and Java servers and how to accelerate the development of your current and future Java SE and Java EE projects.This book shows you how to install JRebel, configure your favourite IDE and Java server for JRebel, and start using it on your projects.With this book, you will learn how to get a free license for JRebel and how to integrate JRebel with most Java servers and development environments. To finish, you will see how to work with desktop and web projects and how to utilize JRebel to dramatically accelerate their development.
Table of Contents (7 chapters)
close
close

Working with Java SE projects (Should know)


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.

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
}

Tip

Downloading the example code

You can download the example code files for all Packt books you have purchased from your account at http://www.packtpub.com. If you purchased this book elsewhere, you can visit http://www.packtpub.com/support and register to have the files e-mailed directly to you

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

CONTINUE READING
83
Tech Concepts
36
Programming languages
73
Tech Tools
Icon Unlimited access to the largest independent learning library in tech of over 8,000 expert-authored tech books and videos.
Icon Innovative learning tools, including AI book assistants, code context explainers, and text-to-speech.
Icon 50+ new titles added per month and exclusive early access to books as they are being written.
Instant JRebel
notes
bookmark Notes and Bookmarks search Search in title playlist Add to playlist font-size Font size

Change the font size

margin-width Margin width

Change margin width

day-mode Day/Sepia/Night Modes

Change background colour

Close icon Search
Country selected

Close icon Your notes and bookmarks

Confirmation

Modal Close icon
claim successful

Buy this book with your credits?

Modal Close icon
Are you sure you want to buy this book with one of your credits?
Close
YES, BUY

Submit Your Feedback

Modal Close icon
Modal Close icon
Modal Close icon