Intellij IDEA Java类在保存时不能自动编译

昨天我从Eclipse切换到IntelliJ IDEA。

我也在WebSphere Server 7中使用JRebel。

现在一切似乎都工作得很好,除了当我修改一个Java文件,和打救, IntelliJ 重新编译文件,以便JRebel拾取它。

Eclipse“建立自动”特性解决了这个问题。

在IntelliJ IDEA中,我必须按CTRL+转变+9来重新编译相关的类,以便JRebel接收它。如果改变是通过两个文件完成的,我有对每一个都做这个他们,因为IntelliJ使用保存所有机制,很难知道手动重新编译什么,我也不感兴趣。

有没有一种方法使IntelliJ 这是自己做的吗?

195909 次浏览

更新

对于IntelliJ IDEA 12+版本,如果我们使用外部编译器选项,我们可以自动构建编辑过的源代码。唯一需要做的就是检查位于“__abc1”下的选项“;自动生成项目"设置:

Compiler Settings .

此外,如果你想在应用程序运行时热部署,或者如果你使用spring boot devtools,你也应该从注册表中启用compiler.automake.allow.when.app.running。这将自动编译您的更改。

对于大于2021.2的版本,我们需要检查“即使开发应用程序正在运行,也允许自动启动”选项: enter image description here < / p >

对于2021.2之前的版本:

一旦注册表窗口打开,使用Ctrl+转变+一个(或Mac上的+转变+一个)键入Registry,找到并启用compiler.automake.allow.when.app.running,见这里:

enter image description here


对于12以上的版本,您可以使用*EclipseMode*插件使IDEA自动编译保存的文件。

有关更多技巧,请参阅“从Eclipse迁移到IntelliJ idea”;指南。

警告

Eclipse模式插件已经过时,与最近的IDEA 12+版本不兼容。如果你安装它,IDE将挂起每一个文件更改,并将响应极慢。


IntelliJ IDEA不使用自动构建,它在运行中检测错误,而不是通过编译器。类似于Eclipse模式将在想法12中可用:

自动创建项目

使用Build | Make,它调用增量make过程,只编译已更改的和依赖的文件(它非常快)。

还有一个FAQ条目可能会有所帮助。

< p > 自动制作功能的更新: 当运行/调试配置正在运行时,Make project automatically没有影响。磁盘上的类只会在Build | Make上改变。这是核心设计决策,因为在我们看来,磁盘上的类更改应该始终由用户控制。自动生成并不是Eclipse特性的模仿者,它的工作方式不同,它的主要目的是在真正需要时(在运行应用程序或测试之前)节省等待类准备就绪的时间。自动生成不会替换您仍然需要触发的显式编译,就像在这个问题中描述的情况一样。如果您正在寻找不同的行为,上面常见问题中的EclipseMode插件链接将是一个更好的选择

你可以通过键映射ctrl+s来保存和编译。转到键映射设置并搜索Compile

我最终记录了一个来保存并在一步中编译,并将Ctrl+s键映射到它。

我也有同样的问题。 我正在使用“省电模式”,这可以防止增量编译和显示编译错误

我设法用宏来解决这个问题。

我开始录制一个宏:

  • 点击编辑-宏-开始宏录制
  • 单击“文件-全部保存”
  • 单击“生成-制作项目”
  • 单击编辑-宏-停止宏记录

命名一些有用的东西,比如“SaveAndMake”。

现在只需删除保存所有键绑定,并添加相同的键绑定到您的宏!

所以现在,每次我保存,它保存并进行脏编译,jRebel现在正确地检测所有更改。

实际上有没有区别,因为两者都需要1次点击:

  • Eclipse:手动保存,自动编译。
  • IntelliJ:自动保存,手动编译。

简单的解决方案只是为了习惯它。因为当您在IDE中度过大部分时间时,最好在一个IDE中养成快习惯,而不是在几个IDE中养成慢习惯。

使用Reformat and Compile插件(灵感来自Alexandre DuBreuil的Save Actions插件):

https://plugins.jetbrains.com/plugin/8231?pr=idea_ce

目前我只提供了一个jar文件,但这是代码中最重要的部分:

private final static Set<Document> documentsToProcess = new HashSet<Document>();
private static VirtualFile[] fileToCompile = VirtualFile.EMPTY_ARRAY;


// The plugin extends FileDocumentManagerAdapter.
// beforeDocumentSaving calls reformatAndCompile
private static void reformatAndCompile(
@NotNull final Project project,
@NotNull final Document document,
@NotNull final PsiFile psiFile) {
documentsToProcess.add(document);
if (storage.isEnabled(Action.compileFile) && isDocumentActive(project, document)) {
fileToCompile = isFileCompilable(project, psiFile.getVirtualFile());
}
ApplicationManager.getApplication().invokeLater(new Runnable() {
@Override
public void run() {
if (documentsToProcess.contains(document)) {
documentsToProcess.remove(document);
if (storage.isEnabled(Action.optimizeImports)
|| storage.isEnabled(Action.reformatCode)) {
CommandProcessor.getInstance().runUndoTransparentAction(new Runnable() {
@Override
public void run() {
if (storage.isEnabled(Action.optimizeImports)) {
new OptimizeImportsProcessor(project, psiFile)
.run();
}
if (storage.isEnabled(Action.reformatCode)) {
new ReformatCodeProcessor(
project,
psiFile,
null,
ChangeListManager
.getInstance(project)
.getChange(psiFile.getVirtualFile()) != null)
.run();
}
ApplicationManager.getApplication().runWriteAction(new Runnable() {
@Override
public void run() {
CodeInsightUtilCore.forcePsiPostprocessAndRestoreElement(psiFile);
}
});
}
});
}
}


if (fileToCompile.length > 0) {
if (documentsToProcess.isEmpty()) {
compileFile(project, fileToCompile);
fileToCompile = VirtualFile.EMPTY_ARRAY;
}
} else if (storage.isEnabled(Action.makeProject)) {
if (documentsToProcess.isEmpty()) {
makeProject(project);
}
} else {
saveFile(project, document, psiFile.getVirtualFile());
}
}
}, project.getDisposed());
}


