Unable to find javadoc command - maven

MacosMavenJavadoc

Macos Problem Overview


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]

Macos Solutions


Solution 1 - Macos

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

Solution 2 - Macos

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

Solution 3 - Macos

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.

enter image description here

This worked for me!

Solution 4 - Macos

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/

Solution 5 - Macos

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.

Solution 6 - Macos

There are 2 options to fix this. Here are the steps:

  1. Make sure to configure JAVA_HOME as an environment variable.

  2. 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>
    
  3. 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>
    

Solution 7 - Macos

Upgrade maven-javadoc-plugin plugin to latest version (3.3.0 or later).

Solution 8 - Macos

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.

Solution 9 - Macos

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/

Screenshot showing IntelliJ IDEA Maven Runner settings

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
Questionuser1508893View Question on Stackoverflow
Solution 1 - MacosmikuView Answer on Stackoverflow
Solution 2 - MacosAnthony HaywardView Answer on Stackoverflow
Solution 3 - MacosRaghuView Answer on Stackoverflow
Solution 4 - MacosIvan VučicaView Answer on Stackoverflow
Solution 5 - MacosSreedhar GSView Answer on Stackoverflow
Solution 6 - MacosVasanth UmapathyView Answer on Stackoverflow
Solution 7 - MacosMaster DroolsView Answer on Stackoverflow
Solution 8 - MacosStefan LView Answer on Stackoverflow
Solution 9 - Macosaksh1618View Answer on Stackoverflow