57

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]
Grzegorz Oledzki
  • 23,614
  • 16
  • 68
  • 106
user1508893
  • 9,223
  • 14
  • 45
  • 57

9 Answers9

59

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
miku
  • 181,842
  • 47
  • 306
  • 310
  • I don't think I have a java-home package per se. This is on the Mac, and when I do `ls -l` on `\usr\bin\java`, which is the `java` that is running, I see that `java` links to this: `/System/Library/Frameworks/JavaVM.framework/Versions/Current/Commands/java`, which is a single executable! – user1508893 Dec 28 '12 at 21:50
  • 2
    it doesn't matter that you have java in your path... in order to use the JDK tools you need JAVA_HOME set to a JDK. having /usr/bin/java pointing to /System/Library/Frameworks/JavaVM.framework/Versions/Current/Commands/java by itself it doesn't *even* mean you have a JDK installed... maybe you just have a JRE (which doesn't include javadoc) – Luigi R. Viggiano Jan 04 '13 at 06:47
  • Note: don't include $() in your export. I.e. `export JAVA_HOME=/System/Library/Java/...` – Piper Jul 01 '14 at 18:27
  • 2
    When invoking Maven through a graphical IDE (such as IntelliJ) you can alleviate this problem by setting `JAVA_HOME` in `/etc/launchd.conf` like this: `setenv JAVA_HOME /Library/Java/Home`. Then run `sudo grep -E "^setenv" /etc/launchd.conf | xargs -t -L 1 launchctl` and relaunch the affected GUI programs. – Erik Gillespie Sep 22 '14 at 19:00
  • 3
    On my Ubuntu install the JVM path is `/usr/lib/jvm/java-8-openjdk-amd64/` – Eborbob Sep 10 '18 at 09:22
48

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

Matt Ke
  • 3,599
  • 12
  • 30
  • 49
Anthony Hayward
  • 2,164
  • 21
  • 17
  • 10
    There is a stupid bug in more recent versions of maven-javadoc-plugin in which it fails unless JAVA_HOME is set, despite the fact that javadoc runs on the command line. Apparently the developers don't use Macs, because the Mac Java install doesn't set JAVA_HOME. This seems to fix the problem. – ccleve Jul 04 '19 at 00:11
  • 10
    The bug report is [here](https://issues.apache.org/jira/browse/MJAVADOC-595). Please vote it up. (Not sure it will have any effect, but it’s worth trying.) – Olivier Cailloux Aug 27 '19 at 06:57
  • 2
    @ccleve I have de same problem in Ubuntu 18.10 but this fix it!! – E. Betanzos Oct 23 '19 at 22:06
  • This may fix the problem for some JDK versions, but not for all. It will not work, if the javadoc is built in multiple environments (e.g. locally usind JDK 8 and in some build pipeline such as GitHub actions using JDK 11). In such cases, it is better to work with maven profiles, see [this stackoverflow post](https://stackoverflow.com/questions/57081473/how-to-make-maven-javadoc-plugin-work-with-any-java-version). – Janothan Jan 08 '21 at 12:11
14

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!

Raghu
  • 1,363
  • 1
  • 23
  • 37
8

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/

Ivan Vučica
  • 9,529
  • 9
  • 60
  • 111
5

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.

Sreedhar GS
  • 2,694
  • 1
  • 24
  • 26
  • 1
    Thanks. This worked. I had no time and this work around worked just fine. I am sure the other comments are as good, but for a QUICK workaround this was great. – Beezer May 01 '16 at 11:43
5

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>
    
madx
  • 6,723
  • 4
  • 55
  • 59
Vasanth Umapathy
  • 1,571
  • 14
  • 7
1

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

Master Drools
  • 728
  • 6
  • 18
0

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.

Stefan L
  • 1,529
  • 13
  • 20
  • 2
    export JAVA_HOME=$(/usr/libexec/java_home -v 1.8) With MVN 3.5.2 this worked for me. I had to put it in mvnyjp and mvnDebug as well. – Bojan Petkovic Dec 05 '17 at 21:01
0

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

aksh1618
  • 2,245
  • 18
  • 37