How to Install Java 8 on Debian GNU/Linux 9 (Stretch)

Posted on : June 19, 2017 , Last Updated on : June 19, 2017 By
| Reply More

debian 9 install javaIn this article, I will go through the process of installation of Java 8 on Debian 9 Stretch. Java comes in two flavors and we install both. OpenJDK is open source version that is compatible with most software.  And Oracle JavaSE is the proprietary version that should be used if your java application is incompatible with OpenJDK. There is also a neat mechanism for switching default Java in case you have both of them installed. In one of our earlier article we have explained java installation on rpm based system and this time we will have on Debian.

Installing OpenJDK

We will start from installing the OpenJDK. There are two packages that we can install, a JRE and JDK. Those familiar with Java already know what this means, but for other lets mention what it means.  JRE means Java Runtime Environment and JDK means Java Development Kit. So the first one is used if you are going only to start Java program, while JDK packs a javac compiler. Meaning if you are developer, you need JDK, otherwise JRE is ok.

To install JRE use this command after getting root access with su:

apt-get install default-jre

Depending on what kind of Debian install you did (CD, DVD, USB drive), this might or might not be already installed. On the other hand, JDK is most probably not installed. To install it, type:

apt-get install default-jdk

And you are set to go.

Installing Oracle Java

To install Oracle java we can use Webupd repository that is made for Ubuntu, but it also works for Debian. To add repository you first need to add add-apt-repository command if it is not installed (and that depends upon install options you selected).

apt-get install software-properties-common dirmngr

After installing this, we can add repository

add-apt-repository "deb http://ppa.launchpad.net/webupd8team/java/ubuntu yakkety main"

Repository is still not ready to use, we need to add key, we will see what key is missing after we run update command

apt update

Next we add key:

apt-key adv --keyserver keyserver.ubuntu.com --recv-keys C2518248EEA14886

And now we can install Oracle Java 8

apt-get install oracle-java8-installer

This will start install process, it will download newest Java 8 from Oracle site and install it. You will also get prompted by ncurses environment to accept the Oracle EULA for Java.

After it is done, you can run following command to see which exact version you have installed

java -version

Should get output similar to this

java version "1.8.0_131"
Java(TM) SE Runtime Environment (build 1.8.0_131-b11)
Java HotSpot(TM) 64-Bit Server VM (build 25.131-b11, mixed mode)

If we need Java 9 (development version as of time of writing) or Java 7 or 6 (legacy versions), we can install those as well. We need to re-run above command, except we will change number 8 for 6, 7 or 9.

Managing which Java will be using by default

Of you installed Oracle Java 6, 7, 8 and 9, plus OpenJDK, then you have 5 java installations. If you are developing for the legacy environment, you would want to set your system to use only one Java version, and to do that you dont need to remove all other java versions. Instead, you need to use following command:

update-alternatives --config java

It will give you output similar to this

Here you only need to enter the number that is shown before Java entry that you want to use as default. The asterisk signifies which one is used currently. After you changed the java alternative, the java -version command should give you different output.

You could also select default java compiler with following command

update-alternatives --config javac

Any other tool like javaws can be also set simply by changing last word in this command to appropriate tool name.

Setting the Environment Variable

If you are using some Java Application server like JBoss or Tomcat, then you will need $JAVA_HOME variable set. To set it use above update-alternatives --config java command to see where java is installed.

Then edit following file in nano

nano /etc/environment

If you just installed Java, file is empty, so add some line like this

JAVA_HOME="/usr/lib/jvm/java-8-openjdk-amd64"

That is if you want OpenJDK to be your $JAVA_HOME.

Save file and run source command to load new variable

source /etc/environment

Now check if it works

echo $JAVA_HOME

it should give you output like this to this

/usr/lib/jvm/java-8-openjdk-amd64

That means that environment variable has been successfully set.

Conclusion

We have successfully installed OpenJDK and Oracle Java SE and now have the environment set up so you can on top of it install some Java EE application servers like Red Hat JBoss, Oracle WebLogic, WildFly or Apache Tomcat. On top of that you can develop and deploy your application. Or you simply want to have plain Java SE desktop applications on your Debian 9 desktop. Either way,  now you can do it.

Filed Under : DEBIAN

Tagged With :

Free Linux Ebook to Download

Leave a Reply

All comments are subject to moderation.