Endless trouble setting JAVA_HOME via jenv for use in IntelliJ

2.9k Views Asked by At

I have a seemingly-common problem, but after scouring through the many posts (here and otherwise) of people experiencing the problem of not being able to properly set the JAVA_HOME path, no proposed solutions are solving my problem.

I recently installed Java via Homebrew (the version was Java16), but I needed to install Java8 for use in a specific IntelliJ project. I installed Java8 via Homewbrew as well and proceeded to also install jenv in order to manage the now-multiple versions of Java on my system.

In IntelliJ > Project Structure, I was able to specify the desired SDK and work in Java8—all was well up until here.

However, I subsequently needed to install Java14, which is when it all fell apart.

I followed the same procedures as when adding Java8, but now IntelliJ complains ERROR: JAVA_HOME is set to an invalid directory: /Users/user/.jenv/versions/system when any SDK is specified. Specifically, this error is thrown when attempting to build a .jar file via gradle by executing gradle jar

What I have done to attempt a fix (not explicitly in this order, and some commands executed multiple times):

jenv enable-plugin export

echo 'export PATH="$HOME/.jenv/bin:$PATH"' >> ~/.bash_profile

echo 'eval "$(jenv init -)"' >> ~/.bash_profile

jenv add $(/usr/libexec/java_home)

jenv add /Library/Java/JavaVirtualMachines/adoptopenjdk-14.jdk/Contents/Home/

jenv add /Library/Java/JavaVirtualMachines/adoptopenjdk-84.jdk/Contents/Home/

When this failed to work, I opened IntelliJ directly from Terminal, in order to inherit the bash_profile variables (a proposed solution I have encountered a few times).

Additionally, I also navigated to the porject's folder in Terminal and issued jenv java 14 in order to implement a folder-level specification.

Below are the results of certain Terminal executions that may be informative:

javac -version

javac 16.0.1

java -version

openjdk version "16.0.1" 2021-04-20
OpenJDK Runtime Environment Homebrew (build 16.0.1+0)
OpenJDK 64-Bit Server VM Homebrew (build 16.0.1+0, mixed mode, sharing)

which java

/Users/jzwi/.jenv/shims/java

ls -1 /Library/Java/JavaVirtualMachines

adoptopenjdk-14.jdk
adoptopenjdk-8.jdk
openjdk.jdk

/usr/libexec/java_home -V

Matching Java Virtual Machines (4):
    16.0.1, x86_64: "OpenJDK 16.0.1"    /Library/Java/JavaVirtualMachines/openjdk.jdk/Contents/Home
    15.0.3, x86_64: "Zulu 15.32.15" /Users/user/Library/Java/JavaVirtualMachines/azul-15.0.3/Contents/Home
    14.0.2, x86_64: "AdoptOpenJDK 14"   /Library/Java/JavaVirtualMachines/adoptopenjdk-14.jdk/Contents/Home
    1.8.0_292, x86_64:  "AdoptOpenJDK 8"    /Library/Java/JavaVirtualMachines/adoptopenjdk-8.jdk/Contents/Home

echo ${JAVA_HOME}

/Users/user/.jenv/versions/system

jenv version

system (set by /Users/user/.jenv/version)

jenv versions

* system (set by /Users/user/.jenv/version)
  1.8
  1.8.0.292
  14
  14.0
  14.0.2
  16
  16.0
  16.0.1
  openjdk64-1.8.0.292
  openjdk64-14.0.2
  openjdk64-16.0.1

The first question is: should echo ${JAVA_HOME} result in system? It seems that most tutorials and explanations result in something more explicit (e.g., /Library/Java/JavaVirtualMachines/jdk1.8.0_111.jdk/Contents/Home).

If system is not the desired result, how do I fix in ways that I have not already tried?

As a last piece of information, here is what I have in my .bash_profile itself:

eval export PATH="/Users/user/.jenv/shims:${PATH}"
export JENV_SHELL=bash
export JENV_LOADED=1
unset JAVA_HOME
source '/usr/local/Cellar/jenv/0.5.4/libexec/libexec/../completions/jenv.bash'
jenv rehash 2>/dev/null
jenv refresh-plugins
jenv() {
  typeset command
  command="$1"
  if [ "$#" -gt 0 ]; then
    shift
  fi

  case "$command" in
  enable-plugin|rehash|shell|shell-options)
    eval `jenv "sh-$command" "$@"`;;
  *)
    command jenv "$command" "$@";;
  esac
}
export PATH="$HOME/.jenv/bin:$PATH"
eval "$(jenv init -)"

The unset JAVA_HOME seems super suspicious, but I neither know what it means, how it got there, nor does commenting it out solve my problem.

Many thanks for any help !!

0

There are 0 best solutions below