0

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:


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).

aCarella
  • 2,369
  • 11
  • 52
  • 85
  • 1
    *export JAVA_HOME=$(/Users/[user-name]/Library/Java/JavaVirtualMachines/corretto-11.0.17/Contents/Home)* That's quite wrong. You're attempting to assign the result of a *command* to a variable. That should be `export JAVA_HOME=$HOME/Library/Java/JavaVirtualMachines/corretto-11.0.17/Contents/Home` – g00se Dec 12 '22 at 17:27
  • @g00se thanks for the suggestion. I changed my export command to `export JAVA_HOME=$HOME/Library/Java/JavaVirtualMachines/corretto-11.0.17/Contents/Home`, and now when I run `source ~/.zshrc` I don't get the permission denied error. But when I run `echo $JAVA_HOME`, I still get a blank line. – aCarella Dec 12 '22 at 17:35
  • 1
    What about `echo $SHELL`? – g00se Dec 12 '22 at 17:46
  • @g00se `echo $SHELL` gives me `/bin/zsh`. – aCarella Dec 12 '22 at 18:11
  • Are you able to post your .zshrc as code? – g00se Dec 12 '22 at 18:19
  • _whenever I run almost any command in the terminal_ : This looks that you have a hook function installed, which is executed for every command prompt (and on certain conditions may invoke `jenv`). Perhaps it would make sense to save your .zshrc (and other zsh setup files you may have) to somewhere and recreate them piece by piece. – user1934428 Dec 13 '22 at 09:33

0 Answers0