必须开启build project automatically、启用compiler.automake.allow.when.app.running、取消skip compilation选项,并配置on update action为update classes and resources,才能实现保存即编译与热更新。

你需要让 IDEA 2026 在保存 Java 文件后自动编译并热更新类,避免每次手动点击 Build → Build Project 或反复重启应用浪费时间。这在 Spring Boot 开发、单元测试快速验证、模板引擎调试等场景中尤为关键。
启用自动编译(Build project automatically)
第一步:打开 Settings → Build, Execution, Deployment → Compiler
勾选 【Build project automatically】 —— 这是自动编译的总开关,不勾选则后续所有设置均无效。
第二步:按 Ctrl+Shift+Alt+/(Windows/Linux)或 Cmd+Shift+Option+/(macOS),调出 Maintenance 工具箱 → Registry
找到并勾选 【compiler.automake.allow.when.app.running】 —— 缺少这一步,IDEA 会在应用运行时禁用自动编译,导致你改完代码却没生效。
第三步:关闭 Registry 窗口,回到 Settings → Advanced Settings
取消勾选 【Skip compilation when launch configuration is running】 —— 若保留勾选,IDEA 会跳过正在运行的配置关联模块的编译,造成“改了但没编译”的假象。
配置运行时热更新(HotSwap 与 Runtime Reloading)
方法一:Spring Boot 项目专用热重载(无需插件)
确保项目已引入 spring-boot-devtools 依赖 → 打开 Run → Edit Configurations → 选择你的 Spring Boot 启动项 → 在 Configuration 标签页下勾选 【Enable hot swap agent (requires JVM restart)】 → 同时将 On ‘Update’ action 设为 Update classes and resources。
方法二:通用 JVM 热替换(HotSwap)
Run → Edit Configurations → 修改目标配置 → Debugger 标签页 → 勾选 【Enable hot swap agent】 → 注意:该选项仅对 JDK 17+ 有效,JDK 8/11 下不可用,强行启用会导致 JVM 启动失败。
方法三:自定义 ClassLoader 热重载(适用于非 Spring 项目)
添加 JVM 参数:-Djdk.attach.allowAttachSelf=true -agentlib:jdwp=transport=dt_socket,server=y,suspend=n,address=*:5005 → 配合 JRebel 插件或自研 ClassLoader 实现类级热替换;此方式绕过 JVM HotSwap 限制,但需额外编码支持。
保存即编译:绑定 Save Action
第一步:Settings → Appearance & Behavior → System Settings → Synchronization
勾选 【Synchronize files on frame activation】 —— 防止切换窗口后文件状态不同步导致编译遗漏。
第二步:Settings → Editor → General → Saving
勾选 【Save files on focus loss】 和 【Save files automatically if application is idle for N sec】(建议设为 1.5 秒)—— 这两项共同构成“保存即触发编译”的底层前提。
第三步:Settings → Tools → Actions on Save
勾选 【Compile modified Java files】 —— 此选项优先级高于 Build project automatically,即使自动编译被临时关闭,只要保存就会强制编译当前文件。
排除干扰:禁用冲突构建行为
第一步:Settings → Build, Execution, Deployment → Compiler → Excludes
点击 + 添加路径,填入 target/、build/、.gradle/ —— 避免 IDEA 尝试编译构建工具生成的中间产物,引发重复类加载错误。
第二步:Settings → Build, Execution, Deployment → Build Tools → Maven → Importing
取消勾选 【Import Maven projects automatically】 —— Maven 自动导入会触发全量 reimport,打断增量编译节奏;改为手动触发 Import 或使用 Reload project 按钮。
第三步:Settings → Build, Execution, Deployment → Compiler → Java Compiler
确认 Use compiler 设为 【Javac】,而非 Eclipse Compiler —— Eclipse 编译器在 IDEA 2026 中已标记为 deprecated,启用后可能导致泛型擦除异常或 Lambda 表达式解析失败。











