227

Note: This question was asked before Oracle made the OpenJDK the free version of the Oracle JDK, and the historic answers reflect that. As of 2022 you should not use Java 7 unless you must for projects which cannot run on OpenJDK 8.

To install java I have always used the classic way from the terminal. I would like to install java manually. I placed the folder of the JDK on the desk and I set environment variables (PATH, CLASSPATH and JAVA_HOME). From the terminal, if I type java -version I get printed

foralobo@ubuntu-vincy:~$ java -version
java version "1.7.0_21"
Java(TM) SE Runtime Environment (build 1.7.0_21-b11)
Java HotSpot(TM) 64-Bit Server VM (build 23.21-b01, mixed mode)

But when I try to install eclipse or netbeans, the system warns by saying that there is no java installed on the machine.

What is missing to compleatare manual installation? (Ubuntu 13.04)

Thorbjørn Ravn Andersen
  • 73,784
  • 33
  • 194
  • 347
foralobo
  • 3,947
  • 5
  • 18
  • 17

10 Answers10

551

This answer used to describe how to install Oracle Java 7. This no longer works since Oracle end-of-lifed Java 7 and put the binary downloads for versions with security patches behind a paywall. Also, OpenJDK has grown up and is a more viable alternative nowadays.

In Ubuntu 16.04 and higher, Java 7 is no longer available. Usually you're best off installing Java 8 (or 9) instead.

sudo apt-get install openjdk-8-jre

or, f you also want the compiler, get the jdk:

sudo apt-get install openjdk-8-jdk

In Trusty, the easiest way to install Java 7 currently is to install OpenJDK package:

sudo apt-get install openjdk-7-jre

or, for the jdk:

sudo apt-get install openjdk-7-jdk

If you are specifically looking for Java 7 on a version of Ubuntu that no longer supports it, see https://askubuntu.com/questions/761127/how-do-i-install-openjdk-7-on-ubuntu-16-04-or-higher .

