Movatterモバイル変換


[0]ホーム

URL:


Skip to content
DEV Community
Log in Create account

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

sudoapt-get update
Enter fullscreen modeExit fullscreen mode

Then type following command in terminal

sudoaptinstallopenjdk-11-jdk
Enter fullscreen modeExit 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 modeExit fullscreen mode

The output should look something like this:

openjdk version"11.0.7" 2020-04-14OpenJDK 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 modeExit fullscreen mode

JAVA_HOME Environment Variable

TheJAVA_HOMEenvironment variable is used by some Java applications to determine the Java installation location.

To set theJAVA_HOME variable, first find the Java installation path withupdate-alternatives:

sudo update-alternatives --config java
Enter fullscreen modeExit 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 modeExit fullscreen mode

Assuming you want to setJAVA_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 modeExit fullscreen mode

Verify that theJAVA_HOME environment variable was correctly set:

echo $JAVA_HOME
Enter fullscreen modeExit 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

classSimple{publicstaticvoidmain(Stringargs[]){System.out.println("Hello Java");}}
Enter fullscreen modeExit 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 )

javacSimple.java
Enter fullscreen modeExit fullscreen mode

and then run using following command

javaSimple
Enter fullscreen modeExit fullscreen mode

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

Hello Java
Enter fullscreen modeExit fullscreen mode

Uninstalling Java

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

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

sudoapt remove openjdk-11-jdk
Enter fullscreen modeExit 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)

Subscribe
pic
Create template

Templates let you quickly answer FAQs or store snippets for re-use.

Dismiss
CollapseExpand
 
clavinjune profile image
Clavin June
a bunch of perceptrons
  • Location
    Jakarta, Indonesia
  • Joined

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

CollapseExpand
 
gsudarshan profile image
G Sudarshan
Software Engineer at PhonePe | PICT'23
  • Location
    Nashik
  • Joined

Awesome alternative✔️👍

Are you sure you want to hide this comment? It will become hidden in your post, but will still be visible via the comment'spermalink.

For further actions, you may consider blocking this person and/orreporting abuse

Software Engineer at PhonePe | PICT'23
  • Location
    Nashik
  • Joined

More fromG Sudarshan

DEV Community

We're a place where coders share, stay up-to-date and grow their careers.

Log in Create account

[8]ページ先頭

©2009-2025 Movatter.jp