Categories: DebianLinuxShell

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.

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

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

$ apt install default-jre

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:

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

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

$ sudo apt install default-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

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.

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.

Now run the complied program using the following syntax:

$ java sample

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.

Karim Buzdar

About the Author: Karim Buzdar holds a degree in telecommunication engineering and holds several sysadmin certifications. As an IT engineer and technical author, he writes for various web sites. You can reach Karim on LinkedIn

Recent Posts

How to Install Magento 2 on AlmaLinux

Magento is a free and open-source e-commerce platform written in PHP. It is simple, easy…

1 year ago

How to Install ISPConfig Hosting Control Panel with Apache Web Server on Ubuntu 24.04

ISPConfig is an open-source control panel that allows users to manage multiple servers from a…

1 year ago

How to Test your Email Server (SMTP) Using the Telnet Command

As a Linux administrator, you may find it necessary to troubleshoot or test your Simple…

1 year ago

Managing Network Interfaces and Settings on Ubuntu 24.04 with nmcli

Ubuntu 24.04, like many modern Linux distributions, relies on the NetworkManager for managing network connections.…

2 years ago

Using Restic Backup on Ubuntu 24.04

Restic is a modern, open-source backup program designed for efficiency, security, and simplicity. It enables…

2 years ago

Installing phpMyAdmin on Rocky Linux 9 and Securing it with Let’s Encrypt SSL

phpMyAdmin is a popular free tool written in PHP intended to administer MySQL and MariaDB…

2 years ago