{"id":470,"date":"2019-04-08T00:45:10","date_gmt":"2019-04-07T23:45:10","guid":{"rendered":"https:\/\/blog.syddel.uk\/?p=470"},"modified":"2024-10-04T13:43:57","modified_gmt":"2024-10-04T12:43:57","slug":"installing-java-on-linux","status":"publish","type":"post","link":"https:\/\/blog.syddel.uk\/?p=470","title":{"rendered":"Installing Java on Linux"},"content":{"rendered":"\n<p>Java has a runtime (the &#8220;JRE&#8221;, or &#8220;Java Runtime Environment&#8221;) which is required to run Java applications. There&#8217;s also a &#8220;JDK&#8221; (&#8220;Java Development Kit&#8221;) which is required to develop applications in Java. There are numerous blogs out there that explain how to install Java from repositories. This blog post will detail how to download and install Oracle&#8217;s Java SE manually.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Download Oracle Java SE<\/h3>\n\n\n\n<p>Oracle Java SE can be downloaded from:<\/p>\n\n\n\n<p><a href=\"https:\/\/www.oracle.com\/technetwork\/java\/javase\/downloads\/index.html\">https:\/\/www.oracle.com\/technetwork\/java\/javase\/downloads\/index.html<\/a><\/p>\n\n\n\n<p>Click on the <strong>Download<\/strong> button for the JDK version you wish to install. There are many Linux options available to download, but the rest of this blog post will assume you downloaded the <code>.tar.gz<\/code> version.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Extract the Oracle Java SE<\/h3>\n\n\n\n<p>Extract the tar file you downloaded to the directory you wish to install Java in. For example, if you downloaded Java 12 (<code>jdk-12_linux-x64_bin.tar.gz<\/code>) and you wish to install Java to <code>\/usr\/local\/lib<\/code>:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">[syd:z97] Downloads $ <strong>sudo tar -zxvf jdk-12_linux-x64_bin.tar.gz -C \/usr\/local\/lib\/<\/strong><br> [sudo] password for syd: <br> jdk-12\/bin\/jaotc<br> jdk-12\/bin\/jar<br> jdk-12\/bin\/jarsigner<br> jdk-12\/bin\/java<br> jdk-12\/bin\/javac<br>...<br><br>[syd:z97] Downloads $ <strong>ls -l \/usr\/local\/lib\/<\/strong><br> total 12<br> drwxr-xr-x 9 root root  4096 Apr  7 23:32 jdk-12<br>...<br> [syd:z97] Downloads $<\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">Set $JAVA_HOME and update $PATH<\/h3>\n\n\n\n<p>Create an environment named <code>JAVA_HOME<\/code>, and set it to point to the directory you installed Java to (in this example, <code>\/usr\/local\/lib\/jdk-12<\/code>). Also add the bin directory (i.e. <code>\/usr\/local\/lib\/jdk-12\/bin<\/code>) to your system&#8217;s path. I normally make these changes system-wide by creating <code>\/etc\/profile.d\/java.sh<\/code> that contains:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">export JAVA_HOME=\/usr\/local\/lib\/jdk-12<br>export PATH=$PATH:$JAVA_HOME\/bin<br><\/pre>\n\n\n\n<p>Run (source) the above using:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">$ <strong>source \/etc\/profile.d\/java.sh<\/strong><\/pre>\n\n\n\n<p>or just:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">$ <strong>. \/etc\/profile.d\/java.sh<\/strong><\/pre>\n\n\n\n<p>You could stop there, and Java will work:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><strong>$ javac -version<\/strong><br>javac 12<\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">But wait! There&#8217;s more!!!<\/h3>\n\n\n\n<p>You may find yourself in a situation where you need to work on multiple Java projects that use different versions of the JDK. It is possible to quickly switch between different versions of the JDK using update-alternatives.<\/p>\n\n\n\n<p>Register your JDK install with <code>update-alternatives<\/code>:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">$ <strong>sudo update-alternatives --install \"\/usr\/bin\/java\" \"java\" \"\/usr\/local\/lib\/jdk-12\/bin\/java\" 1<\/strong><br>$ <strong>sudo update-alternatives --install \"\/usr\/bin\/javac\" \"javac\" \"\/usr\/local\/lib\/jdk-12\/bin\/javac\" 1<\/strong><\/pre>\n\n\n\n<p>You can view how many &#8220;alternatives&#8221; for <code>javac<\/code> you have using:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><strong>$ update-alternatives --list javac<\/strong><br> \/usr\/local\/lib\/jdk-12\/bin\/javac<br> \/usr\/local\/lib\/jdk1.8.0_201\/bin\/javac<\/pre>\n\n\n\n<p>To switch between different versions of <\/code>javac<\/code>:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">$ <strong>sudo update-alternatives --config javac<\/strong><br> There are 2 choices for the alternative javac (providing \/usr\/bin\/javac).<br> Selection    Path                                Priority   Status<br>------------------------------------------------------------------------<br> 0            \/usr\/local\/lib\/jdk-12\/bin\/javac         1         auto mode<br> 1            \/usr\/local\/lib\/jdk-12\/bin\/javac         1         manual mode<br> 2            \/usr\/local\/lib\/jdk1.8.0_201\/bin\/javac   1         manual mode <br> Press  to keep the current choice[*], or type selection number:<br> <\/pre>\n\n\n\n<p>Don&#8217;t forget if you switch your javac version, you should also switch your java version too:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">$ <strong>sudo update-alternatives --config java<\/strong><br>...<br><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">Uninstalling Java<\/h3>\n\n\n\n<p>Assuming I want to uninstall JDK 12 which is installed in <code>\/usr\/local\/lib\/jdk-12<\/code>:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">$ sudo update-alternatives --remove \"javac\" \"\/usr\/local\/lib\/jdk-12\/bin\/javac\"<br>$ sudo update-alternatives --remove \"java\" \"\/usr\/local\/lib\/jdk-12\/bin\/java\"<br>$ sudo rm -rf \/usr\/local\/lib\/jdk-12<\/pre>\n\n\n\n<p>After this, clean up any files that set <code>$JAVA_HOME<\/code> or add the JDK directory to <code>$PATH<\/code> (in my case, this is done in <code>\/etc\/profile.d\/java.sh<\/code>).<\/p>\n\n\n\n<p><br><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Java has a runtime (the &#8220;JRE&#8221;, or &#8220;Java Runtime Environment&#8221;) which is required to run Java applications. There&#8217;s also a &#8220;JDK&#8221; (&#8220;Java Development Kit&#8221;) which is required to develop applications in Java. There are numerous blogs out there that explain how to install Java from repositories. This blog post will detail how to download and<a class=\"moretag\" href=\"https:\/\/blog.syddel.uk\/?p=470\"><span class=\"screen-reader-text\">Read more about Installing Java on Linux<\/span>[&#8230;]<\/a><\/p>\n","protected":false},"author":1,"featured_media":482,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[19,15],"tags":[],"class_list":["post-470","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-coding","category-installation"],"_links":{"self":[{"href":"https:\/\/blog.syddel.uk\/index.php?rest_route=\/wp\/v2\/posts\/470","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/blog.syddel.uk\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blog.syddel.uk\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blog.syddel.uk\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/blog.syddel.uk\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=470"}],"version-history":[{"count":11,"href":"https:\/\/blog.syddel.uk\/index.php?rest_route=\/wp\/v2\/posts\/470\/revisions"}],"predecessor-version":[{"id":481,"href":"https:\/\/blog.syddel.uk\/index.php?rest_route=\/wp\/v2\/posts\/470\/revisions\/481"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/blog.syddel.uk\/index.php?rest_route=\/wp\/v2\/media\/482"}],"wp:attachment":[{"href":"https:\/\/blog.syddel.uk\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=470"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blog.syddel.uk\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=470"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blog.syddel.uk\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=470"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}