Maven does not find Java compiler despite having set JAVA_HOME - maven

I've set
stefan#stefan:~$ echo $JAVA_HOME
/usr/lib/jvm/java-8-openjdk-amd64
(I've also tried /usr/lib/jvm/default-java)
but if I run mvn clean install on my project I see
[ERROR] Failed to execute goal org.apache.maven.plugins:maven-compiler-plugin:3.6.0:compile (default-compile) on project shared: Compilation failure
[ERROR] No compiler is provided in this environment. Perhaps you are running on a JRE rather than a JDK?
The respective entry in the pom.xml for the maven-compiler-plugin is set to Java 8:
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.6.0</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
I don't know what I am missing out here. How can I tell Maven where the compiler is?

Please try following,
On console, run java -version and see if it works fine.
Run javac -version and see if it works
If any of the above does not work, it means
Your installation is not correct. Run sudo apt-get install openjdk-8-jdk again.
There is a broken previous java installation. Step 1 should again solve this problem.

Set JAVA_HOME to JDK instead of JRE.

You need to add JDK_PATH/bin to the PATH.

Related

Confusion on maven compiler plugin

I am quite confused about maven compiler plugin and what it does. I have a project that has several modules. In my top pom.xml I have a section
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>2.5.1</version>
<inherited>true</inherited>
<configuration>
<source>1.7</source>
<target>1.7</target>
</configuration>
</plugin>
My understanding is this specifies the JDK compiler version used to compile the code, and this section get inherited by all the modules. What I don't get is in my IntelliJ IDEA I can still specify project JDK in the settings and it seems like that setting overrides this. When I run maven install in the IDE I can confirm that it is using javac from JDK 8 to compile. So what does this section do exactly?
You are correct; maven-compiler settings should be inherited by child modules.
I don't know about IntelliJ, but I can tell you that Eclipse picks&chooses whatever it wants from maven config, and for all the rest it uses its own settings.
Therefore, I'd expect IntelliJ may do something similar?
The simplest way to test this is to run a mvn clean install via command line, and see which "wins". If you get artifact compiled with 1.8 then it means you're missing something in Maven config which causes those settings not to propagate to children. If you get artifact compiled with 1.7 then it is IntelliJ who does it and not maven-compiler-plugin.

I am getting an error in mule 3.7 maven dependencies

when i am trying to update the dependencies in maven it is throwing an error "There was an error running the studio:studio goal on project " please help me in this.
You are probably using mule-app-maven-plugin without configuring extensions to true. Try to edit your pom.xml and make the config look like this:
<plugin>
<groupId>org.mule.tools.maven</groupId>
<artifactId>mule-app-maven-plugin</artifactId>
<version>1.0</version>
<extensions>true</extensions>
</plugin>
From the exception you're pasting in your comments, it looks like you're trying to build your Mule APP with a JRE, in this case you would need to build using a JDK, please make sure your JAVA_HOME environment variable points to a JDK and attempt the build.

Unable to find javadoc command - maven

I ran this command in my project directory to build and package it:
mvn clean javadoc:jar package
I do have my JAVA_HOME variable set correctly.
Evidently:
$ which java
/usr/bin/java
$ sudo ls -l /usr/bin/java
lrwxr-xr-x 1 root wheel 74 Dec 18 23:42 /usr/bin/java -> /System/Library/Frameworks/JavaVM.framework/Versions/Current/Commands/java
$ which javadoc
/usr/bin/javadoc
Does anyone know why mvn still complains?
Failed to execute goal org.apache.maven.plugins:maven-javadoc-plugin:2.8:jar (default-cli) on project foo_bar: MavenReportException: Error while creating archive: Unable to find javadoc command: The environment variable JAVA_HOME is not correctly set. -> [Help 1]
A correct which java is not evidence enough, since /usr/bin/ will likely be in your PATH anyway. Check
$ echo $JAVA_HOME
for evidence. Or run
$ JAVA_HOME=/path/to/your/java/home mvn clean javadoc:jar package
On OS X you can set your JAVA_HOME via:
$ export JAVA_HOME=$(/usr/libexec/java_home)
which on my machine points to
/System/Library/Java/JavaVirtualMachines/1.6.0.jdk/Contents/Home
You can make it use the java.home system property instead of the JAVA_HOME environment variable:
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-javadoc-plugin</artifactId>
<version>3.0.1</version>
<configuration>
<javadocExecutable>${java.home}/bin/javadoc</javadocExecutable>
</configuration>
</plugin>
Source of idea: https://medium.com/#kankvish/fixing-issue-the-environment-variable-java-home-is-not-correctly-set-b5f0b66a84d0
You can add the JAVA_HOME as an environment variable in eclipse.
Go to your maven build-> add the following.
Click New->
Name: JAVA_HOME
Value : your path here.
This worked for me!
While a lot of answers talk about OS X, for a Debian or Debian-like system (such as Ubuntu), I've decided to abuse the "alternatives" system:
export JAVA_HOME=$(update-alternatives --query javadoc | grep Value: | head -n1 | sed 's/Value: //' | sed 's#bin/javadoc$##')
Rewriting that more cleanly with awk, or using a more correct way to access the value in the "alternatives" database, is left as an exercise for the reader.
Alternatively, given that the point of using "alternatives" system is to maintain symlinks such as /usr/bin/javadoc in this case, we can just query the path pointed to by the symlink:
export JAVA_HOME=$(realpath /usr/bin/javadoc | sed 's#bin/javadoc$##')
While this isn't the only possible "Java home" (you might have numerous JDKs installed), given that I only care about moving the mvn build forward, and the error talks about Javadoc, I chose to refer to this the directory containing the javadoc binary.
Don't forget to install a JDK in addition to a JRE. For instance, the JDK I needed was openjdk-11-jdk, to complement the JRE openjdk-11-jre which I previously installed.
After the above, the JAVA_HOME envvar has this value on my system: /usr/lib/jvm/java-11-openjdk-amd64/
After spending 2-3 hours of time, i felt opening Eclipse via command line looks easiest solution.
Follow below steps,
cd <Folder_where_Eclipse.app>
open Eclipse.app
Now your eclipse can able to find the Terminal Environmental variables.
There are 2 options to fix this. Here are the steps:
Make sure to configure JAVA_HOME as an environment variable.
Option 1: Add javadocExecutable into properties.
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>${project.build.sourceEncoding
</project.reporting.outputEncoding>
<java.version>11</java.version>
<javadocExecutable>${java.home}/bin/javadoc</javadocExecutable>
</properties>
Option 2: Add javadocExecutable into the build section as below.
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-javadoc-plugin</artifactId>
<version>${maven-javadoc-plugin.version}</version>
<executions>
<execution>
<id>attach-javadocs</id>
<goals>
<goal>jar</goal>
</goals>
<configuration>
<additionalparam>-Xdoclint:none</additionalparam>
</configuration>
</execution>
</executions>
<configuration>
<javadocExecutable>${java.home}/bin/javadoc</javadocExecutable>
<excludePackageNames>com.vu.poc.test.objects</excludePackageNames>
<overview />
</configuration>
</plugin>
Upgrade maven-javadoc-plugin plugin to latest version (3.3.0 or later).
They fixed this for OSX in maven 3.1 by adding "export JAVA_HOME" to the "bin/mvn" shell script, obviating the need to set JAVA_HOME externally yourself just to find javadoc.
I started facing this issue once I switched to using sdkman and removed Ubuntu's java packages. Everything else works fine, but the javadoc plugin fails when using IntelliJ IDEA's bundled maven. Thankfully, we can set environment variables at a per project level in
Build, Execution, Deployment > Build Tools > Maven > Runner
In the Environment variables: text box, add
JAVA_HOME=/home/user/.sdkman/candidates/java/11.0.12-open/

Maven Error " annotations are not supported....."

This is one of the most annoying errors ? What I can understand is that I am using a lower version of Java for compiling. How can I specify java version for maven ?
Failed to execute goal
org.apache.maven.plugins:maven-compiler-plugin:2.0.2:compile
(default-compile) on project
springAopMavenDemo: Compilation
failure D:\JAVA
Stuffs\projects\springAopMavenDemo\src\main\java\service\EmployeeServiceImpl.java:[13,1]
annotations are not supported in
-source 1.3 (use -source 5 or higher to enable annotations) #Service
-> [Help 1]
To see the full stack trace of the
errors, re-run Maven with the -e
switch. Re-run Maven using the -X
switch to enable full debug logging.
For more information about the errors
and possible solutions, please read
the following articles: [Help 1]
http://cwiki.apache.org/confluence/display/MAVEN/MojoFailureException
I really appreciate any help......I am using NetBeans 7.0 and Maven 3
You need to tell maven which version of java the source should be compiled to
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>2.3.2</version>
<configuration>
<source>1.5</source>
<target>1.5</target>
</configuration>
</plugin>
</plugins>
http://maven.apache.org/plugins/maven-compiler-plugin/examples/set-compiler-source-and-target.html
You are using some annotated code in your java, but compiling in your ide is using source java 1.3 by passing an additional commandline parameter most likely
I had the same thing in intellij a while back
In netbeans config (sorry I am not a netbeans person) find where jdk/compiler setup is and change the command line arg
Update: quick net serach says it may be under Properties > Build > Compiling
emeraldjava answer is correct. However, I want to add two things:
First, you can set the source and target information as Maven properties:
<build>
...
<maven.compiler.source>1.5</maven.compiler.source>
<maven.compiler.target>1.5</maven.compiler.target>
Second, if you use the latest version of the Maven compiler plugin (2.3.2 for example), the default version for compiler is 1.5, so you will not need any additional configuration:
The Compiler Plugin is used to compile
the sources of your project. The
default compiler is javac and is used
to compile Java sources. Also note
that at present the default source
setting is 1.5 and the default target
setting is 1.5, independently of the
JDK you run Maven with.
(source)

Maven Compiler Plugin Fork Option Not Working

I have a maven pom file with the following compiler-plugin:
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<version>2.0.2</version>
<configuration>
<executable>${path_to_JDK6}</executable>
<fork>true</fork>
<compilerVerison>1.6</compilerVerison>
<source>1.6</source>
<target>1.6</target>
</configuration>
</plugin>
My system's JAVA_HOME is set to a 1.5 JDK. When I run mvn package, maven throws the following error message:
Failure executing javac, but could not parse the error:
javac: invalid target release: 1.6
Usage: javac <options> <source files>
Does anyone have any ideas about why Maven is using the 1.5 JDK instead of forking to the 1.6 executable? Is there any debugging option that I could use? Are the fork and executable options broken in Maven 2?
Note: My system admin would not allow me to change the value of JAVA_HOME and some of my new libraries are written for java 6. So I would like to find a workaround to make maven compile with JDK 1.6.
My ${path_to_JDK6} variable only included the path to the folder containing the JDK. It did not include the /bin/javac. When I added /bin/javac, the 1.6 compiler was invoked.

Resources