Java installed via SDKMAN does not work with Bash Terminal

181 views Asked by At

I'm a Mac user and I've installed Java 11 via SDKMAN. I have no problem running and compiling java code in both VSCode and IntelliJ using zsh terminal, but when I use bash (my preference), I get the following error message.

"The operation couldn’t be completed. Unable to locate a Java Runtime.Please visit http://www.java.com for information on installing Java."

I have the following configuration in my bash_profile

# make .bash_profile same as .bashrc
if [ -f "${HOME}/.bashrc" ]; then source "${HOME}/.bashrc" fi export JAVA_HOME=$(/usr/libexec/java_home)
THIS MUST BE AT THE END OF THE FILE FOR SDKMAN TO WORK!!!
export SDKMAN_DIR="$HOME/.sdkman" [[ -s "$HOME/.sdkman/bin/sdkman-init.sh" ]] && source "$HOME/.sdkman/bin/sdkman-init.sh"

Is there any other configuration I need to get java to work using bash?

One more note is that this issue is only present in the IDE terminals that I'm using. I can use the Macbook terminal with bash and have no problems. I've also made sure that both IDEs have the appropriate JDK settings/point to the SDKMan "current" version of Java.

Any help would be much appreciated.

I've configured Java in the IDEs that I'm using, but this doesn't help the issue. Also, to be clear, java works, and I can compile code using the zsh terminal. I just get this error when using the bash terminal in these IDEs

1

There are 1 answers

1
Maciej Iwan On

Hmm, I don't know if this might be the issue with config, but you're missing the ';' before fi (also check if export in same line as if dont need seperator or new line)

Before:

if [ -f "${HOME}/.bashrc" ]; then source "${HOME}/.bashrc" fi

After:

if [ -f "${HOME}/.bashrc" ]; then source "${HOME}/.bashrc"; fi

I have extra questions:

  1. Is the path JAVA_HOME=$(/usr/libexec/java_home) really the directory where java is located? Why you need this variable? (if you need it, please put it to new line). I use ubuntu, so it could be diffrent but my machine print /home/[username]/.sdkman/candidates/java/current when echo $JAVA_HOME

  2. Are you sure the fragment with sdk is at the end of the file?

Remember that after updating .bashrc you need to reset temrinal.