使用javac和javax.tools.JavaCompiler有什么区别? [英] What is the difference between using javac and javax.tools.JavaCompiler?

查看:1395
本文介绍了使用javac和javax.tools.JavaCompiler有什么区别?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Maven编译器插件文档各州

Maven Compiler Plugin documentation states:


编译器插件用于编译项目的源代码。从3.0开始,默认编译器是javax.tools.JavaCompiler(如果您使用的是java 1.6)并且用于编译Java源代码。如果你想使用javac强制插件,你必须配置插件选项 forceJavacCompilerUse

实际上,当我们的构建中没有指定 forceJavacCompilerUse 时,会出现一些构建错误,例如当代码引用 com.sun时。 / code> packages(遗产,我们知道这是一个坏主意...)

And indeed when forceJavacCompilerUse is not specified in our build there are some build errors, for example when the code references the com.sun. packages (legacy, we know that its a bad idea...)

这两种编译模式之间的其他差异一般和maven有什么区别?是否应该知道任何输出差异?

What are other differences between these two compile modes in general and with maven? Are there any output differences that one should know?

推荐答案

javac (作为java编译器)是一个可执行文件,理论上甚至可以是依赖于平台的可执行文件或脚本。这被称为将 .java 编译为 .class

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.

在Windows上它的名字是 javac.exe ,它通常在 C以下的地方:\程序文件* \ jdk * \\ \\ _ \\ n

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

此编译器也是用java开发的。这意味着,如果我们启动此.exe,则需要启动一个新的Java虚拟机来运行它。这很慢。

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.

但是,因为它是用Java编写的,所以有一个更快的替代方案:从我们已经运行的jvm,我们只需 import 它的主类(fe javax.tools.JavaCompiler 等)并调用它。这不需要启动不需要的jvm。这就是maven的作用。仅仅10年就足以让他们正确地做到这一点。 : - )

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. :-)

当然它也有一些后退。最可能的原因是,在内部编译器的情况下,它需要从与maven核心相同的jvm和相同的命名空间中运行。同样指定备用jvm是不可能的,并且可能还存在一些由命名空间冲突导致的副作用。但它们非常不可能,因为它们都是精心设计的软件。

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.

这篇关于使用javac和javax.tools.JavaCompiler有什么区别?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