在从eclipse转到IDEA的过程中,发现之前在eclipse中的能运行的项目,迁移到IDEA里就无法运行,只要执行maven compile,就会报各种错误,例如找不到符号、找不到程序包等等。
这里需要一提的是,我运行的maven项目有部分源码是错误的,一般是import一个不存在的包,调用一个不存在的方法等。
然而神奇的是居然可以编译通过并正常运行,于是上网搜索总算找到一个比较合理的解释。以下是原文:
If you use Intellij, you should consider using the Eclipse compiler instead of Javac.
The compiler used for your project can be changed in the IDE settings page.
Note that if you are using Java 8, then only Intellij 14 and higher have options to select the Eclipse compiler.
Unlike Javac, the Eclipse compiler is designed to be used first and foremost inside an IDE and thus offers some features that make it ideal for development usage:
It is entirely possible that some portion of your source code may contain some compile time errors, however you want to quickly try executing some small test to check if some other part of your code works properly or not.
With the Javac compiler, you won’t have a choice but to fix all those errors before you are allowed to execute anything. The Eclipse compiler however will allow you to execute code regardless. Its aim is to always produce a runnable binary. If your code does hit a line with compile time error in it, it will throw a runtime exception instead.
To enable this feature, you will need to:
Check ’Proceed on errors’ in the Compiler settings page.
Replace the ’Make’ build step of your Run Configuration with ’Make, no error check’.
2 . Incremental Compilation
The Eclipse compiler is designed to run in the background of an IDE and thus natively supports incremental compilation. What this means is that it can compile only the changed blocks of code since the last compile, resulting in very fast build times.
3 . Parallel compilation
Unlike Javac, the Eclipse compiler can use multiple cores to speed up compilation, finally putting all those extra cores in your PC to good use.
Conclusion
As can be seen, there is no good reason to continue using Javac inside an IDE. Hopefully Jetbrains will make it the default compiler in future versions of Intellij IDEA.
大概意思是说,如果是用IDEA,应该考虑使用eclipse编译器而不是javac。
如果使用的是jdk1.8,只有在IDEA 14版本和更高版本才可以选择eclipse编译器。
eclipse编译器提供了一些有用的特性:
1.继续出错
源代码有些部分有可能包含一些编译时错误,但是用户希望快速执行一些小测试检查代码其他部分是否正常工作
如果使用javac,那么你只能将所有错误修复才能编译通过,而eclipse允许你执行代码,生成class文件,如果遇到了编译器错误将会抛出一个运行时异常
throw new Error(“xxxxx”);
2.增量编译
eclipse编译器可以在ide的后台运行,所以支持本地增量编译。这意味它只能编译更改过的代码块,从而缩短构建时间。
3.并行编译
与javac不同,eclipse编译器可以使用多个内核来加速编译,更好的利用你的多核计算机。