Android,iOS,Gadgets,Reviews Everything About Technology

Installing Java on Ubuntu using apt-get

131

Installing the JRE / JDK version in ubuntu by default

This is the simplest and, as a consequence, the recommended method, as a result of which we will get the installed OpenJDK6 if we have Ubuntu 12.04 or earlier versions or OpenJDK7 for ubuntu versions starting from 12.10. Everything is very simple. First, update the list of packages from apt-get

sudo apt-get update

Now let’s check if Java has been installed before

- Advertisement -

java -version

If, in response, we got something like “The java program was not found /, then it means that java is not installed, so you should run the following command

sudo apt-get install default-jre

After this, we get the installed Java runtime environment (JRE). If you need to compile Java applications (eg Apache Ant, Apache Maven, Eclipse, Intellij IDEA, etc.), then you will need the java development environment (JDK – java development kit). To do this, execute the command:

sudo apt-get install default-jdk

In this case, the basic process of installing java can be considered finished. All the following steps are necessary.

Installing OpenJDK7 (if necessary)

You can install the OpenJDK7 start environment using the following command:

sudo apt-get install openjdk-7-jre

If you need a development environment, then we perform:

sudo apt-get install openjdk-7-jdk

Installing Oracle JDK (if necessary)

Oracle JDK is the official version of java from Oracle, which, unfortunately, is no longer supplied by default with ubuntu. But you can also install it with apt-get. To do this, execute the following commands:

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

Further, depending on what version you need, execute one of the following commands:

Oracle JDK 6

Although this is an old version, but it is widely distributed:

sudo apt-get install oracle-java6-installer

Oracle JDK 7

This is the last stable version of the product:

sudo apt-get install oracle-java7-installer

Oracle JDK 8

This version is for developers only. Its official release is scheduled for March 2014. This article describes the features of this version.

sudo apt-get install oracle-java8-installer

Configuring Java (if necessary)

If you have multiple versions of Java installed on your system, you can choose one of them by default. To do this, run the following command:

sudo update-alternatives --config java

In response, you will receive the following message if you have two Java versions (if more are installed, of course, more versions will be defined):

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

Selection    Path                                            Priority   Status
------------------------------------------------------------
* 0            /usr/lib/jvm/java-7-oracle/jre/bin/java          1062      auto mode
1            /usr/lib/jvm/java-6-openjdk-amd64/jre/bin/java   1061      manual mode
2            /usr/lib/jvm/java-7-oracle/jre/bin/java          1062      manual mode

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

Now you can select the default version. You can do the same for the Java compiler:

sudo update-alternatives --config javac

To select the version of the Java compiler that you need, use the above command in the same way as for selecting the Java version. This command can be executed for all other programs that are part of Java, for example: keytool, javadoc, and jarsigner. And this list is far from complete.

Setting the variable “JAVA_HOME” in the operating system environment

To set the JAVA_HOME variable in the operating system environment, which is sometimes necessary for some programs, first find the installation path for your Java version:

sudo update-alternatives --config java

In response to the command you will see the following message:

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

Selection    Path                                            Priority   Status
------------------------------------------------------------
* 0            /usr/lib/jvm/java-7-oracle/jre/bin/java          1062      auto mode
1            /usr/lib/jvm/java-6-openjdk-amd64/jre/bin/java   1061      manual mode
2            /usr/lib/jvm/java-7-oracle/jre/bin/java          1062      manual mode

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

The installation path for each version:

/usr/lib/jvm/java-7-oracle
/usr/lib/jvm/java-6-openjdk-amd64
/usr/lib/jvm/java-7-oracle

Copy the path you need, and then edit the / etc / environment file:

sudo nano /etc/environment

In this file add the following line (replacing YOUR_PATH with the just copied line):

JAVA_HOME="YOUR_PATH"

This should be enough to set the variable in your operating system. Now restart the file:

source /etc/environment

Verify that everything is configured correctly by invoking the following command:

echo $JAVA_HOME

If you get the path you just created in response, the variable was set successfully. If not, please make sure that you have correctly performed all the steps described above.

Comments