0

I use Java 8 and Java 17 versions in several projects and I want to set default Java HOME as Java 8. For this purpose, I add .zshenv file to my user folder and make its content like this:

# Java 8
export PATH=/usr/local/opt/openjdk@8/bin:$PATH
# sudo ln -sfn /usr/local/opt/openjdk@8/libexec/openjdk.jdk /Library/Java/JavaVirtualMachines/adoptopenjdk-8.jdk 


# Java 17
# export PATH=/usr/local/opt/openjdk@17/bin:$PATH


# Maven
export PATH=~/apache-maven-3.8.6/bin:$PATH


export JAVA_HOME=$(/usr/libexec/java_home -v1.8)
# export JAVA_HOME=$(/usr/libexec/java_home -v 17)

export LC_ALL=en_US.UTF-8
export LANG=en_US.UTF-8
export LANGUAGE=en_US.UTF-8

However, I get some problems related to not being able to set Java Home properly. So, could you please clarify me on how can I set Java paths and set Java8 default path using .zsh way8 ?

I use MacOS Monterey.

Mark Rotteveel
  • 100,966
  • 191
  • 140
  • 197
March
  • 1
  • 2
  • *However, I get some problems related to not being able to set Java Home properly.* What does ```/usr/libexec/java_home -v1.8``` print? If you use '17' why, I wonder, is that not '8'? – g00se Sep 08 '22 at 10:05
  • Use jEnv https://www.jenv.be/ – tgdavies Sep 08 '22 at 10:18
  • @g00se It prints "no such file or directory: usr/libexec/java_home". So, any idea to fix the problem? – March Sep 08 '22 at 11:05
  • @March Thanks March, but at a beginner, I want to use native solutions using zsh. Any idea? – March Sep 08 '22 at 11:07
  • There should be a space before the version I think: ```/usr/libexec/java_home -v 1.8``` And did you omit the starting forward slash? – g00se Sep 08 '22 at 11:12
  • When I retry by `usr/libexec/java_home -v 1.8` then I get `/Library/Java/JavaVirtualMachines/adoptopenjdk-8.jdk/Contents/Home`. On the other hand, I also tried to change this line in .zshenv file from `export PATH=/usr/local/opt/openjdk@8/bin:$PATH` to `export PATH=/Library/Java/JavaVirtualMachines/adoptopenjdk-8.jdk/Contents/Home:$PATH`. Is that true? – March Sep 08 '22 at 11:18
  • And do I restart the Mac after each update on `.zshenv` file? – March Sep 08 '22 at 11:19
  • Use sdkman and its auto-env feature. – Mark Rotteveel Sep 08 '22 at 11:24
  • *Is that true?* You mean is that *right*? I would look to be, yes. But if there are already Mac tools for managing this kind of stuff, it would be worth looking at using them – g00se Sep 08 '22 at 12:20

0 Answers0