flup
  • 26,937
  • 7
  • 52
  • 74
  • 1
    Added @brent-robinson 's tip on how to set the environment variables. Thanks Brent! – flup Jan 13 '14 at 23:42
  • 3
    after installing oracle-java7-set-default, I was not able to echo $JAVA_HOME. Hence set $JAVA_HOME manually in .bashrc – Sumit Ramteke Jan 31 '14 at 05:17
  • @sumitramteke I suspect you'd have to start a new session before the environment settings take effect. – flup Feb 06 '14 at 16:30
  • It's me or the first command it's wrong :P it should be apt-add-repository shouldn't it? – pekechis Feb 06 '14 at 23:32
  • 1
    @pekechis both work, apt-add-repository is a symlink to add-apt-repository. Says here it got added in 11.04: http://askubuntu.com/questions/38021/how-to-add-a-ppa-on-a-server – flup Feb 06 '14 at 23:41
  • @flup : Sorry I'm new with _Ubuntu_. Will you please elaborate more with your comment in regards of **starting new session**. – Sumit Ramteke Feb 07 '14 at 13:12
  • 3
    @sumitramteke I mean to log out and in again. The enviroment variables are set when you log in. – flup Feb 07 '14 at 14:27
  • @flup : Ok, yeah but that thing has already being tested. Still not able to echo the same. – Sumit Ramteke Feb 14 '14 at 09:45
  • @sumitramteke then you can set `JAVA_HOME` manually by adding `JAVA_HOME=/usr/lib/jvm/java-7-oracle` to `/etc/environment` – flup Apr 01 '14 at 20:20
  • Yes, done the same. @flup thanks for the advice anyway. – Sumit Ramteke Apr 07 '14 at 03:26
  • If you want to remove all non-Oracle Java packages before installing, use this command: `sudo apt-get purge openjdk-\* icedtea-\* icedtea6-\*` – Edenshaw May 19 '14 at 13:25
  • If `add-apt-repository` is missing, run `sudo apt-get install python-software-properties` – Brian Low Aug 03 '14 at 07:21
  • Also see http://stackoverflow.com/questions/19275856/auto-yes-to-the-license-agreement-on-sudo-apt-get-y-install-oracle-java7-instal to automatically accept the license agreement. – Brian Low Aug 17 '14 at 22:54
  • 4
    No longer works. Oracle seems to have removed the binaries. See my answer below. – Aniket Thakur Dec 16 '17 at 04:05
  • I always get connection failure when downloading this. It appears the link to site has been move. Any idea where I could still get JDK 7? I am planning to install Android Studio and this frickin software needs it even though I have JDK 8 installed – Neon Warge Mar 17 '18 at 02:31
  • On AskUbuntu, there also were some deprecated answers, so I answered [how to install Java 7 or 8 on a more recent Ubuntu](https://askubuntu.com/a/1161572/830570). This answer also solves the "PPA DISCONTINUED" Problem. You can also consider [this link](http://www.ubuntuupdates.org/package/webupd8_java/precise/main/base/oracle-java7-set-default) to set `JAVA_HOME` and `PATH` as [Brent Robinson mentioned in his answer](https://stackoverflow.com/a/20565550/4575793). – Cadoiz Aug 31 '21 at 07:31
56
sudo apt-get update
sudo apt-get install openjdk-7-jdk

and if you already have other JDK versions installed

sudo update-alternatives --config java

then select the Java 7 version.

steve cook
  • 3,116
  • 3
  • 30
  • 51
Sopan kokre
  • 569
  • 4
  • 2
48

In addition to flup's answer you might also want to run the following to set JAVA_HOME and PATH:

sudo apt-get install oracle-java7-set-default

More information at: http://www.ubuntuupdates.org/package/webupd8_java/precise/main/base/oracle-java7-set-default

Brent Robinson
  • 2,326
  • 1
  • 19
  • 14
38

Note: According to comments this text was copied from a GIST without attribution.

Download java jdk<version>-linux-x64.tar.gz file from https://www.oracle.com/technetwork/java/javase/downloads/index.html.

Extract this file where you want. like: /home/java(Folder name created by user in home directory).

Now open terminal. Set path JAVA_HOME=path of your jdk folder(open jdk folder then right click on any folder, go to properties then copy the path using select all) and paste here.

Like: JAVA_HOME=/home/xxxx/java/JDK1.8.0_201

Let Ubuntu know where our JDK/JRE is located.

sudo update-alternatives --install /usr/bin/java java /home/xxxx/java/jdk1.8.0_201/bin/java 20000
sudo update-alternatives --install /usr/bin/javac javac /home/xxxx/java/jdk1.8.0_201/bin/javac 20000
sudo update-alternatives --install /usr/bin/javaws javaws /home/xxxx/java/jdk1.8.0_201/bin/javaws 20000

Tell Ubuntu that our installation i.e., jdk1.8.0_05 must be the default Java.

sudo update-alternatives --set java /home/xxxx/sipTest/jdk1.8.0_201/bin/java
sudo update-alternatives --set javac /home/xxxx/java/sipTest/jdk1.8.0_201/bin/javac
sudo update-alternatives --set javaws /home/xxxxx/sipTest/jdk1.8.0_201/bin/javaws

Now try:

$ sudo update-alternatives --config java

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

  Selection    Path                                  Priority   Status
------------------------------------------------------------
* 0            /usr/lib/jvm/java-6-oracle1/bin/java   1047      auto mode
  1            /usr/bin/gij-4.6                       1046      manual mode
  2            /usr/lib/jvm/java-6-oracle1/bin/java   1047      manual mode
  3            /usr/lib/jvm/jdk1.7.0_75/bin/java      1         manual mode

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

update-alternatives: using /usr/lib/jvm/jdk1.7.0_75/bin/java to provide /usr/bin/java (java) in manual mode

Repeat the above for:

sudo update-alternatives --config javac
sudo update-alternatives --config javaws
Thorbjørn Ravn Andersen
  • 73,784
  • 33
  • 194
  • 347
Abhijit Bashetti
  • 8,518
  • 7
  • 35
  • 47
  • I saw this text on a GIST, was this yours or copy/pasted? – onaclov2000 May 12 '15 at 15:04
  • copy/pasted here as I did not find any of the above working :) – Abhijit Bashetti May 13 '15 at 04:46
  • 1
    Default Java or from repo couldn't be installed but It worked, solved problem. Thanks! – Arun Sep 05 '16 at 17:27
  • 1
    @AbhijitBashetti If this was copied and pasted from somewhere else, I think it should be better to add a sentence explicitly saying so, with a link to the original source. – lfurini Sep 08 '16 at 10:12
  • 2
    @lfurini:I already mentioned it if you read all the comments...I don't have the original link...The reason I added here is.. so that it could be useful for the rest and they need not have to spend time in searching it...As like I have invested to much time in getting the right steps...don't want others to invest time in such things.. – Abhijit Bashetti Sep 08 '16 at 10:16
12

I think you should consider Java installation procedure carefully. Following is the detailed process which covers almost all possible failures.

Installing Java with apt-get is easy. First, update the package index:

sudo apt-get update

Then, check if Java is not already installed:

java -version

If it returns "The program java can be found in the following packages", Java hasn't been installed yet, so execute the following command:

sudo apt-get install default-jre

You are fine till now as I assume.

