如何启用 Intellij 热代码交换

Intellij 似乎没有在我的安装上进行基本的热代码交换。

对于这个代码:

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");
}
}

我在 mainTest.method1();上放置一个断点,然后在 method2()处修改字符串,点击 ctrl + s 并继续逐步执行。遗憾的是,运行库没有更新,正在打印旧字符串。完全停止-编译-运行确实打印新字符串。所以我的结论是热交换不起作用。

是否需要设置任何设置来启用热代码交换?

  • Ubuntu,JDK 1.6
  • Intellij 10.5极限(评估)
126897 次浏览

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.

There is a Debugger > HotSwap option in intellij 11.

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

enter image description here

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.

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

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://stackoverflow.com/a/5581992/14731
  2. Added Action: Hotswap at the end of the macro (Run -> Reload Changed Classes)

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.

I think you are asking about this,

  1. Right-click on your java class
  2. Select Compile And Reload File