I am trying to fix Java for projects I am working on on my local computer. I had jenv installed to switch between versions more easily, but at some point over the last couple of days I must have done something and messed up my JAVA_HOME
environment variable and now I can't seem to fix it.
I use zsh, and I am on a Mac running MacOS Monterey 12.2. I first tried uninstalling jenv (brew uninstall jenv
- just wasn't really using it and seemed to be making things more confusing to solve this issue). Even though I did that and took jenv out of my $PATH
variable, whenever I run almost any command in the terminal the statement "jenv:11: command not found: jenv" prints. I'm not sure if that is a lesser issue, but something that probably needs to be solved.
My bigger issue is fixing Java on my machine. Up until a couple days ago this was not an issue, but now if I try to set JAVA_HOME
in my .zshrc
file like this
export JAVA_HOME=$(/Users/[user-name]/Library/Java/JavaVirtualMachines/corretto-11.0.17/Contents/Home)
and run source ~/.zshrc
I get the message:
/Users/[user-name]/.zshrc:119: permission denied: /Users/[user-name]/Library/Java/JavaVirtualMachines/corretto-11.0.17/Contents/Home
This happens no matter which Java version I try to set JAVA_HOME
to. My Java installation directories and versions are as follows:
# dir - /Library/Java/JavaVirtualMachines
drwxr-xr-x 3 root wheel 96 Apr 20 2021 adoptopenjdk-8.jdk
drwxr-xr-x 3 root wheel 96 Oct 19 08:54 temurin-11.jdk
# dir - /Users/[user-name]/Library/Java/JavaVirtualMachines
drwxr-xr-x 3 [user-name] staff 96 Dec 10 11:36 corretto-11.0.17
drwxr-xr-x 3 [user-name] staff 96 Sep 30 09:56 corretto-17.0.4.1
Running /usr/libexec/java_home -V
prints the following:
Matching Java Virtual Machines (4):
17.0.4 (x86_64) "Amazon.com Inc." - "Amazon Corretto 17" /Users/[user-name]/Library/Java/JavaVirtualMachines/corretto-17.0.4.1/Contents/Home
11.0.17 (x86_64) "Amazon.com Inc." - "Amazon Corretto 11" /Users/[user-name]/Library/Java/JavaVirtualMachines/corretto-11.0.17/Contents/Home
11.0.17 (x86_64) "Eclipse Adoptium" - "OpenJDK 11.0.17" /Library/Java/JavaVirtualMachines/temurin-11.jdk/Contents/Home
1.8.0_292 (x86_64) "AdoptOpenJDK" - "AdoptOpenJDK 8" /Library/Java/JavaVirtualMachines/adoptopenjdk-8.jdk/Contents/Home
/Users/[user-name]/Library/Java/JavaVirtualMachines/corretto-17.0.4.1/Contents/Home
If I run echo $JAVA_HOME
, a blank line prints followed by jenv:11: command not found: jenv
.
I tried to run unset JAVA_HOME
, followed by either:
export JAVA_HOME=/Users/[user-name]/Library/Java/JavaVirtualMachines/corretto-11.0.17/Contents/Home
or
export JAVA_HOME=$(/Users/[user-name]/Library/Java/JavaVirtualMachines/corretto-11.0.17/Contents/Home)
and don't get a permission denied error, only the jenv:11: command not found: jenv
complaint. But after, if I run echo $JAVA_HOME
, I still get a blank line followed by the jenv complaint.
It seems like no matter what I do, something keeps preventing me from setting JAVA_HOME
.
Out of desperation, I ran mod 777 ~/.zshrc
, and also checked System Preferences -> Security and Privacy -> Full Disk Access and made sure that iTerm was checked in that list, and to check my user permissions I ran sudo -l
and got the following:
User [user-name] may run the following commands on [computer-name]:
(ALL) ALL
None of that seemed to work either.
I also use IntelliJ as my Java IDE; I don't know if something changed there that I'm unaware of.
Any help is appreciated. I would really just like to change my java version and have it working properly on my machine, and up until recently (aside from using jenv) I would change my JAVA_HOME
value to wherever I had a version of Java installed.
Edit: Worth noting that when I run java -version
, I get the following:
java -version
openjdk version "17.0.4.1" 2022-08-12 LTS
OpenJDK Runtime Environment Corretto-17.0.4.9.1 (build 17.0.4.1+9-LTS)
OpenJDK 64-Bit Server VM Corretto-17.0.4.9.1 (build 17.0.4.1+9-LTS, mixed mode, sharing)
I've also gone through various tutorials and other stackoverflow questions on how to set the JAVA_HOME
environment variable, including:
- https://mkyong.com/java/how-to-set-java_home-environment-variable-on-mac-os-x/
- How to set JAVA_HOME environment variable on Mac OS X 10.9?
- https://www.youtube.com/watch?v=mENryUqoNzY
UPDATE: As per a suggestion in the comments below, I changed my export statement in ~/.zshrc
to
export JAVA_HOME=$HOME/Library/Java/JavaVirtualMachines/corretto-11.0.17/Contents/Home
When I run source ~/.zshrc
after that change, the console doesn't print permission denied
, but when I run echo $JAVA_HOME
I still get a blank line (followed by the jenv complaint).