DEV Community

Cover image for Installing Java on Ubuntu 20.04 [ Easy Way ]
G Sudarshan
G Sudarshan

Posted on • Edited on

Installing Java on Ubuntu 20.04 [ Easy Way ]

In this guide, we will describe how to install Java on Ubuntu 20.04.

Alt Text

Installing OpenJDK 11

Open Terminal, As always first check for updates

sudo apt-get update
Enter fullscreen mode Exit fullscreen mode

Then type following command in terminal

sudo apt install openjdk-11-jdk
Enter fullscreen mode Exit fullscreen mode

when asked for [ Y/n ], Type Y

Once the installation is complete, you can verify it by checking the Java version:

java -version
Enter fullscreen mode Exit fullscreen mode

The output should look something like this:

openjdk version "11.0.7" 2020-04-14
OpenJDK Runtime Environment (build 11.0.7+10-post-Ubuntu-3ubuntu1)
OpenJDK 64-Bit Server VM (build 11.0.7+10-post-Ubuntu-3ubuntu1, mixed mode, sharing)
Enter fullscreen mode Exit fullscreen mode

JAVA_HOME Environment Variable

The JAVA_HOME environment variable is used by some Java applications to determine the Java installation location.

To set the JAVA_HOME variable, first find the Java installation path with update-alternatives:

sudo update-alternatives --config java
Enter fullscreen mode Exit fullscreen mode

In this example, the installation paths are as follows:

  • OpenJDK 11 is located at /usr/lib/jvm/java-11-openjdk-amd64/bin/java

Once you found the path of your preferred Java installation, open the /etc/environment file:

sudo nano /etc/environment
Enter fullscreen mode Exit fullscreen mode

Assuming you want to set JAVA_HOME to point to OpenJDK 11, add the following line, at the end of the file:

/etc/environment

JAVA_HOME="/usr/lib/jvm/java-11-openjdk-amd64"
Copy

For changes to take effect on your current shell you can either log out and log in or run the following [source](https://linuxize.com/post/bash-source-command/) command:

source /etc/environment
Enter fullscreen mode Exit fullscreen mode

Verify that the JAVA_HOME environment variable was correctly set:

echo $JAVA_HOME
Enter fullscreen mode Exit fullscreen mode

You should see the path to the Java installation:

That’s it! At this point, you have successfully installed Java on your Ubuntu system!

Now for complete assurance

Open any text editor type following following code

class Simple{  
        public static void main(String args[]){  
         System.out.println("Hello Java");  
        }  
    }
Enter fullscreen mode Exit fullscreen mode

save this code as Simple.java

cd to the folder where the program is saved.

then compile using following command in terminal. ( remember S is capital in Simple )

javac Simple.java
Enter fullscreen mode Exit fullscreen mode

and then run using following command

java Simple
Enter fullscreen mode Exit fullscreen mode

If you are able to see this output ..... then your java is successfully installed on Ubuntu!

Hello Java
Enter fullscreen mode Exit fullscreen mode

Uninstalling Java

You can uninstall Java like any other package installed with apt .

For example, to uninstall the default-jdk package enter:

sudo apt remove openjdk-11-jdk
Enter fullscreen mode Exit fullscreen mode

Conclusion

OpenJDK 11 and OpenJDK 8 are available in the default Ubuntu 20.04 repositories and can be installed using the apt package manager.

GitHub: https://github.com/G-Sudarshan

LinkedIn: https://www.linkedin.com/in/g-sudarshan

Twitter: https://twitter.com/g_sudarshan11

Top comments (2)

Collapse
 
clavinjune profile image
Clavin June

I recommend using sdkman to install Java, it helps you maintaining multiple java version

Collapse
 
gsudarshan profile image
G Sudarshan

Awesome alternative✔️👍