137

So I just installed Ubuntu, and this is my first time working in a Linux environment, so bear with my noobishness here.

Anyway, I downloaded the Java 8 JDK directly from Oracle, but I understand there's also an OpenJDK for Java 8 as well. I have two questions.

  1. What is the difference between the Oracle version and OpenJDK. Pros/Cons?

  2. When I try and get OpenJDK 8, I can't. I'm pasting the terminal command and the error message I get here, hoping for advice. Again, very new to Linux, been on my first Linux distro ever for less than an hour here at this point.

    user@computer:~/Java$ sudo apt-get install openjdk-8-jdk
    Reading package lists... Done
    Building dependency tree       
    Reading state information... Done
    E: Unable to locate package openjdk-8-jdk
    

Any help would be appreciated. In the meantime, I'm just gonna use the Oracle package. Thanks.

Edit:

So, now I have a new problem. I installed OpenJDK 7, it came with Maven and just installed before I realized I was getting a two-for-one deal. Now I have JDK 7, and Eclipse, and Maven, and they're all working, but I'm stuck on JDK 7 rather than 8.

I saw that there's a link below, I intend to check it out in the morning, it's 2am and I'm tired right now. Any other advice for this new issue would be appreciated, otherwise I will definitely be checking out that link first thing in the morning.

5
  • 1
  • What version of Ubuntu are you running? The latest couple of versions have had it available as an installation option.
    – Makoto
    Oct 5, 2015 at 5:55
  • Stephen's link will resolve your issue :)
    – Juned
    Oct 5, 2015 at 6:00
  • Thanks. It's 2am, so I'm going to bed, but I'll check the link in the morning. I added an edit to my question with an additional issue, if you could look it over that would be great. If the existing link/answer will help with that too, I apologize for being too tired to look at it right now, I'll be checking it out in the morning. Oct 5, 2015 at 6:13
  • I'm on ubuntu 14.10 I believe. Whatever the most recent long term release was. Oct 5, 2015 at 6:14

13 Answers 13

149

UPDATE: installation without root privileges below


I advise you to not install packages manually on ubuntu system if there is already a (semi-official) repository able to solve your problem. Further, use Oracle JDK for development, just to avoid (very sporadic) compatibility issues (i've tried many years ago, it's surely better now).

Add the webupd8 repo to your system:

sudo add-apt-repository ppa:webupd8team/java
sudo apt-get update

Install your preferred version of jdk (versions from java-6 to java-9 available):

sudo apt-get install oracle-java8-installer

You can also install multiple version of jdk, mixing openjdk and oracle versions. Then you can use the command update-java-alternatives to switch between installed version:

# list available jdk
update-java-alternatives --list

# use jdk7
sudo update-java-alternatives --set java-7-oracle

# use jdk8
sudo update-java-alternatives --set java-8-oracle

Requirements

If you get add-apt-repository: command not found be sure to have software-properties-common installed:

sudo apt-get install software-properties-common

If you're using an older version Ubuntu:

sudo apt-get install python-software-properties

JDK installation without root privileges

If you haven't administrator rights on your target machine your simplest bet is to use sdkman to install the zulu certified openjdk:

curl -s "https://get.sdkman.io" | bash
source "$HOME/.sdkman/bin/sdkman-init.sh"
sdk install java

NOTE: sdkman allow to install also the official Oracle JDK, although it's not a the default option. View available versions with:

sdk ls java

Install the chosen version with:

sdk install java <version>

For example:

sdk install java 9.0.1-oracle

Glossary of commands

  • sudo <command> [command_arguments]: execute a command with the superuser privilege.

  • add-apt-repository <PPA_id>: Ubuntu (just like every Debian derivatives and generally speaking every Linux distribution) has a main repository of packages that handle things like package dependencies and updating. In Ubuntu is possible to extend the main repository using a PPA (Personal Package Archive) that usually contains packages not available in the system (just like oracle jdk) or updated versions of available ones (example: LibreOffice 5 in LTS is available only through this PPA).

  • apt-get [install|update|upgrade|purge|...]: it's "the" command-line package handler used to manipulate the state of every repository on the system (installing / updating / upgrading can be viewed as an alteration of the repository current state).

In our case: with the command sudo add-apt-repository ppa:webupd8team/java we inform the system that the next repository update must retrieve packages information also from webupd8 repo.

With sudo apt-get update we actually update the system repository (all this operations requires superuser privileges, so we prepend sudo to the commands).

