Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to enable Intellij hot code swap

Intellij does not seem to be doing basic hot code swap on my installation.

For this code:

public class MainTest {     public void method1() {         System.out.println("Breakpoint here");     }      public void method2() {         System.out.println("Line that will get 24 modified");     }      public static void main(String[] args) {         System.out.println("First print here");         MainTest mainTest = new MainTest();         mainTest.method1();         mainTest.method2();         System.out.println("Line that I do not modify");     } } 

I put a breakpoint on mainTest.method1(); then modify the string at method2(), hit ctrl+s and continue to step by step. Unfortunately the runtime is not updated, old string is being printed. Full stop - compile - run does print the new string. So my conclusion is hot-swap not working.

Is there any settings I need to set to enable hot code swap?

  • Ubuntu, JDK 1.6
  • Intellij 10.5 Ultimate (evaluation)
like image 855
Maxim Veksler Avatar asked Jun 19 '11 11:06

Maxim Veksler


People also ask

How do I enable hot swap in IntelliJ?

Reload all files Recompilation happens automatically if the Build project before reloading classes option is enabled in Settings/Preferences | Build, Execution, Deployment | Debugger | HotSwap. If this option is disabled, you need to recompile the files before reloading (Build | Recompile Ctrl+Shift+F9 ).

How do I change the code while debugging in IntelliJ?

Yes, it's called 'hot swap'. You can compile your modified code in the middle of debugging and the class files will be replaced until you stop debug. Make sure to enable the HotSwap option in the debugger settings. Note: hot swap doesn't work when you change method signatures.

Why Debug is not working in IntelliJ?

To solve this, simply remove the jar of the debugged module from all modules' dependencies in the Project Structure. If you do not know which modules have the debugged module jar as dependencies, you can use some tools (Eg. Sublime Text, bash, ...) to search for the module name which is stored in Intellij *.

What is hot swapping code?

Hot code swapping is replacing or adding components without stopping or shutting down the system. It is frequently called as hot plugging. In software development, hot swapping is used to upgrade or update the system without interrupting the current running system.


2 Answers

After saving your class while waiting on your breakpoint just recompile your class with Build -> Compile 'MainTest.java' or press Ctrl+Shift+F9 with the standard key bindings.

IntelliJ IDEA will then show a small dialog in which it asks you if you want to reload that class.

like image 140
joschi Avatar answered Oct 02 '22 13:10

joschi


After below modifications and enabling hot swap, a change in a Java file took 1-2 seconds of restart time. (Initial start time is around 7 seconds for me).

I hope below method helps...


First, you need to check “Make project automatically” in preferences menu.

To open preferences menu;

you can go to top menu and click;

IntelliJ IDEA -> Preferences

or you can type below shortcut via keyboard;

cmd + option + s

Then, you can check Make project automatically as in below picture;

enter image description here

Secondly, you need to modify compiler.automake.allow.when.app.running registry setting as true.

To open registry, you need to click below keyboard shortcut:

cmd + shift + a

Type registry as in below picture, select Registry, and hit enter button of keyboard;

enter image description here

After Registry window opened, type compiler.automake to see compiler.automake.allow.when.app.running option, and check it as in below picture;

enter image description here

Then, you need to restart IntelliJ to make registry changes work.

like image 42
Berk Avatar answered Oct 02 '22 12:10

Berk