{"id":681,"date":"2019-07-06T13:41:38","date_gmt":"2019-07-06T13:41:38","guid":{"rendered":"http:\/\/kabiliravi.com\/?page_id=681"},"modified":"2019-07-21T13:48:25","modified_gmt":"2019-07-21T13:48:25","slug":"switching-jdks-using-jenv","status":"publish","type":"page","link":"http:\/\/kabiliravi.com\/index.php\/software\/programming\/my-java-tutorial\/install-java\/switching-jdks-using-jenv\/","title":{"rendered":"Switching JDKs using jEnv"},"content":{"rendered":"\n<p>If you already installed latest version of java such as 12 and you check the version of java, it still points to java 12 and not 11. However you can switch between different installed JDKs by setting JAVA_HOME environment variable and having the $JAVA_HOME\/bin in $PATH environment variable. <\/p>\n\n\n\n<p>You can also use a command line tool called <strong><a href=\"https:\/\/www.jenv.be\/\">jEnv<\/a><\/strong> to switch between different version of jdks. <\/p>\n\n\n\n<blockquote class=\"wp-block-quote is-layout-flow wp-block-quote-is-layout-flow\"><p>What&#8217;s jEnv ?<br>jEnv is a command line tool to help you forget how to set the JAVA_HOME environment variable<\/p><cite><a href=\"https:\/\/www.jenv.be\/\">https:\/\/www.jenv.be\/<\/a><\/cite><\/blockquote>\n\n\n\n<h2 class=\"wp-block-heading\">Mac OS X via&nbsp;<a href=\"http:\/\/brew.sh\/\">Homebrew<\/a><\/h2>\n\n\n\n<p>To install jEnv on mac using brew use following command:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">$ brew install jenv\nUpdated 2 taps (homebrew\/cask and caskroom\/cask).\n No changes to formulae.\n==&gt; Downloading https:\/\/github.com\/jenv\/jenv\/archive\/0.5.2.tar.gz\n==&gt; Downloading from https:\/\/codeload.github.com\/jenv\/jenv\/tar.gz\/0.5.2\n ################################################################## 100.0%\n==&gt; Caveats\n To activate jenv, add the following to your ~\/.zshrc:\n export PATH=\"$HOME\/.jenv\/bin:$PATH\"\n eval \"$(jenv init -)\"\n==&gt; Summary\n ?  \/usr\/local\/Cellar\/jenv\/0.5.2: 80 files, 70KB, built in 2 seconds<\/pre>\n\n\n\n<p>Referring to Caveats section in above installation logs for jEnv, you will see something like following notes:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">==&gt; Caveats\n To activate jenv, add the following to your ~\/.zshrc:\n export PATH=\"$HOME\/.jenv\/bin:$PATH\"\n eval \"$(jenv init -)\"<\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">Linux\/Unix\/OS X Installation by source code<\/h2>\n\n\n\n<blockquote class=\"wp-block-quote is-layout-flow wp-block-quote-is-layout-flow\"><p>First you need to install <strong>git<\/strong> on your Linux\/Unix\/OS X machine. Click <a href=\"http:\/\/kabiliravi.com\/index.php\/software\/miscellaneous\/working-with-git-command\/\">here<\/a> for more information on <a href=\"http:\/\/kabiliravi.com\/index.php\/software\/miscellaneous\/working-with-git-command\/\">Working with<\/a><strong><a href=\"http:\/\/kabiliravi.com\/index.php\/software\/miscellaneous\/working-with-git-command\/\"> git cli<\/a><\/strong><a href=\"http:\/\/kabiliravi.com\/index.php\/software\/miscellaneous\/working-with-git-command\/\"> tool<\/a>.<\/p><\/blockquote>\n\n\n\n<p>If you have <strong>git cli<\/strong> command installed, clone jEnd github repository using following following command into <strong>~\/.jenv<\/strong> folder<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">$ git clone https:\/\/github.com\/gcuisinier\/jenv.git ~\/.jenv<\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">Post jEnv Configurations<\/h2>\n\n\n\n<p>If you are using bash or zsh you need to go and add following lines into their profile ~\/.bash_profile or ~\/.zshrc accordingly<\/p>\n\n\n\n<h4 class=\"wp-block-heading\">Bash<\/h4>\n\n\n\n<pre class=\"wp-block-preformatted\">$ echo 'export PATH=\"$HOME\/.jenv\/bin:$PATH\"' &gt;&gt; ~\/.bash_profile \n$ echo 'eval \"$(jenv init -)\"' &gt;&gt; ~\/.bash_profile <\/pre>\n\n\n\n<h4 class=\"wp-block-heading\">Zsh<\/h4>\n\n\n\n<pre class=\"wp-block-preformatted\">$ echo 'export PATH=\"$HOME\/.jenv\/bin:$PATH\"' &gt;&gt; ~\/.zshrc \n$ echo 'eval \"$(jenv init -)\"' &gt;&gt; ~\/.zshrc<\/pre>\n\n\n\n<blockquote class=\"wp-block-quote is-layout-flow wp-block-quote-is-layout-flow\"><p>Exit the ssh session or logout the user session you are in and login again or you can simply reboot your OS.<\/p><\/blockquote>\n\n\n\n<h2 class=\"wp-block-heading\">Validating the Installation<\/h2>\n\n\n\n<p>If everything is good, you should be able to see <strong>jenv<\/strong> version and cli help by running just <strong>jenv<\/strong> command like this:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">$ jenv\n jenv 0.5.2-4-gd8ffd5b\n Usage: jenv \n []\n Some useful jenv commands are:\n    commands    List all available jenv commands\n    local       Set or show the local application-specific Java version\n    global      Set or show the global Java version\n    shell       Set or show the shell-specific Java version\n    rehash      Rehash jenv shims (run this after installing executables)\n    version     Show the current Java version and its origin\n    versions    List all Java versions available to jenv\n    which       Display the full path to an executable\n    whence      List all Java versions that contain the given executable\n    add         Add JDK into jenv. A alias name will be generated by parsing \"java -version\"\n See `jenv help \n' for information on a specific command.\n For full documentation, see: https:\/\/github.com\/hikage\/jenv#readme<\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">Adding JDK path to jEnv<\/h2>\n\n\n\n<p>Here is where you can find installed jdks in you Mojave macOS<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">$ ls \/Library\/Java\/JavaVirtualMachines\/\njdk1.8.0_181.jdk   jdk1.8.0_192.jdk   openjdk-11.0.2.jdk openjdk-12.0.1.jdk<\/pre>\n\n\n\n<p>And here are some example where you can find the installed JDK on your Linux machine. Following paths are in my Fedora 30<\/p>\n\n\n\n<p>Using <strong>find<\/strong> command you can search for installed <strong>openjdk<\/strong> for example like this.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">$ find \/ -name '<em>openjdk<\/em>'\n \/var\/lib\/alternatives\/jre_12_openjdk\n \/var\/lib\/alternatives\/jre_openjdk\n \/etc\/java\/java-12-openjdk\n \/etc\/java\/java-12-openjdk\/java-12-openjdk-12.0.1.12-1.rolling.fc30.x86_64\n \/etc\/alternatives\/jre_12_openjdk\n \/etc\/alternatives\/jre_openjdk\n \/usr\/lib\/jvm\/java-12-openjdk-12.0.1.12-1.rolling.fc30.x86_64\n \/usr\/lib\/jvm\/jre-12-openjdk\n \/usr\/lib\/jvm\/jre-openjdk\n \/usr\/lib\/jvm\/jre-12-openjdk-12.0.1.12-1.rolling.fc30.x86_64\n \/usr\/share\/man\/man1\/pack200-java-12-openjdk-12.0.1.12-1.rolling.fc30.x86_64.1.gz\n \/usr\/share\/man\/man1\/java-java-12-openjdk-12.0.1.12-1.rolling.fc30.x86_64.1.gz\n \/usr\/share\/man\/man1\/keytool-java-12-openjdk-12.0.1.12-1.rolling.fc30.x86_64.1.gz\n \/usr\/share\/man\/man1\/jjs-java-12-openjdk-12.0.1.12-1.rolling.fc30.x86_64.1.gz\n \/usr\/share\/man\/man1\/rmid-java-12-openjdk-12.0.1.12-1.rolling.fc30.x86_64.1.gz\n \/usr\/share\/man\/man1\/unpack200-java-12-openjdk-12.0.1.12-1.rolling.fc30.x86_64.1.gz\n \/usr\/share\/man\/man1\/rmiregistry-java-12-openjdk-12.0.1.12-1.rolling.fc30.x86_64.1.gz\n \/usr\/share\/icons\/hicolor\/32x32\/apps\/java-12-openjdk.png\n \/usr\/share\/icons\/hicolor\/48x48\/apps\/java-12-openjdk.png\n \/usr\/share\/icons\/hicolor\/16x16\/apps\/java-12-openjdk.png\n \/usr\/share\/icons\/hicolor\/24x24\/apps\/java-12-openjdk.png<\/pre>\n\n\n\n<p>You should find it somewhere inside <strong>\/usr<\/strong> not \/etc or \/var folders. Also make sure you don&#8217;t want JRE you are looking for JDK path. In this example, here is the location, look it up in above find result.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">\/usr\/lib\/jvm\/java-12-openjdk-12.0.1.12-1.rolling.fc30.x86_64<\/pre>\n\n\n\n<p>Using <strong>jenv add<\/strong> you can add jdk path to jenv environment<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">$ jenv add \/Library\/Java\/JavaVirtualMachines\/openjdk-12.0.1.jdk\/Contents\/Home\n$ jenv add \/Library\/Java\/JavaVirtualMachines\/openjdk-11.0.2.jdk\/Contents\/Home\n$ jenv add \/Library\/Java\/JavaVirtualMachines\/jdk1.8.0_192.jdk\/Contents\/Home<\/pre>\n\n\n\n<p>Using<strong> jenv versions<\/strong> you can see the list of java versions installed on our mac<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">$ jenv versions\n system (set by \/Users\/youruser\/.jenv\/version)\n 1.8\n 1.8.0.192\n 11.0\n 11.0.2\n 12.0\n 12.0.1\n openjdk64-11.0.2\n openjdk64-12.0.1\n oracle64-1.8.0.192 <\/pre>\n\n\n\n<h4 class=\"wp-block-heading\">Configure global version<\/h4>\n\n\n\n<p>You can set your jdk <strong>globally<\/strong> using following command. <\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">$ jenv global 11.0<\/pre>\n\n\n\n<p>Make sure you open another shell after executing above command and check the version of java<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">$ java -version\nopenjdk version \"11.0.2\" 2019-01-15\nOpenJDK Runtime Environment 18.9 (build 11.0.2+9)\nOpenJDK 64-Bit Server VM 18.9 (build 11.0.2+9, mixed mode)<\/pre>\n\n\n\n<h4 class=\"wp-block-heading\">Configure local version (per directory)<\/h4>\n\n\n\n<p>Or you can set jdk for the current folder you are in using following command<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">~\/java01$ jenv local 1.8<\/pre>\n\n\n\n<p>If you check java -version when you are inside <strong>java01<\/strong> that you set jdk using<strong> jenv local<\/strong>, you will see the java version is 1.8<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">~\/java01$ java -version\njava version \"1.8.0_192\"\nJava(TM) SE Runtime Environment (build 1.8.0_192-b12)\nJava HotSpot(TM) 64-Bit Server VM (build 25.192-b12, mixed mode)<\/pre>\n\n\n\n<p>And if you somewhere else it will be still version 11<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">~$ java -version                                                                                                                  openjdk version \"11.0.2\" 2019-01-15\nOpenJDK Runtime Environment 18.9 (build 11.0.2+9)\nOpenJDK 64-Bit Server VM 18.9 (build 11.0.2+9, mixed mode)<\/pre>\n\n\n\n<h4 class=\"wp-block-heading\">Configure shell instance version<\/h4>\n\n\n\n<p>However you can specify the java version for specific shell window you opened. That means if you go to another window or close the shell that you set jdk version using <strong>jenv shell<\/strong>, it will go back and show the jdk gobal version<\/p>\n\n\n\n<p>Here is the command to set shell instance version<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">$ jenv shell openjdk64-12.0.1<\/pre>\n\n\n\n<p>And the version will be 12<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">$ java -version\nopenjdk version \"12.0.1\" 2019-04-16\nOpenJDK Runtime Environment (build 12.0.1+12)\nOpenJDK 64-Bit Server VM (build 12.0.1+12, mixed mode, sharing)<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>If you already installed latest version of java such as 12 and you check the version of java, it still points to java 12 and not 11. However you can switch between different installed JDKs by setting JAVA_HOME environment variable and having the $JAVA_HOME\/bin in $PATH environment variable. You can also use a command line [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"parent":609,"menu_order":6,"comment_status":"open","ping_status":"closed","template":"","meta":{"ngg_post_thumbnail":0,"footnotes":""},"class_list":["post-681","page","type-page","status-publish","hentry"],"_links":{"self":[{"href":"http:\/\/kabiliravi.com\/index.php\/wp-json\/wp\/v2\/pages\/681","targetHints":{"allow":["GET"]}}],"collection":[{"href":"http:\/\/kabiliravi.com\/index.php\/wp-json\/wp\/v2\/pages"}],"about":[{"href":"http:\/\/kabiliravi.com\/index.php\/wp-json\/wp\/v2\/types\/page"}],"author":[{"embeddable":true,"href":"http:\/\/kabiliravi.com\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/kabiliravi.com\/index.php\/wp-json\/wp\/v2\/comments?post=681"}],"version-history":[{"count":11,"href":"http:\/\/kabiliravi.com\/index.php\/wp-json\/wp\/v2\/pages\/681\/revisions"}],"predecessor-version":[{"id":791,"href":"http:\/\/kabiliravi.com\/index.php\/wp-json\/wp\/v2\/pages\/681\/revisions\/791"}],"up":[{"embeddable":true,"href":"http:\/\/kabiliravi.com\/index.php\/wp-json\/wp\/v2\/pages\/609"}],"wp:attachment":[{"href":"http:\/\/kabiliravi.com\/index.php\/wp-json\/wp\/v2\/media?parent=681"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}