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

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

问题描述

Maven 编译器插件文档状态:

Maven Compiler Plugin documentation states:

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

The Compiler Plugin is used to compile the sources of your project. Since 3.0, the default compiler is javax.tools.JavaCompiler (if you are using java 1.6) and is used to compile Java sources. If you want to force the plugin using javac, you must configure the plugin option forceJavacCompilerUse

实际上,当我们的构建中未指定 forceJavacCompilerUse 时,会出现一些构建错误,例如,当代码引用 com.sun. 包时(遗留,我们知道这是个坏主意...)

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:Program Files*jdk*in下面.

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

这个编译器也是用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天全站免登陆