How to enable Intellij hot code swap
Asked Answered
D

8

110

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)
Daysidayspring answered 19/6, 2011 at 11:45 Comment(5)
I've never heard of hot swap for command line apps, only for web apps.Santamaria
Coming from eclipse, which does this I would expect intellij to be able to do this as well. Am I missing something?Daysidayspring
i was also facing lot of issues with this, and then i got to know that if you are using VM - then hot swap won't workDesolation
IDEA 2021.2 : Settings ->Advanced Settings -> Allow auto-make to start even if developed application is currently running. Adding dependency: spring-boot-devtoolsSidelight
In IntelliJ you must be in Debug mode for hot swap to work.Debutante
C
148

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.

Citronellal answered 19/6, 2011 at 12:21 Comment(7)
Great, thanks. Can I make intellij auto compile? (a much better feature would be to allow to: save without auto compile then to have compile on demand turned on by default).Daysidayspring
See jetbrains.com/idea/webhelp/reloading-classes.html and jetbrains.com/idea/webhelp/debugger-hotswap.htmlCitronellal
Thanks a lot, looked for this a lot of time!Hairtail
For Mac version only was necessary Build -> Recompile 'MainTest.java', a little different.Groh
What helped me really was Save Actions plugin plugins.jetbrains.com/plugin/7642-save-actions.Anaxagoras
You saved my days. I prefer the shortcut instead of restarts at every change, even if minor.Doss
For Mac. command + shift + (Fn) + F9Theriault
S
34

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.

Slab answered 29/12, 2016 at 10:21 Comment(2)
HotSwap works for me when I debug the application and then build the project or just compile a file. However, it does not work on save even after the registry setting above was done. Is it because I'm in Debug and not Run?Belay
@DKroot, yes - in IntelliJ you must be in Debug mode for hot swap to work.Inexpressive
M
10

Go to Setting --> Select Debug --> HotSwap

enter image description here

Mortarboard answered 19/8, 2016 at 5:10 Comment(1)
Even when using this I still have to manually instruct intelliJ to recompile. This does make it a bit easier because you don't have to answer the question whether you want to reload the classes.Standstill
B
9

There is a Debugger > HotSwap option in intellij 11.

Banket answered 23/8, 2012 at 22:55 Comment(3)
There is an "Eclipse Mode" plugin, which works exactly like eclipse's background incremental compilation.Banket
Good comment, sadly the plugin does not seem to be maintained any more plugins.intellij.net/plugin/?id=3822Daysidayspring
In intellij 13 use: File->Settings->(type HotSwap in search field)Foulness
K
8

I think you are asking about this,

  1. Right-click on your java class
  2. Select Compile And Reload File
Kiri answered 3/11, 2020 at 11:26 Comment(0)
C
7

I wanted hotswap to automatically run every time I saved a Java file. None of the other answers did this so I just:

  1. Created a macro that will run on save: https://mcmap.net/q/80704/-intellij-reformat-on-file-save
  2. Added Action: Hotswap at the end of the macro (Run -> Reload Changed Classes)
Carver answered 19/1, 2019 at 19:49 Comment(0)
S
3

For anyone experiencing the same problem as me, you might have incurred in the following recent bug affecting the 2020.2 update: https://youtrack.jetbrains.com/issue/IDEA-247530?_ga=2.116305920.729836936.1599654650-984156210.1599654650

TL;DR As a workaround, you need to re-activate the Groovy plugin.

Saied answered 10/9, 2020 at 9:56 Comment(0)
F
0

I encountered this problem on Ubuntu. I also had an apparently unrelated issue where IntelliJ gave an error about the file watch limit (my apologies that I no longer have the exact error message). To solve this later issue I did the following steps:

  1. Add the following line to either /etc/sysctl.conf file or a new *.conf file (e.g. idea.conf) under /etc/sysctl.d/ directory:

    fs.inotify.max_user_watches = 524288
    
  2. Then run this command to apply the change:

    sudo sysctl -p --system

Now my React components rebuild and hot swap just fine.

Source

Freeswimming answered 1/2, 2018 at 16:51 Comment(1)
I have similar issue. however this (increasing file watcher) didn't help me.Necaise

© 2022 - 2024 — McMap. All rights reserved.