Hot Reload Java without Worry with DCEVM + HotSwap in IntelliJIDEA.
Read more about mobile, web, and backend in https://mozzlog.com/blog
How many time do you rebuild
your Java code? Uncountable. Because we don’t count it. We feel the pain.
And every time we rebuild
the code, we are losing momentum. Customer start to move to a new sophisticated platform, partner start to get impatient 😢, and the project start to its end. And sure we lost a lot of time to wait the build.
I know this is not new. You can read tutorial about how to do hot reload in a lot of article. So here, I just want to add another help so if you may have still fail to use DCEVM + HotSwap in IntelliJ, in the end of article, you can get your self to start use the tool with happy.
Too much preface, let’s do this.
What you need:
- DCEVM
- HotSwap plugin.
- Of cource, InteliJ IDEA.
Here I am using macOS but it must not so different in other platform. Thanks to JetBrains.
First, install HotSwap plugin the IDE.
Although there is DCEVM integration plugin, but I don’t install it. In my machine, it doesn’t work.
Reload the IDE if needed to load the plugin.
Then you must ensure to make HotSwap to enable reload classes in the background.
Next. We install DCEVM to our current JVM. We install it as alternate JVM.
-XXaltjvm=dcevm
Open terminal and run DCEVM archive using java -jar <archive>.jar
Then select your Java Home. Run this to find where does your Java Home is
Set DCEVM as altjvm
Then back to Intellij. Let’s create Java project using Intellij. You’ll want to use CommandLineTemplate.
You should select current project configuration to enable HotSwap.
Yay. Project has been setup!
Before we proceed, you may want to know that reloading main
method won’t work. So if you change main method body and expecting the code reload successfully, you’ll end up dissapointed.
To test our hot reload, we’ll use loop. An infinite loop!
public static void main() {
while(true) {
mainLoop()
}
}
And our reloadable method start from mainLoop()
private static void mainLoop() {
System.out.println("Hello World")
}
Run it in Debug mode.
Then change mainLoop()
body.
private static void mainLoop() {
System.out.println("Yuhuu")
}
Rebuild it. CTRL/CMD + SHIFT + F9
Conclusion
Using DCEVM + HotSwap we can make ourself more productive at no cost. If you want other tool, look at JRebel.
Currently DCEVM won’t installed if our JDK is higher than it. I’ve tested it using JDK 12. But let me know if you have the workaround! Stay sharp.
Take a look my Github! http://github.com/muizidn