3

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 !!

ZwiTrader
  • 195
  • 1
  • 2
  • 12
  • Is the issue specific to IntelliJ IDEA? Does it work fine for the apps in the system terminal, like Gradle, etc? What's the tree view of `/Users/user/.jenv/versions/system`? – CrazyCoder Jul 13 '21 at 19:02
  • I don't see why you even use jenv to set a different java version? I would also suggest to stop using home-brew for installing java and use sdkman instead. Then just select the version of java you want to use for your project and stop messing around with JAVA_HOME. – M. Deinum Jul 13 '21 at 19:07
  • I was originally executing `gradle jar` in the Terminal window *inside* of IntelliJ, but when separately opening Terminal and issuing `gradle jar` I get the same problem `ERROR: JAVA_HOME is set to an invalid directory: /Users/jzwi/.jenv/versions/system Please set the JAVA_HOME variable in your environment to match the location of your Java installation.` What do you mean by "tree view" of that path? – ZwiTrader Jul 13 '21 at 19:10
  • @M.Deinum I am new to all of this and was using Homewbrew and jenv because they both seemed like the overwhelmingly encouraged option. It would be helpful for me if you would tell me why sdkman is preferrable (what pitfalls does it avoid that jenv does not, etc.), and an example of how I would go about setting things correctly with sdkman. Thanks. – ZwiTrader Jul 13 '21 at 19:19
  • "What do you mean by "tree view" of that path?". What files/directories are there? `ls -lR /Users/jzwi/.jenv/versions/system` output. – CrazyCoder Jul 13 '21 at 19:40
  • very strangely, the result is `No such file or directory`... – ZwiTrader Jul 13 '21 at 19:49
  • So it explains why the JDK symlink doesn't work. Make sure it points to the valid Java installation home. – CrazyCoder Jul 13 '21 at 19:50
  • Could you please explain how I would properly do that. From what I understood, the steps I had taken were explicitly intended to do just that (i.e., point to a valid installation). Also, if such a folder `system` does not exist in the `versions` folder, how is that path even set as the pointer? What is sabotaging the process by pointing to something that doesn't even exist—is it `jenv`, or did I do something wrong along the way? – ZwiTrader Jul 13 '21 at 19:56
  • With SDKMAN you can install as many java distributions you like and for Intellij to work you don't need to mess around with JAVA_HOME (which is the source of your trouble). Jus point your project to the proper JDK (that should even be doable with installing through homebrew). You don't need to mess around with jenv. – M. Deinum Jul 14 '21 at 05:43
  • @M.Deinum would you recommend I uninstall jenv and the current Java versions I have prior to attempting to install again via SDKMAN—or can I simply install SDKMAN on top of what I have, as a fix to my current barrier? I am a bit worried about making things worse by (inadvertently ) partially uninstalling Java versions and further entangling things. Thanks. – ZwiTrader Jul 14 '21 at 17:20
  • THe main issue is that you think you need to switch the JAVA_HOME for intellij to use the proper JDK. You don't need that. Just point Intellij to the proper JDK you want to use and it will work. – M. Deinum Jul 15 '21 at 05:52

0 Answers0