When running Maven, I get this output:
[WARNING] Unable to autodetect 'javac' path, using 'javac' from the environment.
How do I fix this?
When running Maven, I get this output:
[WARNING] Unable to autodetect 'javac' path, using 'javac' from the environment.
How do I fix this?
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.
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.
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
sudo yum install java-sdk
Centos finds the correct SDK with the -devel sufix
sudo yum install java-1.8.0-openjdk-devel
for java version 8
sudo yum install java-11-openjdk-devel
for java version 11
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.
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.
Right click on project -> Properties -> Java Compiler
then uncheck
Use compliance from execution environment... and then select a compiler in Compiler Compliance level
On a Mac:
java_version=1.8
#version=1.8.0_91
export JAVA_HOME=$(/usr/libexec/java_home -v $java_version)
I am using IntelliJ 2022.3.2 and had to remove the "\bin" part from my JAVA_HOME path to fix the "problem".
analised:
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<executable>/usr/local/pje/jdk-11.0.10+9/bin/javac</executable>
</configuration>
</plugin>