Time is money. Something that I still don’t get is that many Java developers spend a lot of it (time and money) while waiting to see their latest code changes in action.
JVM is known for its great peak performance when serving large numbers of users, but our tooling isn’t by default that well optimized to take new changes into use while developing. A full restart of your Java application can take from seconds to even minutes. Even if it takes just a couple of seconds, it’s enough for you to lose focus and switch to something else.
Thus, many professional Java developers utilize some extra tooling to improve their development turnaround time – the time from saving your code change to seeing the new result on the screen. Let’s have a look at the options.
Limited built-in hot-swap feature in JDK
By default, the JVM has limited support for hot-swapping new code into a running application. If you are running the application in debug mode, you can apply new method bodies to classes using basic IDE features.
JDK’s built-in code hot swap in debug mode is the easiest one to take into use, but is limited in its features.
This is handy and robust when trying to fix your algorithm in instance methods, but useless most of the time. Injecting any less-trivial change using the default JDK tooling will fail. Also, many developers tend to avoid debug mode for performance reasons.
JRebel is a Java agent that greatly improves the class reloading possibilities of JVM. Compared to the built-in features of JDK, it allows you to go from “it sometimes works” to “it almost always works”. You can add new methods, and new classes and lambdas, and do rather advanced refactorings and your new code still gets reloaded. Unlike the built-in class reloading, JRebel also works without the debug mode.
Because of the number of advanced libraries that are built, the improved class reloading is not enough. JRebel comes with a wide range of library and server support, so that it can, for example, reload the Hibernate context if you modify an entity class.
A less known feature of JRebel is that one can also hook into class reload events from the framework side. Vaadin developers using JRebel can see this in action when modifying their UI code. The development mode of Vaadin notices the reloaded class and automatically refreshes the browser. Developers with a dual-screen (or dual-window) setup don’t need to leave their IDE to see how the UI changes while they code.
The hardest part of using JRebel is to convince your boss of the need for the license. Otherwise, the IDE plugin contains simple buttons to launch your application/server with a JRebel Java Agent attached.
The only downside in JRebel is that it is a commercial extension to your setup, which may be fully OSS otherwise. But in my opinion, the well-optimized experience is well worth the price tag.
HotSwap Agent is an open source project providing similar features to JRebel. Feature-wise, HotSwap Agent comes surprisingly close to its better-known commercial competitor, providing quite a wide framework support and an IDE plugin for IntelliJ IDEA.
If you plan to go with HotSwap Agent instead of JRebel, prepare yourself for more hassle when setting up the environment with a special JDK and Java Agent. But once you have the setup in place, the experience is great.
As the open source project has more limited resources than JRebel, you might sometimes need to wait to get support for the latest Java versions.
Spring Boot Dev Tools
Both JRebel and HotSwap Agent work well with Spring Boot projects. The Spring team also has a similar tool, which is called Spring Loaded, but that isn’t getting too much attention currently. In a more prominent project called Spring Boot Dev Tools, there is a feature to speed up the development cycle in a slightly different way.
Spring Boot Dev Tools does its magic with two classloaders. It loads your project code with one, and more-static stuff (JAR files in your dependencies) with the other. When a new version of your application code is available, the application is restarted with a new application classloader, but the classes from dependencies don’t need to be reloaded. Even though the application is really restarted, it now happens much faster than with a cold start.
Even though you can’t get up to the level of performance that class reloading solutions provide, Spring Boot Dev Tools is a good enough solution for many Spring Boot applications. Getting going with it is just a matter of adding one dependency to your project. The downside is naturally that you can only use it with Spring Boot projects.
Note that Spring Boot Dev Tools also provides other features than faster restart. You might want to have it in your tooling even if you’re using JRebel for class reloading. For example, you can accomplish a similar automatic browser reload feature to that of Vaadin and JRebel for all web frameworks by installing the LiveReload plugin in your browser. The live reload feature in Vaadin development mode integrates automatically with Spring Dev Tools in the same way as with JRebel, so no extra plugins are needed for Vaadin developers.
Quarkus dev mode
RedHat uses a similar “hack” to that of Spring Boot Dev Tools in the development mode of their container-first Java stack, Quarkus. The solution in Quarkus comes with multiple levels of classloaders. Like with Spring Boot Dev Tools, faster development turnaround time isn’t the only feature, you can for example run tests without launching another instance of JVM.
In the Quarkus solution, the development mode doesn’t actively look for updated class files. Instead, on each request, the dev mode checks for changes in the source files and recompiles the class if needed. The upside of this solution is that it works even without an IDE, which typically compiles updated classes when using the other solutions.
On the flipside, automatic browser reload, like with the Vaadin development mode or the LiveReload browser plugin, needs a separate solution. Even though checking for new changes is postponed to an actual request, there most often isn’t any disturbing latency in the requests.
Hot code deployment tools are a great way to improve productivity. Some developers are accustomed to the delay between development and testing, but new developers starting on the Java platform often complain about the development cycles in Java development. Personally, I wouldn’t do a full development day without some of these tools configured. The irritating pause while redeploying the latest code changes is just so much that I lose focus and switch it to an extra coffee break or checking the latest news.
The only downside of the tools is that they need a bit of extra setup or IDE plugins to work. While I think it makes sense for Java developers to do the extra setup, I have often wondered why on earth IDEs or JDK don’t provide better support out of the box.
Let us know your favorite tool by commenting below or tagging us in your tweet on Twitter. Whilst you're here, be sure to join our growing Discord server. If you have any questions, our herd of nerds is happy to help you out on there.