How to install Java on Ubuntu

java logo

Java was developed by Sun (now owned by Oracle) back in 1992, and arose from the need to create a platform that would allow the development of universal source code. The idea was to develop applications that could be created in any operating system that has Java support and then be executed in any other without the need for modifications, which in the jargon was known as WORA ("write once run anywhere", or " write once, execute anywhere »).

That is how Java made it to the major operating systems like Windows, Mac OS X (at that time, MacOS) and of course Linux. In this clear case, with arrival to many distros, although not all package systems incorporate it in a friendly way or offer the newest versions. And in some quite notorious cases like that of Ubuntu, we have to take a few laps to install both the Java runtime and its SDK if we prefer (or need to start developing code).

Now let's see how to install java on Ubuntu, something that is not entirely complicated, although it does require some steps that should be clear, especially since we also currently have the possibility of installing both the version of Oracle's Java -that is, the official one- and OpenJDK, which is developed by the community and that began as a bet for the future when it was not clear what Java's role would be in terms of its attitude towards it. FOSS.

The compatibility between the two is 99,9 percent, but personally I think that for a training that makes things easier for us if we want to work in the company it is convenient to adapt as much as possible to the official tools. For example, in the case of Java it is more useful to learn how to use Netbeans or Eclipse and use Oracle's Java. So, the first thing is to check if when installing our distro it came with OpenJDK:

java -version

The system will return the information of the Java version we have installed, for example something like 'OpenJDK Runtime Environment' if we have the OpenJDK version. If that is the case, we can uninstall it by:

sudo apt-get purge openjdk - \ *

Now we have the complete assurance of having removed everything related to the previous Java installation, to start with a clean one. The first step is to create the folders or directories in which we are going to install the new version, and this is very simple:

sudo mkdir -p / usr / local / java

Then we have to download the Java SDK paying special attention to whether it is the one that corresponds to our system, that is, 32 or 64 bits, since for example Java for 64 bits will not work correctly on 32-bit systems and will give us errors of all kinds. We copy the download to the folder that we created in the previous step, using:

cp jdk-8-linux-x64.tar.gz / usr / local / java

Then we position ourselves in that directory and unzip it:

tar -xvf jdk-8-linux-x64.tar.gz

With this command, the java download, and it will be inside the folder that we created previously, something like / usr / local / java / jdk8, and within it all the subfolders that are part of the compressed file that we have downloaded.

We are doing well, and there is little left but we still have an important step to do and that is to make the system identify the Java commands so that we can execute them without having to enter the entire path to them but simply by typing a specific command, such as Java: Javac. This is called 'add to the path' and it is quite simple to do since we have to modify the contents of the file / Etc / profile. For this we use a text editor of our preference, in my case Gedit:

sudo gedit / etc / profile

and we add the following:

JAVA_HOME = / usr / local / java / jdk8
PATH = $ PATH: $ HOME / bin: $ JAVA_HOME / bin
export JAVA_HOME
export PATH

We save the changes, and now we have add this Java installation to the database of our system, which we do through the command update-alternatives.

With this command we inform the system that Oracle Java JRE, JDK and Java Webstart are available:

sudo update-alternatives –install "/ usr / bin / java" "java" "/ usr / local / java / jdk8 / bin / java" 1

sudo update-alternatives –install "/ usr / bin / javac" "javac" "/ usr / local / java / jdk8 / bin / javac" 1

sudo update-alternatives –install "/ usr / bin / javaws" "javaws" "/ usr / local / java / jdk8 / bin / javaws" 1

Now let's set Oracle Java as the default runtime of the system:

sudo update-alternatives –set java / usr / local / java / jdk8 / bin / java

sudo update-alternatives –set javac / usr / local / java / jdk8 / bin / javac

sudo update-alternatives –set javaws / usr / local / java / jdk8 / bin / javaws

That's it, we have finished with the installation, and we can check it by just executing the initial command again and verifying what it saves it gives us:

java-version,

As we will see, we will already be running the Oracle Java runtime updated to its newest version.

More information - Ubuntu could have the best browser in the world and yours, Netbeans in Ubuntu, How to install an IDE in our Ubuntu (I)


13 comments, leave yours

Leave a Comment

Your email address will not be published. Required fields are marked with *

*

*

  1. Responsible for the data: Miguel Ángel Gatón
  2. Purpose of the data: Control SPAM, comment management.
  3. Legitimation: Your consent
  4. Communication of the data: The data will not be communicated to third parties except by legal obligation.
  5. Data storage: Database hosted by Occentus Networks (EU)
  6. Rights: At any time you can limit, recover and delete your information.

  1.   Rodrigo castro said
  2.   James said

    All this to install Java, and then you pretend that people go from windows xp to linux, please… ..It's a chimera, in any case the licenses for windows 7 will increase, I think this year is not going to be the year either Linux ……
    A fantastic news as a paragon of free software and the great options of open source and more chimeras…. IT SEEMS TO BE THAT CANONICAL IS COMING FROM THE CLOUD …….

    Canonical shuts down Ubuntu One for failing to compete with the cloud services price war

  3.   Willy klew said

    Jaumet, it is clear that the procedure is somewhat tedious but installing some development tools in Windows is not a trivial task (for example the tools for Android development).
    Rodrigo, sometimes I have installed Java that way, but in this case I have looked for a more definitive solution. And it is that if one day that PPA stops being maintained or updated there you stay, while in this procedure that we detail we only have to update the directory in which we install Java with a new version, and since the JDK structure is always the same symbolic links and PATH entries will always be correct, regardless of whether we have Java 8, Java 8.1, Java 9 or whatever.

    Regards!

  4.   dani said

    I have tried, but with the first update-install command, the terminal looks silly, I can keep entering commands that do nothing, I don't know if it will be necessary to wait a while or not, but in the end, I have returned to openjdk , that is not bad

  5.   Willy klew said

    Dani, how strange that you tell me
    could you tell me the output of the command

    sudo / usr / sbin / update-alternatives –config java

    Regards!

  6.   Javier said

    Friend, everything was going well. But when I type these commands

    sudo update-alternatives –install “/ usr / bin / javac” “javac” “/ usr / local / java / jdk8 / bin / javac” 1

    sudo update-alternatives –install “/ usr / bin / javaws” “javaws” “/ usr / local / java / jdk8 / bin / javaws” 1

    error: alternative link is not absolute as it should be: “/ usr / bin / javac”

    Just this sudo update-alternatives –install “/ usr / bin / java” “java” “/ usr / local / java / jdk8 / bin / java” 1 does not give me an error.

    And when I write java -version. I get this

    java version "1.8.0_05"
    Java (TM) SE Runtime Environment (build 1.8.0_05-b13)
    Java HotSpot (TM) 64-Bit Server VM (build 25.5-b02, mixed mode)

    I do not know if it was installed well. because when writing in the javac console it does not recognize it.

    I would appreciate your help.

    1.    Myself said

      before install there is not a single script if not such that yes - install

  7.   Hector said

    Good evening, just a tip since I tried to follow this tutorial but I think more explanation is missing apart from the fact that some commands are misspelled and mark many errors such as the problem in the comment above

  8.   Brayan Lopez said

    Where in the document in gdit should I add this?

    JAVA_HOME = / usr / local / java / jdk8
    PATH = $ PATH: $ HOME / bin: $ JAVA_HOME / bin
    export JAVA_HOME
    export PATH

  9.   Federico silva said

    I have a problem, I am a newbie and I followed the tutorial on how to install java to the letter, but when I asked to extract the content of "jdk-8u31-linux-x64.tar.gz" hosted in the folder created, I It says that operation is not allowed and does not let me extract. What I can do?

    1.    Miguel Torres said

      Hello to all friends, today I became a Linux Mint user and I ran into this problem since I need to use Java 8

      and following these steps I ran into the same problems as you.
      and I already solved it, they are only syntax errors if you need help add me to Skype nebneru85@hotmail.com and I solve the problem greetings

  10.   Jimmy olano said

    With your permission: here we are "resuscitating" entries and verifying how current they are today, Tuesday, December 06, 2016 (at this point if you are NOT interested in this, CLICK on another link or close this tab of your web browser),
    AND WE START:

    We uninstall by entering our 'root' password:

    sudo apt-get purge openjdk - \ *

    The link to download jdk-8-linux-x64.tar.gz (check your processor type and GNULinux distro, we use Ubuntu16 64 bits):

    http://www.oracle.com/technetwork/java/javase/downloads/jdk8-downloads-2133151.html

    *** As of today 2016-12dic-06 actually the package has the name jdk-8u111-linux-x64.tar.gz ***

    To copy the downloaded compressed file and extract its content, the command "sudo" must be preceded before each line of what is indicated here in this tutorial (in our case we use Ubuntu16 64-bit, eye):

    sudo cp jdk-8u111-linux-x64.tar.gz / usr / local / java /
    sudo cp jdk-8u111-linux-x64.tar.gz / usr / local / java /
    sudo tar -xvf jdk-8u111-linux-x64.tar.gz

    When executing the previous command the folder «/usr/local/java/jdk1.8.0_111» is created, at this moment if we enter «java -version» in the command line it kindly tells us to install it with «sudo apt install» for what we must tell our operating system WHERE IT IS INSTALLED by modifying the 'profile':

    gksudo gedit / etc / profile

    NOTE that we use "gksudo" because we are going to use gedit that uses a graphical interface, WE GENERALLY USE "nano" and the command would be "sudo nano / etc / profile" BUT USE THE TEXT EDITOR THAT YOU WOULD LIKE THAT IF THE EDITOR OF Selected text has a graphical interface, use "gksudo".

    WE ADD the lines indicated in this tutorial:

    JAVA_HOME = / usr / local / java / jdk8
    PATH = $ PATH: $ HOME / bin: $ JAVA_HOME / bin
    export JAVA_HOME
    export PATH

    (do not leave tabs or spaces in our / etc / profile file, add at the end of the file).

    Then we use update-alternatives to redirect our GNULinux distro (note the use of single quotes, the use of TWO SCREENS in -install and the variation in the path for our version packages jdk1.8.0_111 -on your computer maybe it is different-) :

    sudo update-alternatives –install '/ usr / bin / java' 'java' '/usr/local/java/jdk1.8.0_111/bin/java' 1
    sudo update-alternatives –install '/ usr / bin / javac' 'javac' '/usr/local/java/jdk1.8.0_111/bin/javac' 1
    sudo update-alternatives –install '/ usr / bin / javaws' 'javaws' '/usr/local/java/jdk1.8.0_111/bin/javaws' 1

    Now we are going to set Oracle Java as the default runtime of the system (again note the use of double hyphens in -set and -again- our path may be different from your path on your computer):

    sudo update-alternatives –set java /usr/local/java/jdk1.8.0_111/bin/java
    sudo update-alternatives --set javac /usr/local/java/jdk1.8.0_111/bin/javac
    sudo update-alternatives --set javaws /usr/local/java/jdk1.8.0_111/bin/javaws

    LASTLY, WE CHECK OUR INSTALLED VERSION AGAIN (it will return something like this -depending on your GNULinux distro version):

    jimmy @ KEVIN: /usr/local/java/jdk1.8.0_111$ java -version
    java version "1.8.0_111"
    Java (TM) SE Runtime Environment (build 1.8.0_111-b14)
    Java HotSpot (TM) 64-Bit Server VM (build 25.111-b14, mixed mode)
    jimmy @ KEVIN: /usr/local/java/jdk1.8.0_111$

    I HOPE THE WORK OF THIS HUMBLE SERVER WILL BE USEFUL, thank you for allowing me to publish our experiences and thus we share free knowledge #SoftwareLibre 😎, atte. Jimmy Olano.

  11.   Jesus said

    the fact of "copying" these commands and pasting them in the terminal, was what gave me an error, in addition to the double hyphen in * –install * that was necessary, and that the java path was not correct, I recommend writing it Step by Step