How to create your first Java program in Debian 10

s2 0

If you are unfamiliar with Java programming in the Debian operating system, this article will guide you to write and compile your first Java program. For this you need the Java Runtime Environment and the Java Development Kit. We’ll explain how to install these programs from the command line. The steps to run a Java program include writing the program in a text editor such as Nano, Vim, or Gedit. Then compile to create a class and then run to run the Java program.

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

Java installation

We need the following two programs to run a simple 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 entering the appropriate keyword in the search bar. In the results, click the Terminal icon to open it.

Now first update the apt repositories using the following command in the terminal:

$ sudo apt update

Next, run the following command to check whether or not the Java Runtime Environment is installed on your system.

$ java –version

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

Otherwise, if you get “Command not found” output like this, it means that Java is not installed on your system.

Run the following command to install the Java Runtime Environment on your system:

$ apt install default-jre

Install Java runtime environment

The system may give you a Yes No Option to continue the installation. Blow and and then Enter to continue, after that the Java Runtime Environment (JRE) will be installed on your system. Check the JRE version again to verify by running the java –version command.advertising

The next step is to check whether or not the Java Development Kit is installed on your system by running the following command in the terminal:

$ javac –version

With it installed in your system, you will get a T output similar to the following:

Check the version of the Java compiler

However, if you get a “Command not found” message like this, it means that the JDK is not installed on your system.

JDK not installed

To install the Java Development Kit (JDK) in your system, run the following command in the terminal:

$ sudo apt install default-jdk

Install the Java Development Kit (JDK)

The system may give you a Yes No Option to continue the installation. Blow and and then Enter to continue, after that the Java Development Kit (JDK) will be installed on your system.

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

Your first Java program

Before you start writing a Java program, it is best to create a separate directory for all of your Java-related programs. Here I create such a directory called “myjava_directory” under the home directory with the Mkdir Command as follows.

$ mkdir myjava_directory

Now navigate to this directory with the CD Command:

$ cd myjava_directory

Create a Java home directory

Here we will now write our first Java program with Gedit. You can use any text editor to do this. To create a new Java file with Gedit, run the following command in the terminal:

$ sudo gedit filename.java

Replace the “filename” with any of your desired filenames. Here we create the file with the name “sample.java”.

$ sudo gedit sample.java

Now paste the following lines of code into 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 a Java program

This is the basic program that simply says “Hello! This is my first Java program “on your screen. After writing the program, compile it with the Java compiler using the following syntax:

$ javac filename.java

In our example it would be:

$ javac sample.java

The Java compiler creates a class that you can check with ls Command.

Compile Java program

Now run the compiled program with the following syntax:

$ java sample

Run your first Java program

After reading this article, I hope that you now have a basic understanding of how to create a basic Java program on a Debian system and how to install the Java Runtime Environment and the Java Development Kit, which is necessary to compile and run of the Java program is used.

How to create your first Java program in Debian 10