What is the difference between using javac and javax.tools.JavaCompiler?

余生颓废 提交于 2019-12-02 16:37:34

javac (as "java compiler") is an executable, which could be theoretically even a platform-dependent executable or a script. This is called to compile a .java to a .class.

On windows is its name javac.exe, and it is normally somewhere below C:\Program Files*\jdk*\bin.

This compiler was developed in java as well. That means, if we start this .exe, a new java virtual machine need to be started to run it. This is slow.

But, because it was written in Java, there is a much faster alternative to it: from our already running jvm, we simply import its main class (f.e. javax.tools.JavaCompiler or such) and call this. This doesn't need to start an unneeded jvm. That is what maven does. Simply 10 yrs was them enough to make this correctly. :-)

Of course it has some fallbacks as well. To most probable cause is that in the case of the internal compiler it needs to run from the same jvm and in the same namespace as the maven core. Also specifying an alternate jvm is impossible, and there could be some side effects as well resulting from the namespace collisions. But they are very improbable, because both of them is well-designed software.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!