niedziela, 5 lutego 2017

Set-up Java Development Kit on Linux

All credit goes to author (Dudko) of this guide which you may follow instead of this post if you want, since it is much shorther than this one and worked for me on Linux Mint 18.1.

If you like to develop on Linux, particularly on Linux Mint then you might stumble across a problem when using JDK based applications such as JetBrains InteliJ or Android Studio which require JDK in the system. You might get error about HOME variable not being set-up or something similar. Below guide is slightly more detailed version of the above one with few tips.

In order to install Oracle JDK, first you have to uninstall it's open source counterpart openJDK (well not necesarilly since you may have several JDK versions in your system which is exact purpose of alternatives but since it does not make much sense to have both and for cleaniness and purpose of this tutorial I recommend uninstalling). 
To do so first make sure you have all the latest updates by invoking standard:
"sudo apt-get update". 
Then invoke: "sudo apt-get uninstall openjdk*" - the asterisk at the end  includes all packages that start with openJDK prefix which means you do not have to know the exact version/exact name of the openJDK you currently have.

Then download appropriate JDK from Oracle's official website and unpack it somewhere - make sure to shorten the name of the folder slightly so that you do not have to type the whole awkward path later (e.g. from "jdk1.7.0_25/" to "jdk170").

Now you have a choice where to store the JDK, it can be anywhere but I recommend the same place as the guide, optional software folder in root which is "/opt/java"
Since moving files or folders to that directory requires root access, we can make it easier on ourselves and start file manager (if you are on Linux Mint then it will be Nemo) in privileged mode. 
To do so either open Nemo, right click and choose "Open in terminal" or simply invoke "sudo nemo" (if you are using some other file manager like Nautilus on Ubuntu then it will be "sudo nautilus"). 
This will grant the file manager write privilege for root files and folders so now you can simply drag and drop the unpacked JDK folder from e.g. "home/Downloads" to "/opt/" or you can create folder called "java" like you you usually do and drop it there.

Now that you have done this, you have to actually tell the system where to look for the JDK. 
But first you should check whether openJDK or any other JDK was properly removed from your system, to do so invoke:
"update-alternatives --display java" - this will display current location of any of the JRE or JDK that is in use on your system. 
If you properly uninstalled the openJDK and any other JRE/JDK then it should display that there is no alternatives. 

Note: since JRE (Java Run Time Environment) is subset of JDK (Java Development Kit) you only need JDK to both run Java apps (like you do with JRE) and develop them (for which JDK is needed).

Next to tell your system where JDK is invoke:
'sudo update-alternatives --install "/usr/bin/java" "java" "path_to_jdk_folder/bin/java" 1' - this installs/registers the link to JDK with highest priority in "usr/bin/java" folder in which programs look for JDK (as you may have several java versions with different priorities all pointing to "/usr/bin/java").

Note: you do not need the quotes around the path if the path does not contain spaces. 

Finally to set this version of JDK as system default invoke:
'sudo update-alternatives --set java "path_to_jdk_folder/bin/java" '

To test if everything worked fine invoke:
"java -version" - this should display the version you just installed.

Tested on Linux Mint 18.1 but it should also work fine on Ubuntu. 
As already mentioned the "/opt/java/" folder could be anything else. 
One other suitable location I recommend is "/etc/alternatives" however that folder is usually much more "polluted" by other software making it difficult to navigate.

Brak komentarzy:

Prześlij komentarz