sudo apt-get install oracle-java8-installer

  • update-java-alternatives (a specific java version of update-alternatives): in Ubuntu several packages provides the same functionality (browse the internet, compile mails, edit a text file or provides java/javac executables...). To allows the system to choose the user favourites tool given a specific task a mechanism using symlinks under /etc/alternatives/ is used. Try to update the jdk as indicated above (switch between java 7 and java 8) and view how change the output of this command:

    ls -l /etc/alternatives/java*

In our case: sudo update-java-alternatives --set java-8-oracle update symlinks under /etc/alternatives to point to java-8-oracle executables.

Extras:

  • man <command>: start using man to read a really well written and detailed help on (almost) every shell command and its options (every command i mention in this little answer has a man page, try man update-java-alternatives).

  • apt-cache search <search_key>: query the APT cache to search for a package related with the search_key provided (can be the package name or some word in package description).

  • apt-cache show <package>: provides APT information for a specific package (package version, installed or not, description).

15
  • 1
    The problem is I get a message "Unable to locate package oracle-java8-installer". Same thing happens when I try and get openjdk 8 using the sudo apt-get command. Oct 5, 2015 at 14:07
  • 3
    Notably, this is (at least labeled as) the Oracle commercial version of java, not the open community version (openjre). Jun 11, 2016 at 23:23
  • 2
    This answer is not valid - oracle prohibits direct downloads of java installer. sudo apt-get install oracle-java8-installer fails Oct 12, 2016 at 23:19
  • 2
    I've been trying to solve this issue for 4 hours already. Thanks for this awesome solution!
    – ickyrr
    Jul 8, 2017 at 16:27
  • 2
    ppa:webupd8team/java was discontinued, and sudo apt-get install oracle-java8-installer does not work. When you add the PPA, you get this message: 'Oracle Java downloads now require logging in to an Oracle account to download Java updates, like the latest Oracle Java 8u211 / Java SE 8u212. Because of this I cannot update the PPA with the latest Java (and the old links were broken by Oracle).' Mar 29, 2020 at 22:00
134

As you can see I only have java 1.7 installed (on a Ubuntu 14.04 machine).

update-java-alternatives -l
java-1.7.0-openjdk-amd64 1071 /usr/lib/jvm/java-1.7.0-openjdk-amd64

To install Java 8, I did,

sudo add-apt-repository ppa:openjdk-r/ppa
sudo apt-get update
sudo apt-get install openjdk-8-jdk

Afterwards, now I have java 7 and 8,

update-java-alternatives -l
java-1.7.0-openjdk-amd64 1071 /usr/lib/jvm/java-1.7.0-openjdk-amd64
java-1.8.0-openjdk-amd64 1069 /usr/lib/jvm/java-1.8.0-openjdk-amd64

BONUS ADDED (how to switch between different versions)

  • run the follwing command from the terminal:

sudo update-alternatives --config java

There are 2 choices for the alternative java (providing /usr/bin/java).

  Selection    Path                                            Priority   Status
------------------------------------------------------------
  0            /usr/lib/jvm/java-7-openjdk-amd64/jre/bin/java   1071      auto mode
  1            /usr/lib/jvm/java-7-openjdk-amd64/jre/bin/java   1071      manual mode
* 2            /usr/lib/jvm/java-8-openjdk-amd64/jre/bin/java   1069      manual mode

Press enter to keep the current choice[*], or type selection number:

As you can see I'm running open jdk 8. To switch to to jdk 7, press 1 and hit the Enter key. Do the same for javac as well with, sudo update-alternatives --config javac.

Check versions to confirm the change: java -version and javac -version.

5
  • 2
    Very useful for I didn't know which repo was required to install OpenJDK ! Thanks a lot.
    – cram2208
    May 14, 2016 at 2:49
  • @cram2208 glad that helped you. thanks for the comment.
    – samsamara
    May 16, 2016 at 1:18
  • Is the openjdk-r safe enough for server?
    – Wang
    Jun 11, 2016 at 14:49
  • 1
    Thanks for detail answer, this solution is perfectly work for me.
    – Muddasir23
    Oct 1, 2019 at 7:40
  • 1
    If add-apt-repository command is not found run the below command ``` sudo apt install software-properties-common ``` Oct 18, 2022 at 17:02
18
sudo apt-get update
sudo apt-get  install openjdk-8-jdk

this should work