This will install the Java Runtime Environment (JRE). If you instead need the Java Development Kit (JDK), which is usually needed to compile Java applications (for example Apache Ant, Apache Maven, Eclipse and IntelliJ IDEA execute the following command:

sudo apt-get install default-jdk

That is everything that is needed to install Java.

Installing OpenJDK 7:

To install OpenJDK 7, execute the following command:

sudo apt-get install openjdk-7-jre 

This will install the Java Runtime Environment (JRE). If you instead need the Java Development Kit (JDK), execute the following command:

sudo apt-get install openjdk-7-jdk

Installing Oracle JDK:

The Oracle JDK is the official JDK; however, it is no longer provided by Oracle as a default installation for Ubuntu.

You can still install it using apt-get. To install any version, first execute the following commands:

sudo apt-get install python-software-properties
sudo add-apt-repository ppa:webupd8team/java
sudo apt-get update

Then, depending on the version you want to install, execute one of the following commands:

Oracle JDK 7:

sudo apt-get install oracle-java7-installer

Oracle JDK 8:

sudo apt-get install oracle-java8-installer
Mehran
  • 308
  • 3
  • 15
4

PPA method no longer works.

While Oracle Java 6 and 7 are not supported for quite a while, they were still available for download on Oracle's website until recently.

However, the binaries were removed about 10 days ago (?), so the Oracle Java (JDK) 6 and 7 installers available in the WebUpd8 Oracle Java PPA no longer work.

Oracle Java 6 and 7 are now only available for those with an Oracle Support account (which is not free), so I can't support this for the PPA packages.

Source : http://www.webupd8.org/2017/06/why-oracle-java-7-and-6-installers-no.html Dated : June 2017

Oracle's download page says

Updates for Java SE 7 released after April 2015, and updates for Java SE 6 released after April 2013 are only available to Oracle Customers through My Oracle Support (requires support login).

Java SE Advanced offers users commercial features, access to critical bug fixes, security fixes, and general maintenance".

I had to download it from Oracle archives - http://www.oracle.com/technetwork/java/javase/downloads/java-archive-downloads-javase7-521261.html

You need an account for this though.

Community
  • 1
  • 1
Aniket Thakur
  • 66,731
  • 38
  • 279
  • 289
  • This is correct, however, I got it fully working with Abhijit Bashetti answer: https://stackoverflow.com/a/28874505/2161698 – SebaGra Jan 29 '18 at 20:35
3

Oracle as well as modern versions of Ubuntu have moved to newer versions of Java. The default for Ubuntu 20.04 is OpenJDK 11 which is good enough for most purposes.

If you really need it for running legacy programs, OpenJDK 8 is also available for Ubuntu 20.04 from the official repositories.

If you really need exactly Java 7, the best bet as of 2020 is to download a Zulu distribution. The easiest to install if you have root privileges is the .DEB version, otherwise download the .ZIP one.

https://www.azul.com/downloads/?version=java-7-lts&os=ubuntu&architecture=x86-64-bit&package=jdk#download-openjdk

Thorbjørn Ravn Andersen
  • 73,784
  • 33
  • 194
  • 347
2

Oracle Java 1.7.0 from .deb packages

wget https://raw.github.com/flexiondotorg/oab-java6/master/oab-java.sh
chmod +x oab-java.sh
sudo ./oab-java.sh -7
sudo apt-get update
sudo sudo apt-get install oracle-java7-jdk oracle-java7-fonts oracle-java7-source 
sudo apt-get dist-upgrade

Workaround for 1.7.0_51

There is an Issue 123 currently in OAB and a pull request

Here is the patched vesion:

wget https://raw.github.com/ladios/oab-java6/master/oab-java.sh
chmod +x oab-java.sh
sudo ./oab-java.sh -7
sudo apt-get update
sudo sudo apt-get install oracle-java7-jdk oracle-java7-fonts oracle-java7-source 
sudo apt-get dist-upgrade
Paweł Prażak
  • 3,091
  • 1
  • 27
  • 42
2

flup's answer is the best but it did not work for me completely. I had to do the following as well to get it working:

  1. export JAVA_HOME=/usr/lib/jvm/java-7-oracle/jre/
  2. chmod 777 on the folder
  3. ./gradlew build - Building Hibernate
Community
  • 1
  • 1
Siddharth
  • 9,349
  • 16
  • 86
  • 148
-1
Open Applicaction -> Accessories -> Terminal

Type commandline as below...

sudo apt-get install openjdk-7-jdk

Type commandline as below...

apt-cache search jdk

(Note: openjdk-7-jdk is symbolically used here. You can choose the JDK version as per your requirement.)

For "JAVA_HOME" (Environment Variable) type command as shown below, in "Terminal" using your installation path...

export JAVA_HOME=/usr/lib/jvm/java-7-openjdk

(Note: "/usr/lib/jvm/java-7-openjdk" is symbolically used here just for demostration. You should use your path as per your installation.)

For "PATH" (Environment Variable) type command as shown below, in "Terminal" using your installation path...

export PATH=$PATH:/usr/lib/jvm/java-7-openjdk/bin

(Note: "/usr/lib/jvm/java-7-openjdk" is symbolically used here just for demostration. You should use your path as per your installation.)

Check for "open jdk" installation, just type command in "Terminal" as shown below

javac -version
Akitha_MJ
  • 3,882
  • 25
  • 20