get tomcat hot swap work with intellij

It usually takes a lot of time whenever you’re recompiling a Java web project. The IDE would usually recompile the entire project, package them into a war and have it redeployed on your  application server (i.e., Tomcat) then letting the server reinitialize itself. It takes a lot of time, and it just gets worse as your project grows bigger.

Uh, Hot Swap?

Hot swapping is a more efficient process of doing this. Hot swapping works by replacing the classes that you’ve changed instead of having the entire project recompiled. The resulting classes are then replaced (or rather, hot swapped) from the application server, and everything else just runs normally, without the need for a restart since all the other components didn’t have to be reloaded.
Now, Hotswapping isn’t exactly new. Java actually supports it with its own hot swapping solution via HotSpot VM, but it wasn’t exactly usable enough. It’s dealbreaking flaws prevented you from adding new methods or adding new classes. For the most part, that just leaves you to modifying existing method bodies in existing classes. In most cases, that just isn’t enough.
DCEVM fixes that problem. It’s essentially a Java VM modification that allows the redefinition of loaded classes in runtime. This lets you do the points I mentioned above that couldn’t be achieved before.

Getting Hot Swap to Work

This guide demonstrates how to configure Hot Swapping for IntelliJ using DCEVM.

While in a fully-functional IntelliJ Java web project*

  1. Get the updated, forked version of DCEVM here:
    • 64-bit is supported!
    • Works in Linux with matching build numbers
    • While unsupported, it’s currently working on Oracle Java JDK versions as well.
  2. Run the package, and choose Install DCEVM as altjvm
  3. In IntelliJ, under your project’s build configurations (Run -> Edit Configurations), make sure that your project uses exploded war artifacts, instead of the normal war packages.
    • The use of exploded war packages allows IntelliJ to update both classes AND resources whenever they are updated. That means this change will also help you in swiftly reloading JSP pages that have been updated.
  4. In the Server tab, add -XXaltjvm=dcevm to the VM options (seems to me this is optional.)
  5. Change On ‘update’ action and On frame deactivation to Update classes and resources.
    • These changes will ensure that IntelliJ will update whenever changes are made and when you’ve shifted the focus away from IntelliJ. If you don’t want to do this automatically, then set Do nothing for these options.
    • If you have the Live Edit plugin, you can tick with Javascript debugger on and all of your changes on JSP files will also reflect as soon as you’ve finished typing. I wouldn’t recommend this since it’ll trigger a refresh every time, but it’s convenient if you want a WYSIWYG experience.
The end result should look like this.
Once you’re done, simply use Debug mode when executing your web application.
Depending on your settings:
  • IntelliJ will make and compile any classes you’ve changed once you’ve switched your focus away from the IDE. Your changes will reflect as soon as you refresh the page.
  • If you’ve used HotSpot VM before (the default Hot swap), you’ll be able to create new classes and methods in both new and existing classes. Give it a try.
  • Frameworks like Spring and Hibernate are likely not fully supported, due to the nature of how they work or how they are brought up (and how it might be improperly done by hot swapping). It’s workable, but you just have to be careful when to modify or add code that relies on these frameworks.
    • If you REALLY need to, then JRebel has support for it. Unfortunately, JRebel costs money and DCEVM being free is the exact reason why I wrote this post.




Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s