Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

SDKMAN: JAVA_HOME not set and cannot find javac to deduce location, please set JAVA_HOME #431

Closed
alu0100207385 opened this issue Jul 10, 2016 · 26 comments

Comments

@alu0100207385
Copy link

Hi all,

I'm using sdkman to install groovy. I've followed install instructions from http://sdkman.io/install.html
When I execute "/home/aaron/.sdkman/bin/sdkman-init.sh" I got:

SDKMAN: JAVA_HOME not set and cannot find javac to deduce location, please set JAVA_HOME

Any ideas? Thanks!

I'm using Ubuntu 14.04LTS (64)

Cheers

@alu0100207385
Copy link
Author

Ups, I haven't got defined the variable. I have not said anything.

@marc0der
Copy link
Member

Also remember, do not execute the sdkman-init.sh script but source it.

@YoruNoTori
Copy link

I know this issue is closed but it would be good to specify some sort of solutions

This could happen to anyone who´s starting to use gradle or any other project, declaring a environment variable is something common to do in console.

Just be sure to install GIT BASH on your machine so you can use UNIX commands on your windows machine

then you just write

$export JAVA_HOME="/your_java_sdk_directory"

thats it

@szcc
Copy link

szcc commented Jan 30, 2018

I add JAVA_HOME to my java.config file under JAVA_CONF="/etc/java/java.conf"
JAVA_HOME="/usr/lib/jvm/java-1.8.0-openjdk-1.8.0.141-1.b16.el7_3.x86_64"
but when I run my grails "run-app" I got error "/usr/lib/jvm/java-1.8.0-openjdk-1.8.0.141-1.b16.el7_3.x86_64" is not a directory.
I try "update-alternatives --display java
and it showed me the directory /usr/lib/jvm/java-1.8.0-openjdk-1.8.0.141-1.b16.el7_3.x86_64/bin/java

Where is my javac???

@marc0der
Copy link
Member

@szcc try installing Java with SDKMAN. SDKMAN will dynamically set the JAVA_HOME environment variable for you and also allow you to switch between version just like all the other candidates.

@YoruNoTori sorry for not seeing and replying your message. SDKMAN can also be used to install and manage Java, so no need to export JAVA_HOME. All it takes is sdk install java and the rest is taken care of on your behalf.

@szcc
Copy link

szcc commented Jan 30, 2018

Thanks for answering my question. I installed java by "sdk install java" yesterday but the JAVA_HOME did not setup by sdkman. when I try to run "grails run-app" I got error java home not found. Then I edited java.config file under JAVA_CONF="/etc/java/java.conf" and JAVA_HOME="/usr/lib/jvm/java-1.8.0-openjdk-1.8.0.141-1.b16.el7_3.x86_64".
rerun my grails "run-app" I got "not a directory" error.
Now I have to work around it. install sdk 1.8 and set up java home to point it. The idea to use sdkman is manage grails-groovy and java together.....

@marc0der
Copy link
Member

marc0der commented Jan 31, 2018 via email

@szcc
Copy link

szcc commented Jan 31, 2018

thank you. will do

@vherasme
Copy link

Hi. I am facing this issue when trying to setup a build system with sublime test. I am using SDKMAN 5.7.3+337

@fdchiu
Copy link

fdchiu commented Oct 4, 2019

I had exactly the same issue. After install JDK 1.7, JAVA_HOME is still pointing to 1.8 which I installed manually through oracle's .dmg file on Mac. sdkman seems not doing what it is claiming to do. It just installed a version of java to: .sdkman/candidates/java directory, but the OS is still pointing to 1.8 which is located at /Library/Java/VirualMachines .

@minhtran83
Copy link

@fdchiu you can easily set JAVA_HOME like below

export JAVA_HOME=${SDKMAN_CANDIDATES_DIR}/java/${CURRENT}

@marc0der
Copy link
Member

@minhtran83 SDKMAN is already responsible for setting the JAVA_HOME, so you shouldn't be doing it yourself. The problem here is most probably to do with the type of shell being used (login vs interactive), which can lead to inconsistencies in shell state.

@clehene
Copy link

clehene commented Jul 10, 2020

@marc0der it is?

(⎈ |dev-1:default)➜  ~ echo $JAVA_HOME

I get an empty result in zsh after removing the explicit setting of JAVA_HOME
Is there any documnetation on how sdkman does this, so we can rely on less magical stuff?

@marc0der
Copy link
Member

@clehene there really isn't any magic here. You can simply use a text editor to read what sdkman-init.sh does when it is sourced.

As stated in the previous comment, most likely you are experiencing this due to a login shell being used, as opposed to an interactive shell.

Also worth noting that most users aren't experiencing what you are, so this is most probably due to an issue in your own environment.

@joshuapinter
Copy link

