22

When running Maven, I get this output:

[WARNING] Unable to autodetect 'javac' path, using 'javac' from the environment.

How do I fix this?

Promise Preston
  • 24,334
  • 12
  • 145
  • 143
Aaron Digulla
  • 321,842
  • 108
  • 597
  • 820

13 Answers13

25

You will see this error message when Maven is being run using a JRE (Java Runtime Environment) which is a stripped down version of Java that can only execute Java code but can't compile sources.

To fix the warning, install the JDK (Java Development Kit) and set the environment variable JAVA_HOME to the newly installed version of Java.

You can put this into the file .mavenrc if you want; then only Maven will use this version of Java.

Aaron Digulla
  • 321,842
  • 108
  • 597
  • 820
  • 4
    What if this happens in IntelliJ? – MiguelMunoz Jan 24 '20 at 19:05
  • 4
    @MiguelMunoz In the configuration for the Maven runner is an option to select the JRE to use. Make sure it points to a SDK. See the "JRE" field here: https://www.jetbrains.com/help/idea/delegate-build-and-run-actions-to-maven.html#build_through_maven – Aaron Digulla Jan 27 '20 at 10:16
  • @AaronDigulla thanks for this, I put in the environment variables box: JAVA_HOME=/usr/lib/jvm/jdk-14.0.1 and it worked fine – Simon B Jun 30 '21 at 09:20
  • Why this warning at all? I have JDK set up but did not bother to configure JAVA_HOME because I want to switch quickly between Java versions using OS-provided tooling, e.g. `archlinux-java`. All is fine for me, aside from the Maven warning. – mikhailian Jan 11 '22 at 13:50
  • @mikhailian: In most cases, this means that a "random" version of Java is being used (i.e. whatever is on the path). This leads to nonrepeatable builds for many people without them being aware of this. It's also not always possible to provide a clean path or to put Java on the path at all. JAVA_HOME will always work and solve all these problems. – Aaron Digulla Jan 13 '22 at 20:29
  • Check `Preferences | Build, Execution, Deployment | Build Tools | Maven | Runner -> JRE` if you are using IntelliJ. If not, check project structure. – Chris Wu Aug 30 '22 at 14:52
  • 1
    @ChrisWu This should be an answer; nobody will find this comment :-) – Aaron Digulla Aug 31 '22 at 16:35
  • Isn't this just a warning we could ignore? On a Debian 11 with `openjdk-17-jdk-headless` installed I'm getting this warnings while building [DSpace](https://wiki.lyrasis.org/display/DSDOC7x/Installing+DSpace) but the build ends succesfully. `javac` run fine, with or without JAVA_HOME set (with `sudo update-java-alternatives -l`) – Pablo Bianchi Jun 22 '23 at 07:06
8

If you're running Maven from Eclipse (e.g. Run As : Maven Install), make sure your environment is configured with correct JRE (you'll need JDK, not JRE). Go to Window -> Preferences -> Java -> Installed JRE. Select JDK if it's there or add JDK if it's not.

7

A friend of mine experienced this issue when working on building a Java project in Ubuntu 18.04.

When he runs the command:

maven clean build

He gets the warning:

[WARNING] Unable to autodetect 'javac' path, using 'javac' from the environment

And then gets this error:

[ERROR] Failed to execute goal org.apache.maven.plugins:maven-compiler-plugin:3.5.1:compile (default-compile) on project geostore-model: Compilation failure -> [Help 1]

Here's how it was solved:

The issue was that he did not have javac (Java programming language compiler) installed on his machine. To confirm, we ran the command:

javac --version

And we got no version output

So we installed the Java development kit that contains javac and all of its dependencies using the command:

sudo apt install default-jdk

Afterwhich we ran the command javac --version and got the output:

javac 11.0.9.1

That's all.

I hope this helps

Promise Preston
  • 24,334
  • 12
  • 145
  • 143
1

try to set JAVA_HOME in the MyComputer-->Environment Variable

vinay Maneti
  • 1,447
  • 1
  • 23
  • 31
1

sudo yum install java-sdk Centos finds the correct SDK with the -devel sufix

1

sudo yum install java-1.8.0-openjdk-devel for java version 8 sudo yum install java-11-openjdk-devel for java version 11

  • Note: This will just install Java. You may still have to point Maven to use the correct one using one of the other answers, for example when you have both 8 and 11 installed. – Aaron Digulla Oct 05 '22 at 08:34
1

If you are using IntelliJ:
Check Preferences | Build, Execution, Deployment | Build Tools | Maven | Runner -> JRE.
Also check File | Project Structure.
Most of the time you will find a wrong java version.

Chris Wu
  • 257
  • 3
  • 6
1

For people who don't find that the accepted answer applies: check if your maven-compiler-plugin has the fork option set to true in pom.xml, like this:

<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-compiler-plugin</artifactId>
    <version>3.8.1</version>
    <configuration>
        <fork>true</fork>
    </configuration>
</plugin>

This seems to be a bug in IntelliJ that hasn't been fixed as of 21.10.2022.

Dzeri96
  • 196
  • 2
  • 13
0

Right click on project -> Properties -> Java Compiler then uncheck Use compliance from execution environment... and then select a compiler in Compiler Compliance level

Mehraj Malik
  • 14,872
  • 15
  • 58
  • 85
0

On a Mac:

java_version=1.8
#version=1.8.0_91
export JAVA_HOME=$(/usr/libexec/java_home -v $java_version) 
Wolfgang Fahl
  • 15,016
  • 11
  • 93
  • 186
0

On Mac deleting the .idea folder in intelliJ worked for me

0

I am using IntelliJ 2022.3.2 and had to remove the "\bin" part from my JAVA_HOME path to fix the "problem".

romeucr
  • 169
  • 1
  • 13
0

analised:

<plugin>
            <artifactId>maven-compiler-plugin</artifactId>
            <configuration>
                <executable>/usr/local/pje/jdk-11.0.10+9/bin/javac</executable>
            </configuration>
        </plugin>