openxava / documentation / Hot code reloading

×News: XavaPro 7.7.2 released - May 13 · Read more

Table of contents

Hot code reloading
Supported changes
IntelliJ
Without IDE
Other IDEs
OpenXava Studio and Java 11
Performance
Extra classpaths
XavaPro
Starting from OpenXava 7.5, you can enjoy hot code reloading while developing your application. This means you can modify any part of the code, save it, go to the browser and see the updated application. Everything happens instantly, without needing to build, run Maven install, restart the application, or even reload the page in the browser.

Supported changes

These are the changes you can make to your application without restarting it. Most of them take effect without even reloading the browser page:

It's not necessary to start in debug mode for changes to be recognized, but you do need to use a JetBrains JDK with the HotSwap agent configured and started with certain options, as explained below.

IntelliJ

IntelliJ is the recommended IDE for hot code reloading with OpenXava. To use it you need a JetBrains JDK: JBR 17, 21 or 25. Use the Project Structure... option in IntelliJ to set one of these JDKs for your project:

jbr-17-in-intellij.png

If you don't have a JetBrains JDK, you can download one directly from within IntelliJ in Project Structure..., in the SDKs section:

download-jbr-intellij.png

You also need to install a plugin called HotSwapHelper. Go to Settings > Plugins and search for it:

hotswaphelper-intellij-plugin.png

With this in place you can run your application, but not in the usual way. You must use the run with Hotswap debug option that is now available in the IntelliJ run menu:

run-with-hotswap-plugin.png

If everything goes well, at the start of the log output you should see a message like this:

Connected to the target VM, address: '127.0.0.1:54715', transport: 'socket'
HOTSWAP AGENT: 12:30:48.070 INFO (org.hotswap.agent.HotswapAgent) - Loading Hotswap agent {2.0.2} - unlimited runtime class redefinition.

If that's the case, you can now change the code and see the effect immediately. Note that IntelliJ does not compile the code automatically, even if you check the option to build the project automatically (because that option doesn't work when the application is running). What will happen is that when you change the code, a button will appear in the IntelliJ code editor saying 'Code changed', click it to compile and reload the code:

'Code changed' button inside IntelliJ code editor

Sometimes the button does not appear, for example if you change an XML file or the Java code is generated by an AI assistant. In that case, press Ctrl + F9. This will compile and reload the changed code.

With Java 25 the application stops from time to time, with Java 21 it fails much less and with Java 17 it does not fail. Even so, it is much more practical than restarting the application for every change. As new maintenance versions of Java 25 are released, it is expected to become increasingly robust.

Without IDE

Hot class reloading can also be used outside IntelliJ and without any IDE, from the terminal.

First, download a JetBrains JDK from here: https://github.com/JetBrains/JetBrainsRuntime/releases

After installing the JetBrains JDK, inside the lib folder create a hotswap folder and copy a file named hotswap-agent.jar (without version number) into it. This jar can be downloaded from here: https://github.com/HotswapProjects/HotswapAgent/releases
Important: when copying it into lib/hotswap, rename it so it is called exactly hotswap-agent.jar.

Now just start the application with this JDK, passing the startup options to activate HotSwap.

For example, on Linux:

export MAVEN_OPTS="-XX:+AllowEnhancedClassRedefinition -XX:HotswapAgent=fatjar"
export JAVA_HOME=/home/javi/.jdks/jbr-25.0.2
export PATH=$JAVA_HOME/bin:$PATH
mvn exec:java

On Windows:

set MAVEN_OPTS=-XX:+AllowEnhancedClassRedefinition -XX:HotswapAgent=fatjar
set JAVA_HOME=C:\Users\youruser\.jdks\jbr-25.0.2
set PATH=%JAVA_HOME%\bin;%PATH%
mvn exec:java

If done correctly, when you run the application you should see:

Connected to the target VM, address: '127.0.0.1:54715', transport: 'socket'
HOTSWAP AGENT: 12:30:48.070 INFO (org.hotswap.agent.HotswapAgent) - Loading Hotswap agent {2.0.3} - unlimited runtime class redefinition.

For more information see: https://github.com/HotswapProjects/HotswapAgent

Other IDEs

Hot class reloading can work from any IDE, such as Eclipse, NetBeans or Visual Studio Code. You just need to use a JetBrains JDK and copy the hotswap-agent.jar into it as described in the Without IDE section above. In addition, you need to configure the application startup in your IDE to use the options -XX:+AllowEnhancedClassRedefinition -XX:HotswapAgent=fatjar.

OpenXava Studio and Java 11

If you want to use OpenXava Studio or Java 11, follow this documentation: Hot code reloading with OpenXava Studio / Java 11.

Performance

The hot reloading mechanism is designed for optimal performance, loading just the necessary metadata only when needed or restarting the Hibernate session only when the persistent part of an entity is modified. Additionally, in production where you use a different JDK than in development, the reloading mechanism is disabled.

Extra classpaths

Sometimes you are developing a library or a project that is a dependency of the current project, and you would like to see the results instantly when you change the library code too. For this case, you can start your application indicating a list of extra classpaths. Edit your launcher class and write something like this:

public static void main(String[] args) throws Exception {
    AppServer.run("yourapp", "../yourlib/target/classes"); 
}

Now when something changes in ../yourlib/target/classes it will also be reloaded automatically. This allows you to develop your library testing it in a final application, with the same agility as with any other application.

Indicating extra classpaths when starting the application is available since version 7.5.

XavaPro

In XavaPro 7.5, there are significant improvements regarding module availability during development. When using Hot code reloading:

This streamlines the development process, as you no longer need to manually add new modules to the "user" role to make them available. However, in production environments (where Hotswap Agent is not used), the behavior remains unchanged for security reasons: new modules must be explicitly assigned to a role to be available to users.