{"id":62970,"date":"2022-03-16T09:02:55","date_gmt":"2022-03-16T09:02:55","guid":{"rendered":"https:\/\/www.cryptocabaret.com\/?p=62970"},"modified":"2022-03-16T09:02:55","modified_gmt":"2022-03-16T09:02:55","slug":"how-to-find-your-java-home-when-running-multiple-versions","status":"publish","type":"post","link":"https:\/\/www.cryptocabaret.com\/?p=62970","title":{"rendered":"How to find your Java Home when running multiple versions"},"content":{"rendered":"<p><span class=\"field field--name-title field--type-string field--label-hidden\">How to find your Java Home when running multiple versions<\/span><br \/>\n<span class=\"field field--name-uid field--type-entity-reference field--label-hidden\"><a title=\"View user profile.\" href=\"https:\/\/opensource.com\/users\/seth\" class=\"username\">Seth Kenlon<\/a><\/span><br \/>\n<span class=\"field field--name-created field--type-created field--label-hidden\">Wed, 03\/16\/2022 &#8211; 03:00<\/span><\/p>\n<div data-drupal-selector=\"rate-node-69651\" class=\"rate-widget-thumbs-up\">\n<div class=\"rate-thumbs-up-btn-up rate-thumbs-up-btn-up vote-pending\">Up<\/div>\n<div class=\"rate-score\"><a href=\"https:\/\/opensource.com\/user\/register?absolute=1\">Register<\/a> or <a href=\"https:\/\/opensource.com\/user\/login?current=\/rss.xml&amp;absolute=1\">Login<\/a> to like.<\/div>\n<\/div>\n<div class=\"clearfix text-formatted field field--name-body field--type-text-with-summary field--label-hidden field__item\">\n<p>When you install Java, it&#8217;s part of the installer&#8217;s job to tell your system where to find the right language when it&#8217;s needed. That&#8217;s why you&#8217;re able to run Java applications\u2014in fact, you may sometimes run Java applications without ever realizing what language they&#8217;ve been written in.<\/p>\n<p>Put simply, it just works, just as when you install Python to run a Python application, C to run an application written in C, and so on.<\/p>\n<p>But what happens when you install more than one version of Java on your system? In that case, you&#8217;ll have to take on the extra responsibility of understanding where each version&#8217;s <code>JAVA_HOME<\/code> is located.<\/p>\n<p>The easiest way to solve this issue is to use <a href=\"https:\/\/opensource.com\/article\/22\/3\/manage-java-versions-sdkman\">SDKMan<\/a>. However, if you don&#8217;t have SDKMan available, you need to do it manually.<\/p>\n<\/p>\n<div class=\"embedded-resource-list callout-float-right\">\n<div class=\"field field--name-title field--type-string field--label-hidden field__item\">More on Java<\/div>\n<div class=\"field field--name-links field--type-link field--label-hidden field__items\">\n<div class=\"field__item\"><a href=\"https:\/\/developers.redhat.com\/topics\/enterprise-java\/?intcmp=7013a000002Cxq6AAC\">What is enterprise Java programming?<\/a><\/div>\n<div class=\"field__item\"><a href=\"https:\/\/developers.redhat.com\/products\/openjdk\/overview?intcmp=7013a000002Cxq6AAC\">Red Hat build of OpenJDK<\/a><\/div>\n<div class=\"field__item\"><a href=\"https:\/\/opensource.com\/downloads\/java-cheat-sheet?intcmp=7013a000002Cxq6AAC\">Java cheat sheet<\/a><\/div>\n<div class=\"field__item\"><a href=\"https:\/\/www.redhat.com\/en\/services\/training\/do092-developing-cloud-native-applications-microservices-architectures?intcmp=7013a000002Cxq6AAC\">Free online course: Developing cloud-native applications with microservices arc\u2026<\/a><\/div>\n<div class=\"field__item\"><a href=\"https:\/\/opensource.com\/tags\/java?intcmp=7013a000002Cxq6AAC\">Fresh Java articles<\/a><\/div>\n<\/p><\/div>\n<\/p><\/div>\n<h2>Why you might want multiple versions of Java<\/h2>\n<p>There are a few reasons you might want more than one version of Java on your computer.<\/p>\n<ul>\n<li>\n<p>Programming languages progress at one rate, while an application might develop at a different rate. It&#8217;s possible for Java (the language) to increment its version number while your favorite application sticks with an older version. If you have two must-have applications, each of which uses a different version of Java, you may want to install both.<\/p>\n<\/li>\n<li>\n<p>Developers work on many different projects, some of which might use an old long-term support (LTS) version of Java while another uses the latest version.<\/p>\n<\/li>\n<li>\n<p>You might be on a system for which you don&#8217;t have permission to install Java at the systemwide level. You can, instead, run Java out of your home directory, but that sometimes means you have to point applications to that nonstandard install location.<\/p>\n<\/li>\n<\/ul>\n<h2>The JAVA_HOME variable<\/h2>\n<p>It&#8217;s perfectly acceptable to have more than one version of Java installed, but applications may not know where to find the version it needs.<\/p>\n<p>Normally, an application just uses whatever you have set as your default Java runtime. This is something you may not have set yourself. Depending on what Linux distribution you run and what method you&#8217;ve used to install Java, your system default could be the first version of Java you installed or the most recent one.<\/p>\n<p>Regardless of what your default is, if you try to launch an application and then receive an error about the version of Java you&#8217;re using, even though you know you&#8217;ve got the right version installed somewhere on your system, you can manually override the setting.<\/p>\n<p>Applications can derive the location of Java from your current session&#8217;s <dfn><a href=\"https:\/\/opensource.com\/article\/19\/8\/what-are-environment-variables\">environment variables<\/a><\/dfn>.<\/p>\n<p>First, take a look at what, if anything, <code>JAVA_HOME<\/code> is set to currently:<\/p>\n<pre>\n<span class=\"geshifilter\"><code class=\"bash geshifilter-bash\"><span class=\"co4\">$ <\/span><span class=\"kw3\">echo<\/span> <span class=\"re1\">$JAVA_HOME<\/span><\/code><\/span><\/pre>\n<p>If there&#8217;s output, then your computer is maintaining a <code>JAVA_HOME<\/code> variable. If there&#8217;s no output, then your computer doesn&#8217;t have that variable set. That doesn&#8217;t necessarily mean your computer has no Java home, it just means that nothing&#8217;s bothered to set it yet.<\/p>\n<h2>Find the right version<\/h2>\n<p>Before you can set <code>JAVA_HOME<\/code>, you need to know which Java you want to use. Which Java you need to use depends on the application you&#8217;re trying to run. If an application requires Java 8 to run,\u00a0you need to point it to the <code>bin<\/code> directory containing Java 8. If an application requires Java 17 to run, you need to point it to the <code>bin<\/code> directory containing Java 17.<\/p>\n<p>You can find your current default Java version with the <code>-version<\/code> option. Launch a terminal and type this command (omit the <code>$<\/code> command prompt):<\/p>\n<pre>\n<span class=\"geshifilter\"><code class=\"bash geshifilter-bash\"><span class=\"co4\">$ <\/span><span class=\"kw2\">java<\/span> <span class=\"re5\">-version<\/span><\/code><\/span><\/pre>\n<h2>Find the right location from a package manager<\/h2>\n<p>Now you need to know where the version of Java you want to use is located.<\/p>\n<p>If you installed Java through a package manager, you can query your package manager for a file list. This tells you what and where files were installed with that package.<\/p>\n<p>For example, on Fedora, CentOS, or Mageia, you can list all installed packages and <dfn><a href=\"https:\/\/opensource.com\/article\/21\/3\/grep-cheat-sheet\">grep<\/a><\/dfn> to see just packages containing the string <code>java<\/code>:<\/p>\n<pre>\n<span class=\"geshifilter\"><code class=\"bash geshifilter-bash\"><span class=\"co4\">$ <\/span>dnf list installed <span class=\"sy0\">|<\/span> <span class=\"kw2\">grep<\/span> <span class=\"kw2\">java<\/span><\/code><\/span><\/pre>\n<p>For even cleaner output, use <dfn><a href=\"https:\/\/opensource.com\/article\/19\/10\/intro-awk\">awk<\/a><\/dfn>:<\/p>\n<pre>\n<div class=\"geshifilter\"><div class=\"bash geshifilter-bash\">$ dnf list installed <span class=\"sy0\">|<\/span> <span class=\"kw2\">awk<\/span> <span class=\"re5\">--field-separator<\/span>=<span class=\"st_h\">' '<\/span> <span class=\"st_h\">'\/java\/{print $1;}'<\/span><br>\njava-1.8.0-openjdk.x86_64<br>\njava-1.8.0-openjdk-devel.x86_64<br>\njava-1.8.0-openjdk-headless.x86_64<br>\njava-<span class=\"nu0\">11<\/span>-openjdk.x86_64<br>\njava-<span class=\"nu0\">11<\/span>-openjdk-devel.x86_64<br>\njava-<span class=\"nu0\">11<\/span>-openjdk-headless.x86_64<br>\njava-<span class=\"nu0\">11<\/span>-openjdk-src.x86_64<br>\njavapackages-filesystem.noarch<br>\njavapackages-tools.noarch<br>\ntzdata-java.noarch<\/div><\/div><\/pre>\n<p>Assume you need information on Java 8 (version <code>1.8<\/code>).<br \/>\nYou can see each file in the Java 8 packages using the <code>rpm<\/code> command:<\/p>\n<pre>\n<div class=\"geshifilter\"><div class=\"bash geshifilter-bash\">$ rpm <span class=\"re5\">--query<\/span> <span class=\"re5\">--all<\/span> <span class=\"re5\">--list<\/span> <br>\njava-1.8.0-openjdk<span class=\"sy0\">*<\/span> <span class=\"sy0\">|<\/span> <span class=\"kw2\">grep<\/span> bin<span class=\"sy0\">\/<\/span><span class=\"kw2\">java<\/span>$<br><span class=\"sy0\">\/<\/span>usr<span class=\"sy0\">\/<\/span>lib<span class=\"sy0\">\/<\/span>jvm<span class=\"sy0\">\/<\/span>java-1.8.0-openjdk-1.8.0.322.b06-<span class=\"nu0\">2<\/span>.el8_5.x86_64<span class=\"sy0\">\/<\/span>jre<span class=\"sy0\">\/<\/span>bin<span class=\"sy0\">\/<\/span><span class=\"kw2\">java<\/span><br><span class=\"sy0\">\/<\/span>usr<span class=\"sy0\">\/<\/span>lib<span class=\"sy0\">\/<\/span>jvm<span class=\"sy0\">\/<\/span>java-1.8.0-openjdk-1.8.0.322.b06-<span class=\"nu0\">2<\/span>.el8_5.x86_64<span class=\"sy0\">\/<\/span>bin<span class=\"sy0\">\/<\/span><span class=\"kw2\">java<\/span><\/div><\/div><\/pre>\n<p>You may get more than one result, but look at the file paths carefully. Generally, you just want the Java executable in the primary <code>bin<\/code> directory of its Java distribution.<\/p>\n<p>In this example, the path to Java 8 is:<\/p>\n<pre>\n<span class=\"geshifilter\"><code class=\"bash geshifilter-bash\">\u00a0<span class=\"sy0\">\/<\/span>usr<span class=\"sy0\">\/<\/span>lib<span class=\"sy0\">\/<\/span>jvm<span class=\"sy0\">\/<\/span>java-1.8.0-openjdk-1.8.0.322.b06-<span class=\"nu0\">2<\/span>.el8_5.x86_64<span class=\"sy0\">\/<\/span>bin<\/code><\/span><\/pre>\n<p>If you use a different package manager, the commands are different, but the goal remains the same. Consult your package manager&#8217;s documentation for details.<\/p>\n<h2>Find the right location from a manual install<\/h2>\n<p>If you&#8217;ve manually installed Java from a <code>.tar<\/code> archive (&#8220;tarball&#8221;), your package manager has no record of it. Hopefully, howerer, you know where you put it, so finding the path to its <code>JAVA_HOME<\/code> is as simple as listing the absolute path to its <code>bin<\/code> directory.<\/p>\n<p>Here&#8217;s how I confirm that the directory I think ought to exist does in fact exist:<\/p>\n<pre>\n<span class=\"geshifilter\"><code class=\"bash geshifilter-bash\"><span class=\"co4\">$ <\/span><span class=\"kw2\">file<\/span> <span class=\"sy0\">\/<\/span>opt<span class=\"sy0\">\/<\/span>jdk-1.8.0<span class=\"sy0\">\/<\/span>bin<\/code><\/span><\/pre>\n<h2>Set JAVA_HOME temporarily<\/h2>\n<p>Now you know the location of your desired <code>JAVA_HOME<\/code>.<\/p>\n<p>To set a <code>JAVA_HOME<\/code> variable temporarily, you can precede the command you want to run with your variable setting. For instance, suppose you&#8217;re on a system with both Java 8 and Java 11 installed on it. Java 8 was installed originally, but you downloaded Java 11 because a cool new application you want to try requires it.<\/p>\n<p>Evironment variables placed before the command you&#8217;re running get inherited by the command:<\/p>\n<pre>\n<span class=\"geshifilter\"><code class=\"bash geshifilter-bash\"><span class=\"co4\">$ <\/span><span class=\"re2\">JAVA_HOME<\/span>=<span class=\"sy0\">\/<\/span>opt<span class=\"sy0\">\/<\/span>jdk-1.8.0<span class=\"sy0\">\/<\/span>bin coolNewApp<\/code><\/span><\/pre>\n<h2>Set JAVA_HOME permanently with the alternatives command<\/h2>\n<p>To set a new default Java version permanently, use the <code>alternatives<\/code> command. The <code>alternatives<\/code> command looks at applications installed on your Linux system and lets you choose which version to use. Some distributions, such as Slackware, don&#8217;t provide an <code>alternatives<\/code> command, so you must use the local install method instead.<\/p>\n<p>On Fedora, CentOS, Mageia, and similar distributions, the command is <code>alternatives<\/code>.<\/p>\n<p>On Debian and Ubuntu and similar, the command is <code>update-alternatives<\/code>.<\/p>\n<p>To get a list of available versions of an application currently installed on your Fedora system:<\/p>\n<pre>\n<span class=\"geshifilter\"><code class=\"bash geshifilter-bash\"><span class=\"co4\">$ <\/span>alternatives <span class=\"re5\">--list<\/span><\/code><\/span><\/pre>\n<p>On Debian, you must specify the application you want alternatives for:<\/p>\n<pre>\n<span class=\"geshifilter\"><code class=\"bash geshifilter-bash\"><span class=\"co4\">$ <\/span>update-alternatives <span class=\"re5\">--list<\/span> <span class=\"kw2\">java<\/span><\/code><\/span><\/pre>\n<p>To choose which version you want to make the system default on Fedora and similar systems:<\/p>\n<pre>\n<span class=\"geshifilter\"><code class=\"bash geshifilter-bash\"><span class=\"co4\">$ <\/span><span class=\"kw2\">sudo<\/span> alternatives <span class=\"re5\">--config<\/span> <span class=\"kw2\">java<\/span><\/code><\/span><\/pre>\n<p>On Debian-based systems:<\/p>\n<pre>\n<span class=\"geshifilter\"><code class=\"bash geshifilter-bash\"><span class=\"co4\">$ <\/span><span class=\"kw2\">sudo<\/span> updates-alternatives <span class=\"re5\">--config<\/span> <span class=\"kw2\">java<\/span><\/code><\/span><\/pre>\n<p>You can change the default Java version as needed, based on which application you want to run.<\/p>\n<h2>Choose your Java<\/h2>\n<p>Usually, you don&#8217;t have to worry about applications getting confused when using\u00a0Java, but it&#8217;s a benefit of Java that it allows you to have different versions installed at once. Thanks to that feature, you can run even old software that hasn&#8217;t been updated to the latest version of Java, while still running the latest Java apps.<\/p>\n<p>However, with that flexibility comes the responsibility of understanding how to guide applications to the correct version. Now you know how.<\/p>\n<p>If this method seems like a lot of work, consider using SDKMan instead.<\/p>\n<\/div>\n<div class=\"clearfix text-formatted field field--name-field-article-subhead field--type-text-long field--label-hidden field__item\">\n<p>Make the most of Java&#8217;s flexibility by learning how to guide applications to the correct version.<\/p>\n<\/div>\n<div class=\"field field--name-field-lead-image field--type-entity-reference field--label-hidden field__item\">\n<article class=\"media media--type-image media--view-mode-caption\">\n<div class=\"field field--name-field-media-image field--type-image field--label-hidden field__item\">  <img decoding=\"async\" src=\"https:\/\/www.cryptocabaret.com\/wp-content\/uploads\/2022\/03\/java-coffee-beans.jpg\" width=\"520\" height=\"292\" alt=\"Coffee beans\" title=\"Coffee beans\" loading=\"lazy\"><\/div>\n<div class=\"field field--name-field-caption field--type-text-long field--label-hidden caption field__item\"><span class=\"caption__byline\">Image by: <\/span><\/p>\n<p>Pixabay. CC0.<\/p>\n<\/div>\n<\/article>\n<\/div>\n<div class=\"field field--name-field-tags field--type-entity-reference field--label-hidden field__items\">\n<div class=\"field__item\"><a href=\"https:\/\/opensource.com\/tags\/java\" hreflang=\"en\">Java<\/a><\/div>\n<\/p><\/div>\n<div class=\"field field--name-field-listicle-title field--type-string field--label-hidden field__item\">What to read next<\/div>\n<div class=\"field field--name-field-listicles field--type-entity-reference field--label-hidden field__items\">\n<div class=\"field__item\"><a href=\"https:\/\/opensource.com\/article\/22\/3\/manage-java-versions-sdkman\" hreflang=\"und\">Manage Java versions with SDKMan<\/a><\/div>\n<\/p><\/div>\n<div class=\"field field--name-field-default-license field--type-list-string field--label-hidden field__item\"><a rel=\"license\" href=\"http:\/\/creativecommons.org\/licenses\/by-sa\/4.0\/\"><br \/>\n        <img decoding=\"async\" alt=\"Creative Commons License\" src=\"https:\/\/www.cryptocabaret.com\/wp-content\/uploads\/2022\/03\/cc-by-sa-4.png\" title=\"This work is licensed under a Creative Commons Attribution-Share Alike 4.0 International License.\"><\/a>This work is licensed under a Creative Commons Attribution-Share Alike 4.0 International License.<\/div>\n<section class=\"field field--name-field-comments field--type-comment field--label-hidden comment-wrapper\">\n<div class=\"comments__count\">\n<div class=\"login\"><a href=\"https:\/\/opensource.com\/user\/register?absolute=1\">Register<\/a> or <a href=\"https:\/\/opensource.com\/user\/login?current=\/rss.xml&amp;absolute=1\">Login<\/a> to post a comment.<\/div>\n<\/p><\/div>\n<\/section>\n<p class=\"wpematico_credit\"><small>Powered by <a href=\"http:\/\/www.wpematico.com\" target=\"_blank\" rel=\"noopener\">WPeMatico<\/a><\/small><\/p>\n","protected":false},"excerpt":{"rendered":"<p>How to find your Java Home when running multiple versions Seth Kenlon Wed, 03\/16\/2022 &#8211; 03:00 Up Register or Login to like. When you install Java, it&#8217;s part of the installer&#8217;s job to tell your system where to find the right language when it&#8217;s needed. That&#8217;s why you&#8217;re able to run Java applications\u2014in fact, you [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":62971,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[307],"tags":[],"class_list":["post-62970","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-open-source"],"_links":{"self":[{"href":"https:\/\/www.cryptocabaret.com\/index.php?rest_route=\/wp\/v2\/posts\/62970","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.cryptocabaret.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.cryptocabaret.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.cryptocabaret.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.cryptocabaret.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=62970"}],"version-history":[{"count":0,"href":"https:\/\/www.cryptocabaret.com\/index.php?rest_route=\/wp\/v2\/posts\/62970\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.cryptocabaret.com\/index.php?rest_route=\/wp\/v2\/media\/62971"}],"wp:attachment":[{"href":"https:\/\/www.cryptocabaret.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=62970"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.cryptocabaret.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=62970"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.cryptocabaret.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=62970"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}