Mac os Big Sur | No compiler is provided in this environment. Perhaps you are running on a JRE rather than a JDK?

Viewed 17938

I am getting this error on the screenshot and I have seen other posts about it, however, the fixes provided are mainly for Windows, so whatever I tried did not fix the issue on my Mac.

Currently, when I run /usr/libexec/java_home it points me here: /Library/Internet Plug-Ins/JavaAppletPlugin.plugin/Contents/Home

So when running a maven project it throughs message like this: No compiler is provided in this environment. Perhaps you are running on a JRE rather than a JDK?

So, I am wondering if the location of JAVA_HOME is incorrect, and if so how can I fix it? Sorry, if asking something obvious questions, but I am new to this.

I would really appreciate your help.

enter image description here

5 Answers

I meet the same question as yours.
This solution (Build failure run "mvn clean install". I just upgraded Big Sur) is to remove useless jre.
Mine is to give mvn shell a correct java_home.

Solution - edit mvn bash

Step 1: Check how to filter your target java by /usr/libexec/java_home

When I run command /usr/libexec/java_home -V

I can see 2 virtual machines

MacBook-Pro:bin charles$ /usr/libexec/java_home -V
Matching Java Virtual Machines (2):
    1.8.251.08 (x86_64) "Oracle Corporation" - "Java" /Library/Internet Plug-Ins/JavaAppletPlugin.plugin/Contents/Home
    1.8.0_251 (x86_64) "Oracle Corporation" - "Java SE 8" /Library/Java/JavaVirtualMachines/jdk1.8.0_251.jdk/Contents/Home
/Library/Internet Plug-Ins/JavaAppletPlugin.plugin/Contents/Home

My default return value of /usr/libexec/java_home is my jre path such as
/Library/Internet Plug-Ins/JavaAppletPlugin.plugin/Contents/Home

But I can change the return jdk path
/Library/Java/JavaVirtualMachines/jdk1.8.0_251.jdk/Contents/Home

if I run command
/usr/libexec/java_home -v1.8.0

Step 2: Update mvn shell

Find following lines in mvn shell


if [ -z "$JAVA_HOME" ] && [ -x "/usr/libexec/java_home" ]; then
    #
    # Apple JDKs
    #
    export JAVA_HOME=`/usr/libexec/java_home`
fi

Because I can use usr/libexec/java_home -v1.8.0 to make sure system using jdk, I update my shell as

if [ -z "$JAVA_HOME" ] && [ -x "/usr/libexec/java_home" ]; then
    #
    # Apple JDKs
    #
    export JAVA_HOME=`/usr/libexec/java_home -v1.8.0`
fi

And then, it works.

How to check difference

mvn -version

Before I update my mvn, I will see

MacBook-Pro:~ charles$ mvn -version
Apache Maven ........
Maven home: ........
Java version: 1.8.0_251, vendor: Oracle Corporation
Java home: /Library/Internet Plug-Ins/JavaAppletPlugin.plugin/Contents/Home
Default locale: zh_TW, platform encoding: UTF-8
OS name: "mac os x", version: "10.16", arch: "x86_64", family: "mac"

After I update my mvn, I can see the result

MacBook-Pro:~ charles$ mvn -version
Apache Maven ........
Maven home: .......
Java version: 1.8.0_251, vendor: Oracle Corporation
Java home: /Library/Java/JavaVirtualMachines/jdk1.8.0_251.jdk/Contents/Home/jre
Default locale: zh_TW, platform encoding: UTF-8
OS name: "mac os x", version: "10.16", arch: "x86_64", family: "mac"

Though java home display as jre path, but the jre is sub-folder inside of jdk. It's ok.

The default JRE comes with Mac is /Library/Internet Plug-Ins/JavaAppletPlugin.plugin/Contents/Home

% /usr/libexec/java_home
/Library/Internet Plug-Ins/JavaAppletPlugin.plugin/Contents/Home

I have installed JDK using a dmg file. Even after passing the JAVA_HOME to the new path from bashrc (/Library/Java/JavaVirtualMachines/jdk1.8.0_202.jdk/Contents/Home), it was not taking.

To list the avaiable java installations

% /usr/libexec/java_home -V
Matching Java Virtual Machines (2):
    1.8.311.11 (x86_64) "Oracle Corporation" - "Java" /Library/Internet Plug-Ins/JavaAppletPlugin.plugin/Contents/Home
    1.8.0_202 (x86_64) "Oracle Corporation" - "Java SE 8" /Library/Java/JavaVirtualMachines/jdk1.8.0_202.jdk/Contents/Home

In order to point to your JDK, just do the below.

% cd /Library/Internet\ Plug-Ins
% ls
JavaAppletPlugin.plugin
$ sudo mv JavaAppletPlugin.plugin JavaAppletPlugin.plugin-backup 

After that, if you run the command

% /usr/libexec/java_home
/Library/Java/JavaVirtualMachines/jdk1.8.0_202.jdk/Contents/Home

Do not attempt to uninstall Java by removing the Java tools from /usr/bin. This directory is part of the system software and any changes will be reset by Apple the next time that you perform an update of the OS.

I was having the same issue, and could not fix it using the answers provided, maven kept using the default jre and I was always getting the "No compiler error..". I managed to solve it setting the JAVA_HOME environment, so in my case I used the command:

export JAVA_HOME=/Library/Java/JavaVirtualMachines/jdk1.8.0_331.jdk/Contents/Home

After this command, the command mvn -v now outputs:

Maven home: /opt/homebrew/Cellar/maven/3.8.5/libexec Java version: 1.8.0_331, vendor: Oracle Corporation, runtime: /Library/Java/JavaVirtualMachines/jdk1.8.0_331.jdk/Contents/Home/jre

Although it says /jre, the command that was failing, "mvn clean package -D skipTests", now executes properly.

So hope it helps somebody.

Run the command to list the available java installations:

$ /usr/libexec/java_home -V

Matching Java Virtual Machines (2):

1.8.251.08 (x86_64) "Oracle Corporation" - "Java" /Library/Internet Plug-Ins/JavaAppletPlugin.plugin/Contents/Home
1.8.0_251 (x86_64) "Oracle Corporation" - "Java SE 8" /Library/Java/JavaVirtualMachines/jdk1.8.0_251.jdk/Contents/Home

Check the default return value of the following command which is jre path in my case:

$/usr/libexec/java_home

/Library/Internet Plug-Ins/JavaAppletPlugin.plugin/Contents/Home

Run the command to get JDK path:

$ /usr/libexec/java_home -v1.8.0

/Library/Java/JavaVirtualMachines/jdk1.8.0_251.jdk/Contents/Home

Add the following line in .bash_profile file:

export JAVA_HOME=$(/usr/libexec/java_home -v1.8.0)

Go to your terminal and edit the zshrc File with your own JAVA_HOME version

For step 3 enter your java version

Type:

  1. vim ~/.zshrc
  2. press i to insert and add your path to JAVA_HOME
  3. export JAVA_HOME=/Library/Java/JavaVirtualMachines/jdk1.8.0_261.jdk/Contents/Home
  4. press control + c to exit insert mode on vim
  5. Type :wq and it will save the document and quit vim editor
  6. source ~/.zshrc
  7. mvn -version

After you type mvn -version after all these steps You will be able to see your JAVA version and vendor update from the default "/Library/Internet Plug-Ins/JavaAppletPlugin.plugin/Contents/Home" to the java version you mentioned.

Related