Ubuntu Java Development

If you are new to Java Programming in the Ubuntu operating system, this simple tutorial will guide you along in writing and compiling your first Java Hello World program. The applications that you need for this purpose include the Java Runtime Environment and the Java Development Kit. This article covers the installation of these two through the Ubuntu command line. You can then write your first program in a text editor such as gedit, nano or even some graphical text writing tool. You will then compile your program in order to create a class that you can then execute in order to run your Java program.

We have run the commands and procedures mentioned in this article on an Ubuntu 18.04 LTS system.

Java Installation

In order to compile and run a basic Java program, you need to have the following two software installed on your Ubuntu system:

  • Java Runtime Environment (JRE)
  • Java Development Kit (JDK)

Let us first open the Ubuntu command line, the Terminal, in order to install these two. You can open the Terminal either through the system Dash or the Ctrl+alt+T shortcut.

Since we will be installing Java through the apt utility, first let us update our apt repositories through the following command:

$ sudo apt update

The next thing is to run the following command:

$ java -version

This will ensure either Java Runtime Environment is already installed on your system or not. If yes, it will also let you know which version of Java you have on your system.

In my case, the output shows that I do not have Java installed on my system yet.

Java version

In order to install the Java Runtime Environment on your system, run the following command as root:

$ apt install default-jre

Install Java default JRE

The system will prompt a Y/n option for you to continue installation. Please enter Y to continue, after which JRE will be installed on your system.

You can then check the installed version of Java as follows:

Check Java version again

After installing the JRE, let us check if we have the Java Development Kit installed on our system or not. This can be done by checking the version of your Java Compiler, javac, as follows:

$ javac -version

javac version

The above output shows that I need to install the Java compiler or the JDK on my system.

You can install it through the following command as root:

$ sudo apt install default-jdk

Install Java JDK

The system will prompt a Y/n option for you to continue installation. Please enter Y to continue, after which JDK will be installed on your system.

You can then check the installed version of the Java Compiler as follows:

Check java compiler version again

The basic installations you need to have before running a Java program are now complete.

Your First Java Program

Before starting to write Java programs, it is best practice to make a dedicated directory for all your Java related work. I am creating such a directory through the following command in my home folder:

$ mkdir MyJavaDirectory

Then move to that directory as follows:

$ cd MyJavaDirectory

Create project directory

The next step is to write your first Java program. You can write it in any of your favorite text editors. Here, I am using the gedit editor to write the program. Run the following command in order to open a new java file in the gedit editor:

$ gedit “filename”.java

Example:

$ gedit MyFirstProgram.java

Open new Java source code file

Then copy the following code in your file:

class MyFirstProgram {

    public static void main(String args[]){
        System.out.println("Hello World!");
    }
}

Java program codeThis program is simply intended to print “Hello World” on your screen.

Save the file and close it.

Then is the time to compile your first program through the Java compiler as follows:

$ javac “filename”.java

Example:

$ javac MyFirstProgram.java

The ls command will show that the compiler will create a class based on the class in your Java code:

Compile java sourcecode

In order to run the compiled program, run the following program:

$ java filename

Example:

$ java MyFirstProgram

Running Japa program

In this article, you have learned to install both the Java Runtime Environment and the Java Development Kit used to compile and run your java programs. We also wrote a simple Hello World program in Java and ran to see if you and your system are ready to move to the more complex world of Java Programming.

Your First Java Program in the Ubuntu Terminal