How to create your first Java program in Debian 10

If you are not familiar with Java programming in the Debian operating system, this article will help you write and compile the first Java program. To do this, you need the Java runtime 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, compile it to create the class, and then run it to run the Java program.

We ran the commands and procedures mentioned in this article on a Debian 10 system.

Java installation

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

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

We will install these programs on our system using the terminal application from the command line. To open the terminal, go to the “Actions” tab in the upper left corner of the desktop. Then find the Terminal application by entering the appropriate keyword in the search bar. From the results, click on the terminal icon to open.

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

$ sudo apt update

Then run the following command to check if the Java runtime is installed on your system or not.

$ java –version

If you get output similar to the one below, it means that Java is installed on your system.

Otherwise, if you get the output “command not found” as follows, it means that Java is not installed on your system.

Java command not found

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

$ apt install default-jre

Install Java Runtime Environment

System can provide you Y / N option to continue the installation. Hit Y and then To come in to continue, after that the Java Runtime Environment (JRE) will be installed on your system. To check, check the JRE version again by running the java –version command.

The next step is to check whether the Java development kit is installed on your system by running the following command in Terminal:

$ javac –version

If it is installed on your system, you will get output similar to the following:

Check Java Compiler Version

However, if you get a “command not found” as follows, this means that the JDK is not installed on your system.

JDK not installed

To install the Java development kit (JDK) on your system, run the following command in the terminal:

$ sudo apt install default-jdk

Install Java Development Kit (JDK)

System can provide you Y / N option to continue the installation. Hit Y and then To come in to continue, after that a Java Development Kit (JDK) will be installed on your system.

Now you can verify your JDK installation by running the javac –version command.

Your first Java program

Before you start writing a Java program, it is best to create a separate directory for all your Java programs. Here I create such a directory called “myjava_directory” in the home directory using the Mkdir command as follows.

$ mkdir myjava_directory

Now go to this directory using the cd command:

$ cd myjava_directory

Create java home directory

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

$ sudo gedit filename.java

Replace “file name” with any desired file name. Here we create a file called “sample.java”.

$ sudo gedit sample.java

Now add the following lines of code to 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 main program that simply prints “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 will be:

$ javac sample.java

The java compiler will create a class that you can verify with the ls command.

Compile Java Program

Now run the executed program using the following syntax:

$ java sample

Run your first Java program

After reading this article, I hope you now have a general idea of ​​how to create a basic Java program on a Debian system, as well as how to install the Java runtime and Java Development Kit used to compile and run a Java program.

How to create your first Java program in Debian 10

Related Posts