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

JAVA_HOME did not get updated #51

Closed
jonatan-ivanov opened this issue Feb 26, 2020 · 17 comments · Fixed by #76
Closed

JAVA_HOME did not get updated #51

jonatan-ivanov opened this issue Feb 26, 2020 · 17 comments · Fixed by #76

Comments

@jonatan-ivanov
Copy link
Contributor

If I switch between java versions, JAVA_HOME did not get updated.

❯ asdf current
java           adopt-openjdk-13.0.2+8 (set by /Users/jivanov/.tool-versions)

❯ java -version
openjdk version "13.0.2" 2020-01-14
OpenJDK Runtime Environment AdoptOpenJDK (build 13.0.2+8)
OpenJDK 64-Bit Server VM AdoptOpenJDK (build 13.0.2+8, mixed mode, sharing)

❯ echo $JAVA_HOME
/Users/jivanov/.asdf/installs/java/adopt-openjdk-13.0.2+8

❯ asdf global java adopt-openjdk-8u242-b08

❯ asdf current
java           adopt-openjdk-8u242-b08 (set by /Users/jivanov/.tool-versions)

❯ java -version
openjdk version "1.8.0_242"
OpenJDK Runtime Environment (AdoptOpenJDK)(build 1.8.0_242-b08)
OpenJDK 64-Bit Server VM (AdoptOpenJDK)(build 25.242-b08, mixed mode)

❯ echo $JAVA_HOME
/Users/jivanov/.asdf/installs/java/adopt-openjdk-13.0.2+8
@jonatan-ivanov jonatan-ivanov changed the title JAVA_HOME JAVA_HOME did not get updated Feb 26, 2020
@halcyon
Copy link
Owner

halcyon commented Feb 29, 2020

You'll need to restart your shell for JAVA_HOME to be updated.

@jonatan-ivanov
Copy link
Contributor Author

@halcyon Yes, that is the issue, I should not, right? :)

@joschi
Copy link
Contributor

joschi commented Mar 21, 2020

@jonatan-ivanov I think there's currently no hook in asdf which will invoke a plugin when a version was selected (asdf global <plugin> <version> or asdf local <plugin> <version>).

@jonatan-ivanov
Copy link
Contributor Author

@joschi I'm not very familiar with asdf but isn't exec-env for this? https://asdf-vm.com/#/plugins-create?id=binexec-env
The main documentation is not very verbose, this might help a little more: https://github.com/asdf-vm/asdf/blob/master/docs/core-manage-versions.md#shims
And here how it's used: https://github.com/asdf-vm/asdf/blob/master/lib/utils.bash#L527-L543

@joschi
Copy link
Contributor

joschi commented Mar 21, 2020

@jonatan-ivanov exec-env controls the environment when a shim is executed (for example $HOME/.asdf/shims/java) but it doesn't change the environment you're calling asdf or the shim from, which would be required to update $JAVA_HOME after changing the version with asdf shell, asdf local, or asdf global.

It's not that it couldn't be added to asdf, but this hook doesn't exist right now, so there's nothing asdf-java could do about it. It has to be added in asdf first.

Related issue: asdf-vm/asdf#384

@jonatan-ivanov
Copy link
Contributor Author

@joschi Thank you very much!

@rubencaro
Copy link

I live with this in my .bashrc as a workaround. It updates JAVA_HOME every time it updates the prompt:

function asdf_update_java_home {
  asdf current java 2>&1 > /dev/null
  if [[ "$?" -eq 0 ]]
  then
      export JAVA_HOME=$(asdf where java)
  fi
}

function prompt_command {
  __vte_prompt_command  # put here whatever previous PROMPT_COMMAND value
  asdf_update_java_home
}

export PROMPT_COMMAND=prompt_command

Maybe you could add this to the README while the hook is not there.

@donbeave
Copy link

donbeave commented Apr 8, 2020

Zsh users can add this to .zshrc:

# set JAVA_HOME on every change directory
function asdf_update_java_home {
  asdf current java 2>&1 > /dev/null
  if [[ "$?" -eq 0 ]]
  then
      export JAVA_HOME=$(asdf where java)
  fi
}

precmd() { asdf_update_java_home; }
# end set JAVA_HOME

Also, suggest to add it to the README.

@rubencaro
Copy link

@halcyon do you prefer a PR adding those to the readme? I could do it.

@joschi
Copy link
Contributor

joschi commented Apr 10, 2020

@rubencaro @donbeave Be aware that it can substantially slow down your shell when asdf is being called before every single command you're running in your shell.

@jthegedus
Copy link

@joschi is this something the asdf-direnv plugin can solve? Does it have hooks for this kind of functionality?

@rubencaro
Copy link

@rubencaro @donbeave Be aware that it can substantially slow down your shell when asdf is being called before every single command you're running in your shell.

I didn't notice any effect after months using it like that. You should take into account that this will call asdf only when the shell renders the prompt, so right before going idle. It will not slow down execution of every single command 😁.

Maybe it would be enough to add a warning for that specific usecase (i.e. if somebody needs to render the prompt many times per second ?).

@joschi
Copy link
Contributor

joschi commented Apr 10, 2020

@rubencaro You're right. I mixed up precmd and preexec. 👍

For reference: https://github.com/rothgar/mastering-zsh/blob/master/docs/config/hooks.md#hooks

@halcyon
Copy link
Owner

halcyon commented Apr 12, 2020

@rubencaro @donbeave @joschi @jonatan-ivanov - how would you feel about #76 ?

@thuandt
Copy link

thuandt commented Apr 12, 2020

@halcyon How about my solution? It worked with both system java and asdf provider java

asdf_update_java_home() {
  # shellcheck disable=SC2046
  JAVA_HOME=$(realpath $(dirname $(readlink -f $(asdf which java)))/../)
  export JAVA_HOME
}
autoload -U add-zsh-hook
add-zsh-hook precmd asdf_update_java_home

https://github.com/thuandt/zsh-config/blob/master/functions.sh#L3-L7
https://github.com/thuandt/zsh-config/blob/master/my-zsh-config.plugin.zsh#L103

@halcyon
Copy link
Owner

halcyon commented Apr 12, 2020

Thank you @thuandt
I've integrated your solution into PR #76

@donbeave
Copy link

@halcyon LGTM, I just tested zsh script, works perfectly.

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

Successfully merging a pull request may close this issue.

7 participants