-2

I read about this the maven-compiler-plugin. On the linked page, it told:

Currently, the Compiler Plugin is bundled with the javac compiler artifact with artifactId plexus-compiler-javac, which can be seen as one of the dependencies declared inside the Compiler Plugin's POM

According to my understanding, we do not need the local javac anymore, say on my local operating system, mere JRE instead of JDK is enough.

I did try to remove JDK and installed only JRE on my local system. However, when I tried to compile with maven with command mvn clean compile, there was the error message -

No compiler is provided in this environment. Perhaps you are running on a JRE rather than a JDK?

Could anyone explain why is it so? Probably my understanding is wrong?

Pshemo
  • 122,468
  • 25
  • 185
  • 269
Rui
  • 3,454
  • 6
  • 37
  • 70
  • 6
    "According to my understanding, we do not need the local javac anymore." well yeah, unless you want to **compile** something. – Kayaman Feb 10 '18 at 15:45
  • 1
    @Kayaman my understanding is: if I want to compile something, the `plexus-compiler-javac` should be able to be used in place of local `javac` – Rui Feb 10 '18 at 15:49
  • What part of *your undersanding is wrong and based on a wrong **assumption** based on mis-reading the documentation*? –  Feb 10 '18 at 15:50
  • 1
    What part of 'contrary to this plugin's name, the Compiler Plugin does not compile the sources of your project by itself' didn't you understand? It is the first line of the documentation you cited. – user207421 Feb 10 '18 at 15:58
  • 1
    Read the entire documentation for comprehension next time. Emphasis is mine; ***Contrary to this plugin's name, the Compiler Plugin does not compile the sources of your project by itself.** To compile, the Compiler Plugin uses another class to compile them. The parameter compilerId determines which class will be used. Currently, the Compiler Plugin is bundled with the javac compiler artifact with artifactId plexus-compiler-javac, which can be seen as one of the dependencies declared inside the Compiler Plugin's POM.* –  Feb 10 '18 at 19:45
  • You still need the Oracle Java compiler, which is in the JDK, not in the JRE, and not available separately from Maven Central. – Thorbjørn Ravn Andersen Feb 10 '18 at 21:15

2 Answers2

2

From the documentation :

Plexus Compiler

Plexus Compiler is a Plexus component to use different compilers through a uniform API.

Contrary to this plugin's name, the Compiler Plugin does not compile the sources of your project by itself.

So the plexus-compiler-javac artifact has to be considered as a wrapper to compile.
It is not stated that it contains the javac program.

To be exact, from the 3.0 version, it is not directly javac any longer that is by default used but javax.tools (more particularly javax.tools.JavaCompiler).
The maven-compiler-plugin documentation states indeed :

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

You have to understand that the Maven core plugin will not reinvent the wheel and guess what you need.
To compile java classes you need a Java compiler and generally you want to able to ensure that you are using a specific Java compiler as OS and Java version matter.
So the maven compiler plugin needs and uses a JDK under the hood.

user207421
  • 305,947
  • 44
  • 307
  • 483
davidxxx
  • 125,838
  • 23
  • 214
  • 215
  • We did compilations with Maven using plain JRE - which doesn't have javac - and the plexus Eclipse compiler plugin - this was back around version 3.5. For projects which _really_ need long term support this is a viable option. – Thorbjørn Ravn Andersen Feb 10 '18 at 18:27
  • @ThorbjørnRavnAndersen - that is because Eclipse includes a **non-standard** compiler of it its own in older versions. Maven never included `javac`. Not sure about now because I switched to Intellij as soon as it came out. –  Feb 10 '18 at 19:43
  • @JarrodRoberson I am not sure that "*non-standard*" compiler is the right term. They wrote their own but the byte code format is well defined so it typically does not matter which compiler did the work. Those working in Eclipse use it all the time. https://stackoverflow.com/a/33165304/53897 demonstrates how to use it with maven. An additional feature is that it allows for compilation of very old code with new releases of Java where javac will not anymore. I am not advocating either; competition is healthy in compilers. – Thorbjørn Ravn Andersen Feb 10 '18 at 21:14
1

Maven is not a java compiler. Maven runs the java compiler for you and configures all the paths.

When you run mvn -X compile you will see the compiler details.

In conclusion: you need a java compiler installed to compile java - even when maven starts the compiler for you.

user2088476
  • 603
  • 6
  • 15
  • Thanks first for your answer :) Then what is the purpose of `plexus-compiler-javac` dependency in the *maven-compiler-plugin*? In my opinion, `plexus-compiler-javac` should be able to be used in place of local `javac`. But seems that my understanding is wrong. If I am wrong, I wish anyone can help modify my understanding :) – Rui Feb 10 '18 at 15:55
  • **NO** that artifact is the one that supports `javac` it says nothing about including the `javac` executable which would be impossible to do in a platform neutral way. Think about it, use some common sense. –  Feb 10 '18 at 15:57
  • @JarrodRoberson Great! Thanks a lot for your explanation :) You can answer it formally then I will check it :) (Y) – Rui Feb 10 '18 at 16:00
  • 1
    @JarrodRoberson I am not native English speaker but when I see "Plugin is bundled with the `javac`" it gives me impression that `javac` may be also *provided* with plugin, not that it just *uses* available `javac`. – Pshemo Feb 10 '18 at 16:01
  • 2
    @Pshemo There's a lot more to see than that. See the *first line* of the document he cited. – user207421 Feb 10 '18 at 16:09
  • 1
    @Pshemo - you like the OP are taking part of a complete sentance and paragraph out of content. Here is the complete context; ***Contrary to this plugin's name, the Compiler Plugin does not compile the sources of your project by itself.** To compile, the Compiler Plugin uses another class to compile them. The parameter compilerId determines which class will be used. Currently, the Compiler Plugin is bundled with the javac compiler artifact with artifactId plexus-compiler-javac, which can be seen as one of the dependencies declared inside the Compiler Plugin's POM.* –  Feb 10 '18 at 19:41