Java is one of the most widely used programming languages thanks to its great versatility, portability and its ability to create robust and scalable applications. It is an object-oriented language with a large number of libraries and tools that facilitate the development of applications, whether desktop, web or mobile.
In this manual we will show you how to create your first program using Java 11 in Ubuntu 22.04 or Debian 11, where we will show the text "Hello World!" through the console of our Operating System.
The first step will be to install the development environment OpenJDK 11 to be able to compile and execute our applications made in Java.
sudo apt install openjdk-11-jdk-headless -y
Once the installation is finished, you must create the file where we will write the program code. In our case we will call it HelloWorld.java. You can create the file with the following command:
touch HelloWorld.java
The next step is to open the file with your favorite editor. We will do it with the nano editor:
nano HelloWorld.java
The program code will be as follows:
// Tu primer programa en Java 11
class HelloWorld {
public static void main(String[] args) {
System.out.println("Hello World!");
}
}
ATTENTION It is very important that the name of the class (line 2 of the program code) is the same as the name of the file (without the extension ".java").
Once the file is saved, we must compile our program with the following command:
javac HelloWorld.java
And finally we can run it:
java HelloWorld
We will see on our console screen the text Hello World!. This means that the program has been executed correctly.