private static void makeProject(@NotNull final Project project) {
ApplicationManager.getApplication().invokeLater(new Runnable() {
@Override
public void run() {
CompilerManager.getInstance(project).make(null);
}
}, project.getDisposed());
}


private static void compileFile(
@NotNull final Project project,
@NotNull final VirtualFile[] files) {
ApplicationManager.getApplication().invokeLater(new Runnable() {
@Override
public void run() {
CompilerManager.getInstance(project).compile(files, null);
}
}, project.getDisposed());
}


private static void saveFile(
@NotNull final Project project,
@NotNull final Document document,
@NotNull final VirtualFile file) {
ApplicationManager.getApplication().invokeLater(new Runnable() {
@Override
public void run() {
final FileDocumentManager fileDocumentManager = FileDocumentManager.getInstance();
if (fileDocumentManager.isFileModified(file)) {
fileDocumentManager.saveDocument(document);
}
}
}, project.getDisposed());
}

请遵循这两个步骤:

1 -从编译器中启用Automake

  • 按:ctrl + 转变 + 一个(适用于Mac + 转变 + 一个)
  • 类型:make project automatically
  • 冲击:输入
  • 启用Make Project automatically特性

2 -在应用程序运行时启用Automake

  • 按:ctrl + 转变 + 一个(适用于Mac + 转变 + 一个)
  • 类型:Registry
  • 找到键compiler.automake.allow.when.app.running和旁边的启用它单击复选框

注意:现在重新启动您的应用程序:)

注意:这也应该允许使用spring boot devtools实时重载。

请仔细执行以下步骤来启用它。

1)使用SB V1.3创建Spring Boot项目,并将“Devtools”(1*)添加到依赖项中

2)调用Help->查找动作…输入“注册表”,在对话框中搜索“automake”并启用“compiler.automake.allow.when.app.running”条目,关闭对话框

3)在设置->构建,执行,部署->编译器“自动制作项目”中启用后台编译

4)打开Spring Boot运行配置,如果一切配置正确,你应该得到警告信息

5)运行应用程序,实时更改课程

请将您的经验和问题以评论的形式报告给我们。

点击这里查看更多信息

在我的maven项目中,唯一受此影响的是在单元测试的运行配置中添加“test-compile”目标。令人难以置信的笨拙解决方案,但它有效。

enter image description here

没有足够的分数来评论现有的答案,但类似于上面的一些人,我只是添加了一个宏&keymap到组织导入/格式化/保存/快速加载(F9) /同步

同步是添加的,因为它似乎是我还可以看到由外部构建工具/观察者(即webpack)修改的资源更新的唯一方式。

这个过程比eclipse慢——而且对于外部文件刷新经常需要多次运行该命令——但假设我可以接受它。

当Intellij在其他模块中遇到编译问题时,它会悄悄地失败,然后不执行自动编译。所以检查你的Problems窗口

我得到错误:一些罐子不在类路径。因此,我只是删除损坏的jar并执行以下步骤

1.Project >  Setting>Build,Execution,Deployment>Compiler>check build project automatically
2.CTRL+SHIFT+A find/search **registry** --Check for below param
compiler.automake.allow.when.app.running
compiler.automake.trigger.delay=500---According to ur requirement
3.Add devtool in pom.xml
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<optional>true</optional>
</dependency>
4.Build ,If found any probelm while building ,saying some jar in not in class path.Just delete the corrupted jar
and re-build the project angain after sync with maven lib

编辑运行/调试配置,以便在启动之前选择生成选项

enter image description here

选择“构建”选项后

enter image description here

上面的解决方案在我开发JBehave测试套件时非常适用

它不适合我,因为在我的项目结构中有一个不必要的模块。我想测试是用另一个模块执行的。

我不知道它是怎么变成这样的,但去掉它就解决了问题。

确保运行/调试设置使用的是使用自动保存构建的模块。

例:参见

enter image description here

您可以在“项目结构-模块”中更改模块

enter image description here

我也有同样的问题。我认为检查你的类是否可以编译是合适的。单击recompile(默认为Ctrl+Shift+F9)。如果它不工作,那么你必须调查为什么它不能编译。

在我的例子中,代码没有自动编译,因为编译过程中存在隐藏的错误(它们没有在任何地方的日志中显示,而且maven clean-install正在工作)。根本原因是错误的项目结构->模块配置,因此Intellij Idea无法根据此配置构建它。

我有同样的问题,也有一个问题文件图标在intellij,所以我删除了.idea文件夹和重新导入项目解决了我的问题。

对于使用新的Intellij版本并且找不到compiler.automake.allow.when.app.running的人

https://youtrack.jetbrains.com/issue/IDEA-274903

基本上该选项现在已经移动到Settings -> Advanced Settings -> Compiler(检查允许自动生成选项)

对于Intellij 2021或更高版本,您将找不到注册表项

compiler.automake.allow.when.app.running

它已经移动到高级设置如下面的截图所示

IntelliJ高级设置截图 .

来源:https://youtrack.jetbrains.com/issue/IDEA-274903