@minhtran83 SDKMAN is already responsible for setting the JAVA_HOME, so you shouldn't be doing it yourself.

This is bang on. Thanks for clarifying that. I didn't know this and we were unnecessarily setting JAVA_HOME on all of our machines.

SDKMAN FTW!!

@yangzhongru
Copy link

You just need to set export JAVA_HOME=/Users/{user}/.sdkman/candidates/java/current

@jmagana2000
Copy link

This worked for me.
export JAVA_HOME=${SDKMAN_DIR}/candidates/java/current

@jaymoid
Copy link

jaymoid commented Aug 23, 2021

As mentioned above, don't manually set the JAVA_HOME, sdk man does this for you when you specify the java version to use:
sdk use java java_identifier_goes_here

for example:

$ sdk use java 11.0.11.hs-adpt
Setting java version 11.0.11.hs-adpt as default.

Using java version 11.0.11.hs-adpt in this shell.
$ env | grep JAVA_HOME
JAVA_HOME=/home/james/.sdkman/candidates/java/11.0.11.hs-adpt

@leandrotapia
Copy link

In my case, after executing sdk install java <some_version> it didn't automatically create the JAVA_HOME (env | grep JAVA_HOME didn't show anything). But executing sdk use java <some_version> did the trick. 🙌
I couldn't figure out what the problem was, so maybe some local configuration made "noise". 🤷‍♂️

What I had it was java and maven working ok, both were installed with brew.
Then I uninstalled java (with brew) and (of course) maven couldn't find the JAVA_HOME.
Lastly, I installed java with sdkman, and then it happened what I said in the previous paragraph above.

@mckenzm
Copy link

mckenzm commented Jul 30, 2023

