How to Create Your First Java Program on Ubuntu Linux
Introduction
Java is a popular, versatile programming language widely used for web development, desktop applications, and more. Ubuntu Linux is a stable and highly customizable open-source operating system that offers powerful features and excellent security. This article will teach you how to create your first Java program on Ubuntu Linux.
1.Installing Java Development Kit (JDK)
To start developing in Java, you will need to install the JDK (Java Development Kit) on your Ubuntu system. Open the terminal (Ctrl+Alt+T) and execute the following commands:
“`bash
sudo apt update
sudo apt install openjdk-11-jdk
“`
This will install OpenJDK 11, a free and open-source implementation of the Java Platform.
2.Verify Your JDK Installation
Once the JDK is installed, verify the installation by checking the version of the compiler:
“`bash
javac -version
“`
If successful, this command will display the installed version of ‘javac,’ indicating that the installation is complete.
3.Start Writing Your Java Program
Now that you have the JDK installed, let’s create a new directory where you can write your first Java program.
“`bash
mkdir java_project
cd java_project
“`
Create a new file named ‘HelloWorld.java’:
“`bash
touch HelloWorld.java
“`
Open ‘HelloWorld.java’ in your preferred text editor, such as nano or gedit:
“`bash
nano HelloWorld.java
“`
4.Write Your Java Code
Inside ‘HelloWorld.java’, insert this basic “Hello, World!” code:
“`java
public class HelloWorld {
public static void main(String[] args) {
System.out.println(“Hello, World!”);
}
}
“`
Save and exit the text editor.
5.Compile Your Java Program
Now it’s time to compile your code into an executable .class file. Use the following command in your terminal:
“`bash
javac HelloWorld.java
“`
You will now have a ‘HelloWorld.class’ file in your java_project directory. This means that your Java program has been successfully compiled.
6.Run the Compiled Java Program
Finally, execute the compiled Java program with the following command:
“`bash
java HelloWorld
“`
The output should display:
“`txt
Hello, World!
“`
Congratulations! You have successfully created, compiled, and executed your first Java program on Ubuntu Linux. You can now dive into learning more about Java programming and exploring its capabilities further.