Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SET JAVA_HOME to reflect jenv java version

I just started to use jenv, I followed a blog post that was explaining how to use jenv and setup multiple java version on MacOSX. But the problem now I am running into is setting up the JAVA_HOME. As I switch java environment using jenv I want to make sure JAVA_HOME on my bash_profile also changes accordingly.

How do i do that?

I have following on my ~/.bash_profile

if which jenv > /dev/null; then eval "$(jenv init -)"; fi
like image 927
add-semi-colons Avatar asked Feb 19 '15 19:02

add-semi-colons


People also ask

How do I add Java 11 to JENV?

Use jenv add to inform jenv where your Java environment is located. jenv does not, by itself, install Java. For example, on macOS, use brew to install the latest Java (OpenJDK 11) followed by the appropriate jenv add PATH_TO_JVM_HOME command to recognize it.

How do you JAVA_HOME is set to the location of your JDK?

To set JAVA_HOME, do the following: Right click My Computer and select Properties. On the Advanced tab, select Environment Variables, and then edit JAVA_HOME to point to where the JDK software is located, for example, C:\Program Files\Java\jdk1.

Is JAVA_HOME set automatically?

If your JDK installation didn't set JAVA_HOME automatically, you can always open the Windows environment variable editor and set it yourself. This is the easiest way to manually set JAVA_HOME in Windows 7, 8 and 10.


3 Answers

Try the "export" plugin:

jenv enable-plugin export

You can check the Export plugin section in Readme.md at the jEnv Github repo (https://github.com/gcuisinier/jenv)

like image 98
kimbaudi Avatar answered Oct 16 '22 23:10

kimbaudi


For me, enabling the export plugin like kimbaudi didn't work. Adding the following code to .bash_profile (or .bashrc, .zprofile or .zshrc depending on what shell you use) did the job for me:

if which jenv > /dev/null; then eval "$(jenv init -)"; fi

This was in the troubleshooting page, but they seemed to state it was in the instructions guide, which it wasn't.

like image 45
Ben Butterworth Avatar answered Oct 16 '22 23:10

Ben Butterworth


You can add these alias in your ~/.bash_profile file and easily switch between different JAVA versions.

export JAVA_8_HOME=$(/usr/libexec/java_home -v1.8)
export JAVA_11_HOME=$(/usr/libexec/java_home -v11)

alias java8='export JAVA_HOME=$JAVA_8_HOME'
alias java11='export JAVA_HOME=$JAVA_11_HOME’
  • To check the available java versions in your system use

    jenv versions
    
    jenv version
    
  • You can even set particular version for single project by using following command in private terminal.

    jenv local 11.0
    

# Seconds option :-

Download adopt jdk from your choise from file -> project structure -> project sdk and then add following function in your .zshrc to switch between different version as per requirement.

[Attaching screenshot for reference:] :-

enter image description here

Switch between different JDK versions

Add the below function in your ~/.bashrc or ~/.zshrc

jdk() {
        version=$1
        export JAVA_HOME=$(/usr/libexec/java_home -v"$version");
        java -version
 }
  • Source the profile and you can change the version like below:

jdk 1.8 jdk 9 jdk 11 jdk 13

like image 5
Vrushali Raut Avatar answered Oct 16 '22 23:10

Vrushali Raut