It's 2023 and this still happens. No JAVA HOME.
Clean install of SDKMAN
Installs Grails OK
sdk install java 8.0.382-zulu
(no other java installed, so I would not expect update-alternatives issues. It should set this to the default and do sdk use automatically?

@mjburghoffer
Copy link

mjburghoffer commented Sep 20, 2023

Yeah the issue is you have to do sdk use java <exact-version-number>, and it will set JAVA_HOME for you. Sadly, there is no sdk use java or sdk use java current or even sdk use java "$(sdk current java)".

Here is the line of bash that I found to actually work, since this tool obviously doesn't want to be too friendly to use out-of-the-box:

sdk use java "$(sdk current java | tail -1 | awk -F'. ' ' { print $NF } ')" > /dev/null 2>&1

@NoPhaseNoKill
Copy link

NoPhaseNoKill commented Nov 25, 2023

@clehene there really isn't any magic here. You can simply use a text editor to read what sdkman-init.sh does when it is sourced.

As stated in the previous comment, most likely you are experiencing this due to a login shell being used, as opposed to an interactive shell.

Also worth noting that most users aren't experiencing what you are, so this is most probably due to an issue in your own environment.

This is contrary to what I'm seeing right now when facing the issue.

Taken from here: https://unix.stackexchange.com/questions/26676/how-to-check-if-a-shell-is-login-interactive-batch (261 upvotes, so the assumption I'm making here is that this how to 'properly' check it).

Running both commands gives me:

$ [[ $- == *i* ]] && echo 'Interactive' || echo 'Not interactive'
Interactive

$ shopt -q login_shell && echo 'Login shell' || echo 'Not login shell'
Not login shell

My .bashrc file contains the below right at the end of it:

#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"

In the interest of actually providing some feedback and not just complaining (otherwise we'll never get a resolution to these types of things), I did a little more digging, because I thought it was strange that I was sometimes able to use the command 'java --version' completely fine, and other times it wasn't.

Sometimes I noticed it would come back with:

Error: Could not create the Java Virtual Machine.
Error: A fatal exception has occurred. Program will exit.")
  1. When attempting to run this manually, aka: "$HOME/.sdkman/bin/sdkman-init.sh" <----- bash, I got error:
/$MY_HOME/.sdkman/bin/sdkman-init.sh: Permission denied 

Note: '$MY_HOME' is literally a drop in replacement that I've chosen to exclude for this purpose, but is the correct location for what I would expect

  1. Checking the file permissions confirmed that this was the expected message given what they were, aka:
-rw-r--r--
  1. So I then ran:
chmod +x $MY_HOME/.sdkman/bin/sdkman-init.sh
  1. Checked for my java version inside of the same shell ("java --version"), which once again returned the expected error:
$java --version
Unrecognized option: --version
Error: Could not create the Java Virtual Machine.
Error: A fatal exception has occurred. Program will exit."
  1. Refreshed my .bashrc file using: "source ~/.bashrc", in case it would in any way shape or form hold any reference to the old permissions

  2. Ran "sdk use java 17.0.9-zulu", which returned

Using java version 17.0.9-zulu in this shell.
  1. Changed it to my default using: "sdk default java 17.0.9-zulu" , which returned

setting java 17.0.9-zulu as the default version for all shells.

  1. Ran "java --version" again, to confirm it was set. It was, as it returns:
openjdk 17.0.9 2023-10-17 LTS
OpenJDK Runtime Environment Zulu17.46+19-CA (build 17.0.9+8-LTS)
OpenJDK 64-Bit Server VM Zulu17.46+19-CA (build 17.0.9+8-LTS, mixed mode, sharing)
  1. Opened a completely new terminal (the previous one was literally the only one open), ran "java --version" and voila - it finally works with:
openjdk 17.0.9 2023-10-17 LTS
OpenJDK Runtime Environment Zulu17.46+19-CA (build 17.0.9+8-LTS)
OpenJDK 64-Bit Server VM Zulu17.46+19-CA (build 17.0.9+8-LTS, mixed mode, sharing)
  1. Closed down all shells, re-opened one, "java --version" again, same thing -> still works.

  2. Opened secondary shell, tried again, still works.

  3. In primary shell, used "sdk default java 11.0.21-zulu" -> returned

setting java 11.0.21-zulu as the default version for all shells."
  1. Went back to secondary shell, did "java --version" again, and voila, it's now working (when previously it was not):
openjdk 11.0.21 2023-10-17 LTS
OpenJDK Runtime Environment Zulu11.68+17-CA (build 11.0.21+9-LTS)
OpenJDK 64-Bit Server VM Zulu11.68+17-CA (build 11.0.21+9-LTS, mixed mode)
  1. Closed shells again, re-opened, did one more "java --version" -> got expected version 11
openjdk 11.0.21 2023-10-17 LTS
OpenJDK Runtime Environment Zulu11.68+17-CA (build 11.0.21+9-LTS)
OpenJDK 64-Bit Server VM Zulu11.68+17-CA (build 11.0.21+9-LTS, mixed mode)

My follow up questions to @marc0der:

  1. When searching for chmod in the sdkman-init.sh file, it returns no results (aka: "E486: Pattern not found: chmod")
  2. Because this init script is running inside of .bashrc, do you think it's reasonable to firstly check if the file has executable permissions, and warn/error if it doesn't, and explain how to do this? It may also need some cache/lock/async if you do choose to do it, because it's probs not worth checking each and every time (particularly when it's inside of .bashrc)
  3. The webpage also has no mention of needing to set the file up with executable permissions
  4. And neither does the running: "curl -s "https://get.sdkman.io" | bash" from memory (but could be wrong on this point)

tldr; for anyone stumbling across this:

  1. ensure you've got relevant executable permissions on the sdkman-init.sh file
  2. fix them with something similar to (don't use this blindly, please know what it does first, as you may want to achieve the same thing via other means)
 chmod +x $MY_HOME/.sdkman/bin/sdkman-init.sh
  1. Refresh/restart shells
  2. Using the latest version of sdkman, you should be able to then set your default with something like this, and it should now work:
sdk default java 17.0.9-zulu

edit: formatting
edit2: PS: Totally forgot to thank you for the effort you've put into this script/the project. Absolutely love what it does :)

@bouguern
Copy link

export JAVA_HOME=${SDKMAN_DIR}/candidates/java/current

This is worked for me

@straiforos
Copy link

I found after installing Java 17 Correto, I could not run mvn. the use command solved this for me. sdk use java 17.0.11-amzn set my JAVA_HOME path. Hope this helps someone else.

@pavinduLakshan
Copy link

In my case, after executing sdk install java <some_version> it didn't automatically create the JAVA_HOME (env | grep JAVA_HOME didn't show anything). But executing sdk use java <some_version> did the trick. 🙌 I couldn't figure out what the problem was, so maybe some local configuration made "noise". 🤷‍♂️

What I had it was java and maven working ok, both were installed with brew. Then I uninstalled java (with brew) and (of course) maven couldn't find the JAVA_HOME. Lastly, I installed java with sdkman, and then it happened what I said in the previous paragraph above.

Thanks. saved my day!

@Jesper-Hustad
Copy link

In my case, after executing sdk install java <some_version> it didn't automatically create the JAVA_HOME (env | grep JAVA_HOME didn't show anything). But executing sdk use java <some_version> did the trick. 🙌 I couldn't figure out what the problem was, so maybe some local configuration made "noise". 🤷‍♂️

What I had it was java and maven working ok, both were installed with brew. Then I uninstalled java (with brew) and (of course) maven couldn't find the JAVA_HOME. Lastly, I installed java with sdkman, and then it happened what I said in the previous paragraph above.

This worked for me

# will output version
sdk current

sdk use [version here]

# test if it worked
echo $JAVA_HOME

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests