In this lab, you will write, compile, and run your first Java program: "Hello, World!" The "Hello, World!" program is a simple program that displays the text "Hello, World!" on the screen. This is a common first step in learning any programming language. You will use the Nano editor in a Linux environment to write the Java code, compile it using the javac
compiler, and then execute the program using the java
command.
By the end of this lab, you will be able to:
class
definition and main
method.cd ~
nano HelloWorld.java
public class HelloWorld {
public static void main(String[] args) {
System.out.println("Hello, World!");
}
}
CTRL + O
, then hit Enter
to confirm the filename.CTRL + X
.javac
command:javac HelloWorld.java
HelloWorld.class
. You can check that the file exists by running:ls
HelloWorld.java
file and fix any mistakes.java
command:java HelloWorld
Hello, World!
HelloWorld.java
file again using Nano:nano HelloWorld.java
System.out.println()
statement, for example:System.out.println("Hello, Java Programming!");
javac
, and run the program again to see the new output:javac HelloWorld.java
java HelloWorld
HelloWorld.java
file:nano HelloWorld.java
// This program prints "Hello, World!" to the console.
public class HelloWorld {
public static void main(String[] args) {
System.out.println("Hello, World!");
}
}
System.out.println
):System.out.println("Hello, World!")
javac HelloWorld.java
HelloWorld.java
file:nano HelloWorld.java
HelloWorld
to HelloJava
and update the filename accordingly:public class HelloJava {
public static void main(String[] args) {
System.out.println("Hello, World!");
}
}
mv
command:mv HelloWorld.java HelloJava.java
javac HelloJava.java
java HelloJava
.class
files:rm *.class
In this lab, you learned how to create, edit, compile, and run a simple Java program in a Linux environment using Nano. You practiced using basic Java syntax, including the structure of a class and the main
method, which is the entry point for all Java applications. You also explored modifying code, working with comments, handling errors, and renaming classes, all of which are essential steps in developing more complex Java programs.