Java

Install OpenJDK 8 on Debian 9 Stretch

Java Development Kit or JDK is used by Java programmers all over the world. Java is a compiled language. But unlike C/C++, Java compiles Java source code into Java byte code. The JVM (Java Virtual Machine) then runs the Java byte code. Java byte code is not anything like C/C++ compiled binaries, which require recompilation when needed to run on different machines and operating systems. The Java byte code is the same no matter what operating system or machine you use to generate it. The only difference is the JVM.  The JVM is operating system and machine specific.  The JVM reads the Java byte code and converts it into machine specific codes and then runs it on that particular machine and operating system.

There are 2 implementations of JDK. One is called the Oracle JDK and the other one is OpenJDK. OpenJDK is totally open source and it does not include any proprietary component of Oracle JDK. The license of Oracle JDK and OpenJDK are not same either. Oracle JDK is more restrictive than OpenJDK. For those looking for a free and open source version of JDK, OpenJDK is the best choice for them. You won’t really see much of a difference when you run OpenJDK. The experience is almost the same. You can use exactly the same command line utilities with OpenJDK as with Oracle JDK.It is really easy to install OpenJDK 8 on Debian 9 Stretch because it is already available on the official repository of Debian 9 Stretch.

In this article, I will show you how to install OpenJDK 8 on Debian 9 Stretch. Let’s get started.

First update the package repository cache of your Debian 9 operating system with the following command:

$ sudo apt-get update

Your package repository cache should be updated.

Before you install OpenJDK 8, you must know one thing. There are two versions of OpenJDK 8 on Debian 9 Stretch official repository. One is OpenJDK 8 headless and the other one is OpenJDK 8.

The difference between OpenJDK 8 headless and OpenJDK 8 is that OpenJDK 8 headless does not install any libraries for working with graphical user interfaces. It has less dependencies. So it is perfect for headless servers where you never need any graphical user interfaces (GUIs). The OpenJDK 8 provides everything that OpenJDK 8 headless provides along with libraries that are required for working with graphical user interfaces (GUIs). In this article I will install OpenJDK 8 (not headless), but I will show you how to install the OpenJDK 8 headless version as well.

You can install OpenJDK 8 headless with the following command:

$ sudo apt-get install openjdk-8-jdk-headless

You can install OpenJDK 8 with the following command:

$ sudo apt-get install openjdk-8-jdk

Now press ‘y’ and then press <Enter> to continue.

The apt package manager should download and install all the required packages. It may take a while depending on your internet connection.

OpenJDK 8 should be installed shortly.

Once the installation is complete, run the following command to check if OpenJDK 8 is installed correctly.

$ javac -version

If you get similar output as shown in the screenshot below, everything should be working correctly.

Now I am going to run a simple Hello World Java program and show you that the OpenJDK 8 that I installed can indeed compile Java code.

This is the code that I am going to run.

I saved the ‘HelloWorld.java’ Java source code file to ‘$HOME/Documents/codes/’ directory as you can see from the output of ‘ls’ command.

Now you can compile the ‘HelloWorld.java’ Java source file with the following command:

$ javac HelloWorld.java

The ‘HelloWorld.java’ Java source file should be compiled. You can run the ‘ls’ command now and you should be able to see a ‘HelloWorld.class’ file as shown in the screenshot below. This is the Java byte code that I was talking about.

Now you can run the Java byte code ‘HelloWorld.class’ with the following command:

$ java HelloWorld

NOTE: Don’t include the ‘.class’ extension when you run a Java byte code file. ‘java HelloWorld.class’ is not how you should run a Java program.

You should see “Welcome to LinuxHint!” printed on the screen as shown in the screenshot below. So we can compile and run Java codes with OpenJDK 8.

This is how you install OpenJDK 8 on Debian 9 Stretch. Thanks for reading this article. Tweet us at @linuxhint for comments.

About the author

Shahriar Shovon

Freelancer & Linux System Administrator. Also loves Web API development with Node.js and JavaScript. I was born in Bangladesh. I am currently studying Electronics and Communication Engineering at Khulna University of Engineering & Technology (KUET), one of the demanding public engineering universities of Bangladesh.