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 not exported properly #188

Closed
wkhatch opened this issue Feb 14, 2018 · 16 comments · Fixed by #289
Closed

JAVA_HOME not exported properly #188

wkhatch opened this issue Feb 14, 2018 · 16 comments · Fixed by #289

Comments

@wkhatch
Copy link

wkhatch commented Feb 14, 2018

After installing, and adding jdk's 8 and 9, then setting jenv global to 1.8, I get expected results when issuing jenv info java. java -version also outputs expected results. However, other tools that rely on JAVA_HOME are failing, and incorrectly referencing the wrong java version's home. I don't have any other variable exports anywhere. echo $JAVA_HOME returns a blank line.

@gcuisinier
Copy link
Collaborator

gcuisinier commented Feb 18, 2018

Do you have enabled the export plugin?

jenv enable-plugin export (and restart the shell just after)

@SButterfly
Copy link

I have the same problem.
I've tried 'enable-plugin' command, but..

➜  ~ jenv enable-plugin export
jenv: no such command `enable-plugin'
➜  ~ jenv --version
jenv 0.4.4

@SButterfly
Copy link

I found issue.
There were several problems in jenv doctor. After fixing them, 'enable-plugin' fixed the original problem.

@jillesvangurp
Copy link

This is definitely not working correctly.

$ jenv enable-plugin export
export plugin already enabled
$ jenv local 10
jillesvangurp@jilless-MacBook-Pro ~/git/inbot/inbot-eth-client[master]*
$ jenv doctor
[OK]	JAVA_HOME variable probably set by jenv PROMPT
[OK]	Java binaries in path are jenv shims
[OK]	Jenv is correctly loaded
jillesvangurp@jilless-MacBook-Pro ~/git/inbot/inbot-eth-client[master]*
$ echo $JAVA_HOME
/Users/jillesvangurp/.jenv/versions/1.8
jillesvangurp@jilless-MacBook-Pro ~/git/inbot/inbot-eth-client[master]*
$ jenv version
10 (set by /Users/jillesvangurp/git/inbot/inbot-eth-client/.java-version)

So clearly not doing what it is supposed to and jenv doctor is lying about it as well. Latest jenv installed via home brew.

I have this in my .bash_profile:

# brew install jenv, install some jdks and make sure jenv knows where they are, and run this once to get jenv to export JAVA_HOME:
# jenv enable-plugin export
export PATH="$HOME/.jenv/bin:$PATH"
eval "$(jenv init -)"

The reason this project has a lot of issues related to JAVA_HOME being reported is that this variable is key to pretty much any custom shell script to launch e.g. maven, gradle, tomcat, jboss, etc. The expected behavior here is that jenv always manages JAVA_HOME. IMHO the plugin should not be needed. Any time you have jenv pointing to one thing and JAVA_HOME to another is a problem that is going to surprise/confuse users.

So why not fix this properly? I know there are work arounds but they sort of defeat the point of having jenv. Is there any good technical reason for not setting this variable always?

@ronin00
Copy link

ronin00 commented Sep 14, 2018

@jillesvangurp
Re the mismatch (1.8 vs 10) in the output you gave ...

Most likely cause is that you had jenv set to 1.8 when the shell was started which set the JAVA_HOME var. But subsequently you changed the jenv version to 10 but you didn't restart the shell.

Since shell wasn't restarted the JAVA_HOME remains initialized to the old 1.8 while jenv itself reports its current value 10 (extracted from the global/local .version files).

From what I can deduce and the brief mention in the README docs: JAVA_HOME is set by the 'export' plugin at shell startup only. So behaviour is as designed and not a bug per say though I can't fathom why it's designed that way.

$ echo $JAVA_HOME
/Users/jillesvangurp/.jenv/versions/1.8

jillesvangurp@jilless-MacBook-Pro ~/git/inbot/inbot-eth-client[master]*
$ jenv version
10 (set by /Users/jillesvangurp/git/inbot/inbot-eth-client/.java-version)

Hope that helps.

@epferrari
Copy link

This might help someone. While changing versions on the fly with jenv version <version> throws the JAVA_HOME envar out of sync, the export plugin does work when switching between directories with a .java-version file present.

In .{bash,zsh,local,whatever}rc file:

eval "$(jenv init -)"
eval "$(jenv enable-plugin export)"

$ cd ~/
$ echo $JAVA_HOME
/Users/<user>/.jenv/versions/system
$ cd ./projectWithJavaVersionFile
$ echo $JAVA_HOME
/Users/<user>/.jenv/versions/<version defined in .java-home>

@gcuisinier
Copy link
Collaborator

Thank @epferrari for your feedback.

We will check how to export directly the JAVA_HOME directly on jenv local|global call.

@zeratul021
Copy link

Also having this issue while using jenv shell inside a script (0.5.0)
jenv info provides correct value for JAVA_HOME but the actual echo contains wrong (global) path.

@juman123
Copy link

I'm new to jenv and ran into this problem.

The reason this project has a lot of issues related to JAVA_HOME being reported is that this variable is key to pretty much any custom shell script to launch e.g. maven, gradle, tomcat, jboss, etc. The expected behavior here is that jenv always manages JAVA_HOME. IMHO the plugin should not be needed. Any time you have jenv pointing to one thing and JAVA_HOME to another is a problem that is going to surprise/confuse users.

Exactly. After installing jenv, I thought JAVA_HOME would always be either whatever is set in your shell profile, or whatever jenv would automatically set it to, based on .java_version files and what not.

I don't understand why an "export plugin" needs to be enabled separately. How does jenv work at all, if not by exporting JAVA_HOME?

@bric3
Copy link
Contributor

bric3 commented May 24, 2019

This appear to have done the trick for me #188 (comment)

@danielporto
Copy link

There is still a problem with fish shell.
to get JAVA_HOME updated, I had to use the extension "bass" and run the following command whenever I switch dir to update JAVA_HOME according to the current configuration of jenv:

bass 'eval "$(jenv init -)"; eval "$(jenv enable-plugin export)"'

note that the command must be inline. split into two lines and it wont work.
how can I make this work automatically?

@jonatan-ivanov
Copy link

Same thing for me:
(also, SDKMan is installed)

❯ jenv global 13.0

❯ jenv doctor
[OK]    JAVA_HOME variable probably set by jenv PROMPT
[OK]    Java binaries in path are jenv shims
[OK]    Jenv is correctly loaded

❯ jenv info java
Jenv will exec : /Users/xyz/.jenv/versions/13.0/bin/java
Exported variables :
  JAVA_HOME=/Users/xyz/.jenv/versions/13.0

❯ echo $JAVA_HOME
/Users/xyz/.jenv/versions/1.8

❯ 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)

❯ $JAVA_HOME/bin/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)

❯ jenv enable-plugin export
export plugin already enabled

@easye
Copy link

easye commented Mar 9, 2020

I had problems with the enable-plugin command not working.

After a little bit of tracing, I noticed that JENV_ROOT was not getting set correctly.

Therefore, the following initialization sequence under bash now works better for me:

export JENV_ROOT=$HOME/.jenv                                                    
export PATH=$JENV_ROOT/bin:$PATH                                                                                                                                
eval "$(jenv init -)"                                                           
eval "$(jenv enable-plugin export)"   

@simonrouse9461
Copy link

simonrouse9461 commented Mar 30, 2020

There is still a problem with fish shell.
to get JAVA_HOME updated, I had to use the extension "bass" and run the following command whenever I switch dir to update JAVA_HOME according to the current configuration of jenv:

bass 'eval "$(jenv init -)"; eval "$(jenv enable-plugin export)"'

note that the command must be inline. split into two lines and it wont work.
how can I make this work automatically?

I found the problem with fish shell is that the jenv export plugin only provides hooks for bash and zsh. Fish hook is not implemented at all. I have hardcoded the following script in config.fish to enable export hook in fish.

# load jenv and enable export hook
status --is-interactive; and source (jenv init - | psub)
function __jenv_export_hook --on-event fish_prompt
    set -gx JAVA_HOME (jenv javahome)
    set -gx JENV_FORCEJAVAHOME true

    if test -e "$JAVA_HOME/bin/javac"
        set -gx JDK_HOME "$JAVA_HOME"
        set -gx JENV_FORCEJDKHOME true
    end
end

herder added a commit to herder/jenv that referenced this issue Apr 19, 2020
Ensures that JAVA_HOME is set properly and that prompts get correct info on current java version.

From jenv#188 (comment)

Fixes jenv#188
herder added a commit to herder/jenv that referenced this issue Apr 19, 2020
Ensures that JAVA_HOME is set properly and that prompts get correct info on current java version.

From jenv#188 (comment)

Fixes jenv#188
@unhuman
Copy link

unhuman commented May 6, 2020

Trying to switch over from bash -> zsh.

When I run: eval "$(jenv init -)"
in zsh, I get the following: zsh: bad pattern: ^[[0mexport

Any ideas here? Thanks!

@skabashnyuk
Copy link

@unhuman try jenv doctor for it was complaining that plugins were linked to the wrong version. After I complete instructions JAVA_HOME exported correctly.

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

Successfully merging a pull request may close this issue.