Java IDEs and VMs support a feature called HotSwapping. It allows you to update the version of a class while the virtual machine is running, without needing to redeploy the webapp, restart, or otherwise interrupt your debugging session. It's a huge productivity boost if you're editing the body of a method.Limitations
You cannot change the "shape" of a class. This means you can't add or remove member variables, methods, change the superclass, etc. This restriction may be relaxed by newer VMs someday. The VM will tell you if it can't handle the request.
You cannot change a class that hasn't been loaded by the VM already. The VM will ignore the request.
The webapp will always start up with the version of the class that was produced by the Ant build, even if you HotSwapped during an earlier debug session.
Changes to your class will be reflected AFTER the current stack has exited your method.Workflow
These steps are the sequence in IntelliJ. Other IDEs should very similar.
- Do an Ant build.
- In IntelliJ, do Build->Make Project. This gets IntelliJ's build system primed.
- Start up Tomcat, and use the webapp so that the class you want to change is loaded (the line breakpoint icon will show a check in the left hand column once it's been loaded).
- Edit the class.
- In IntelliJ, do Build->Compile <MyClass>.java.
- If you get a dialog, tell the IDE to HotSwap and always do that in the future.
- Make your code run again. Marvel at how fast it was.
If you need to change the shape of the class, I suggest killing Tomcat, doing an Ant build, and restarting the server. This leaves you poised to HotSwap again because the class will be the right "shape" already.