3
  • 4
    Please add an explanation what this adds to the existing answers. Feb 28, 2019 at 7:35
  • I was using the WSL2 from Windows, and I forgot to update the packages before installing the OpenJDK and got the same error. @GertArnold, the answer of Zoe helped me in that scenario.
    – pcdro
    Oct 26, 2019 at 15:40
  • Why is this answer at the bottom? Can we please upvote it to go to the top ;-) Dec 6, 2021 at 3:47
14

I'm getting OpenJDK 8 from the official Debian repositories, rather than some random PPA or non-free Oracle binary. Here's how I did it:

sudo apt-get install debian-keyring debian-archive-keyring

Make /etc/apt/sources.list.d/debian-jessie-backports.list:

deb http://httpredir.debian.org/debian/ jessie-backports main

Make /etc/apt/preferences.d/debian-jessie-backports:

Package: *
Pin: release o=Debian,a=jessie-backports
Pin-Priority: -200

Then finally do the install:

sudo apt-get update
sudo apt-get -t jessie-backports install openjdk-8-jdk
2
  • 2
    And final finally update-alternatives --config java ;-)
    – Javali
    Feb 22, 2017 at 18:32
  • Thanks a lot! This is the only solution that worked for me (Deb 8)
    – user1334546
    Jul 31, 2017 at 14:25
13

I've been searching the internet for an answer on this. I have a Debian 10 server installation.

I had the same problem with installing openjdk-8-jdk. An apt install resulted in package not found.

The other comment here helps you install the oracle jdk 8, but I wanted to use the openjdk 8.

What I discovered is that for Debian 10, by default, only the openjdk-11 packages are installable without adding a ppa, however for Debian 9 (stretch), by default, you can install openjdk-8 packages. Therefore, adding the stretch Debian ppa allowed me to install openjdk-8 packages.

Adding the Debian stretch ppa.

sudo apt-add-repository 'deb http://security.debian.org/debian-security stretch/updates main'

Then update and install openjdk-8-jdk

sudo apt update && sudo apt install openjdk-8-jdk
7

i have same problem by installing openjdk-8-jdk on ubuntu and on kali but failed then i found program is not listed in the repository list so i solved it by adding PPA (personal package archive These are unofficial repositories).

sudo apt update

sudo apt install software-properties-common

sudo apt-add-repository 'deb http://security.debian.org/debian-security stretch/updates main'

sudo apt install openjdk-8-jdk

3

I was having the same issue and tried all of the solutions on this page but none of them did the trick.

What finally worked was adding the universe repo to my repo list. To do that run the following command

sudo add-apt-repository universe

After running the above command I was able to run

sudo apt install openjdk-8-jre

without an issue and the package was installed.

Hope this helps someone.

2
  • 1
    Not sure why I've been downvoted for this answer. If there is something incorrect or a potential issue please leave a comment to help others and myself understand better. Jun 26, 2020 at 19:19
  • Getting this error in add-apt repository 0.482 Error: 'universe' invalid Oct 10 at 12:41
2

Run the below commands... Issue resovled :)

sudo apt install software-properties-common -y

sudo add-apt-repository ppa:openjdk-r/ppa
sudo apt-get update -y
sudo apt-get install openjdk-8-jdk -y
1

Probably coming in late, but it saved me a lot:

On Ubuntu, Search and click on Software and Updates. The following page loads. Make sure you check the appropriate boxes under Downloadable from the internet. When these are unchecked, several packages don't get found.

enter image description here

1

Personally I tried a bunch of these solutions none of which worked for me, eventually though I found this worked.

sudo apt-add-repository 'deb http://security.debian.org/debian-security stretch/updates main'

Then to install openjdk 8.

sudo apt install openjdk-8-jdk

0

After adding the JDK repo, before Installing you might want to run an update first so the repo can be added run apt update

an then continue with your installation sudo apt install adoptopenjdk-8-hotspot

-2

Try; openjdk-11-jdk and default-jdk

Or :

Update all packages with :

Sudo apt-get install updates

Then go to root , download the java version you want and extract. (If you don't know manual install try the recommended codes in the discussion)

Change the environment libraries (java) with oracle.com guides for each,long process but magical

-2

I had the same problem on windows10 when i installed Ubuntu as Subsystem (terminal) on win10 , I tried many things to fix this issue, but I could download openjdk-8-jdk by installing Kali Linux (terminal) instead of Ubuntu on win10.

1
  • 1
    Your answer could be improved with additional supporting information. Please edit to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers in the help center.
    – Community Bot
    Sep 19, 2021 at 19:50

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.