Setup Java and VS Code on Ubuntu

hadyrashwan

Hady Rashwan

Posted on August 7, 2020

Setup Java and VS Code  on Ubuntu

Settings up your development environment for Java on Ubuntu is straight forward. We are going to install the Java SDK and VS code with the Java extension then create a simple Hello World app.

Find the available versions of Java JDK

First, we will search for the available packages to install. I'm looking for version 11, so my search will be.



$ sudo apt search openjdk-11


Enter fullscreen mode Exit fullscreen mode

You will get similar to the following.



Sorting... Done
Full Text Search... Done
default-jdk/focal 2:1.11-72 amd64
  Standard Java or Java compatible Development Kit

openjdk-11-dbg/focal-security,focal-updates 11.0.8+10-0ubuntu1~20.04 amd64
  Java runtime based on OpenJDK (debugging symbols)

openjdk-11-demo/focal-security,focal-updates 11.0.8+10-0ubuntu1~20.04 amd64
  Java runtime based on OpenJDK (demos and examples)

openjdk-11-doc/focal-security,focal-security,focal-updates,focal-updates 11.0.8+10-0ubuntu1~20.04 all
  OpenJDK Development Kit (JDK) documentation

openjdk-11-jdk/focal-security,focal-updates 11.0.8+10-0ubuntu1~20.04 amd64
  OpenJDK Development Kit (JDK)

openjdk-11-jdk-headless/focal-security,focal-updates 11.0.8+10-0ubuntu1~20.04 amd64
  OpenJDK Development Kit (JDK) (headless)

openjdk-11-jre/focal-security,focal-updates,now 11.0.8+10-0ubuntu1~20.04 amd64 [installed,automatic]
  OpenJDK Java runtime, using Hotspot JIT

openjdk-11-jre-dcevm/focal 11.0.3+1-1 amd64
  Alternative VM for OpenJDK 11 with enhanced class redefinition

uwsgi-plugin-servlet-openjdk-11/focal 2.0.18-11ubuntu1 amd64
  JWSGI plugin for uWSGI (OpenJDK 11)



Enter fullscreen mode Exit fullscreen mode
  • You can change the number 11 and look for other versions of Java.

  • As you can see openjdk-11-jdk is available. So let's install it.

Install Java JDK

Now we will install the Java JDK using one command.



$ sudo apt install openjdk-11-jdk


Enter fullscreen mode Exit fullscreen mode

Verify that Java installation using the following.



$ sudo java -version


Enter fullscreen mode Exit fullscreen mode

The output should be similar to.



openjdk version "11.0.1" 2020-04-14
OpenJDK Runtime Environment (build 11.0.1+7-Ubuntu-1ubuntu1)
OpenJDK 64-Bit Server VM (build 11.0.1+7-Ubuntu-1ubuntu1, mixed mode, sharing)



Enter fullscreen mode Exit fullscreen mode

Install VS code

You can use the following command to do so.



sudo snap install --classic code


Enter fullscreen mode Exit fullscreen mode

If snap is not available, you can use apt by following this post.

Install Java Extension Pack

Launch VS Code Quick Open (Ctrl+P), paste the following command, and press enter.



ext install vscjava.vscode-java-pack


Enter fullscreen mode Exit fullscreen mode

Run A Hello World App

1 - Open VS code.
2 - Create a file called Hello.java.
3 - Paste the following code. And save it.



public class Hello {

    public static void main(String[] args) {

        System.out.println("Hello World!");

    }


}


Enter fullscreen mode Exit fullscreen mode

4- Open the Debug tab and click on Run and Debug.

Debug Tab

5- The output in the terminal should be.
Hello World!

Credit

This post was based on the information from the linuxconfig post .

💖 💪 🙅 🚩
hadyrashwan
Hady Rashwan

Posted on August 7, 2020

Join Our Newsletter. No Spam, Only the good stuff.

Sign up to receive the latest update from our blog.

Related