by

How to Create your First Java Program in Debian 10

If you are unfamiliar with Java programming in the Debian operating system, this article will guide you towards writing and compiling the first Java program. For this purpose, you will require the Java Runtime Environment and the Java Development Kit. We will explain the installation of these programs through the command line. The steps to run a Java program include writing the program in a text editor such as Nano, Vim, or Gedit. After that compiling it to create a class and then executing it in order to run the Java program.

We have run the commands and procedures mentioned in this article on Debian 10 system.

Java Installation

We will need the following two programs in order to run a basic Java program:

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

We will install these programs in our system using the command-line Terminal application. To open the Terminal, go to the Activities tab in the top left corner of your desktop. Then search for the Terminal application by typing the relevant keyword in the search bar. From the results, click on the Terminal icon to open.

Now first update the apt repositories through the following command in Terminal:

$ sudo apt update

Next, run the following command to verify if the Java Runtime Environment is installed in your system or not.

$ java –version

If you receive the output similar to below output, it implies Java is installed in your system.

Check java version

Otherwise, if you receive the output “command not found” as follows, it implies Java is not installed in your system.

Java command not found

To install the Java Runtime Environment on your system, run the following command:

$ apt install default-jre

Install Java Runtime Environment

The system might provide you with a Y/n option in order to continue the installation. Hit y and then Enter to continue, after that Java Runtime Environment (JRE) will be installed on your system. TO verify, again check the JRE version by running the “java –version” command.

The next stpe is to check if the Java development kit is installed in your system or not by running the following command in Terminal:

$ javac –version

If it installed in your system, you will receive tee output similar to below:

Check the version of java Compiler

However, If you receive the “command not found” as follows, it implies the JDK is not installed in your system.

JDK Not installed

In order to install the Java development kit (JDK) in your system, run the following command in Terminal:

$ sudo apt install default-jdk

Install java Development Kit (JDK)

The system might provide you with a Y/n option in order to continue the installation. Hit y and then Enter to continue, after that Java development kit (JDK) will be installed in your system.

Now you can verify the installation of JDK by running the “javac –version” command.

Your First Java Program

Before starting to write a Java program, the best approach is to make a separate directory for all of your Java-related programs. Here, I am creating such a directory named “myjava_directory” under the Home directory using the Mkdir command as follows.

$ mkdir myjava_directory

Now navigate to this directory using the cd command:

$ cd myjava_directory

Create java Home directory

Now here, we will write our first Java program using the Gedit. You can use any Text editor for this purpose. To create a new Java file using the Gedit, run the following command in terminal:

$ sudo gedit filename.java

Replace the “filename” with any of your desired file name. Here, we are creating the file with name “sample.java”.

$ sudo gedit sample.java

Now add the following lines of code in your file.

class MyFirstProgram {

public static void main(String args[]){

System.out.println("Hello! This is my first java program");

}

}

Now save and close the file.

Create java Program

This is the basic program that will simply print “Hello! This is my first java program” on your screen. After writing the program, compile it using the java compiler using the following syntax:

$ javac filename.java

In our example, it would be:

$ javac sample.java

The java compiler will create a class which you can verify using the ls command.

Compile Java program

Now run the complied program using the following syntax:

$ java sample

Run your first Java program

After reading this article, I hope now you have a basic understanding of how to create a basic Java program in a Debian system and also how to install Java Runtime Environment and the Java Development Kit used for compiling and running the java program.

Comment

Leave a Reply

Your email address will not be published. Required fields are marked *

News Feed