39

Maven Compiler Plugin documentation states:

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

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

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

peterh
  • 11,875
  • 18
  • 85
  • 108
Vic
  • 21,473
  • 11
  • 76
  • 97
  • perhaps irrelevant, but could you post your maven compiler plugin configuration? – Yaneeve Nov 26 '13 at 13:05
  • 2
    The JavaDoc: http://docs.oracle.com/javase/6/docs/api/javax/tools/JavaCompiler.html – Raedwald Nov 26 '13 at 13:15
  • I think in your case the (boot)classpath is different. The difference is mainly in the way the environment is set up (by the launcher vs. by the plugin). It might also differ in default options, but I dont think so. – eckes Dec 29 '14 at 18:30

1 Answers1

24

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.

peterh
  • 11,875
  • 18
  • 85
  • 108
  • Good points, I do want to get more information about the differences between the operations functionality of those two ways, like the differences in the original post. – Vic Jun 30 '14 at 13:22
  • @Vic I extended my answer a little bit, and will do yet a little. – peterh Jun 30 '14 at 15:01
  • Thanks. What I wanted was an explanation of why would one mode throw errors and the second one won't. And other differences, if exist – Vic Jun 30 '14 at 15:09
  • @Vic The most probable cause is that in the external case you are using a different java compiler as in the case of the internal (which is the same as the maven uses). – peterh Jul 01 '14 at 10:18