Writing Java Program and Testing in Command Line

This is the first tutorial for beginners in Java Programming language and you will learn to write Java programs in notepad. You will also learn to compile and test your first Java Program.

Writing Java Program and Testing in Command Line

Writing Java Program and Testing in Command Line: Java Hello World

This is the first program that you should make in Java after installing Java/JDK on your windows desktop. First of all you have to learn to write, compile and run the first Java Program. Hello World is a popular program that you should learn to run in Java. This program is very simple and prints 'Hello, World!' message on the console when executed. This program will give you the first feeling of Java Programming Language. So, let's get started and develop our first Java Hello World Program.

What you need to develop first Hello World Java program?

  • Java/JDK: First of all you should have Java installed on your system. In the previous section we learned How to Install Java/JDK 22 on Windows 10/11. So, you should check the Java Installation section if you don't have Java SDK installed.
     
  • Code Editor: If you are using windows then 'notepad' is the simple text editor that you can use for writing code for the 'Hello World' Java program.
     
  • Terminal to compile and run program: In windows cmd terminal can be used for compiling and running the program.

You should open the cmd in Windows 10/11 and check if Java is installed by running following command:

java --version

If this command displays the version of Java then you are all set to write and test your Java program:

C:\Users\ujjawal>java --version
openjdk 17.0.2 2022-01-18
OpenJDK Runtime Environment (build 17.0.2+8-86)
OpenJDK 64-Bit Server VM (build 17.0.2+8-86, mixed mode, sharing)

If you are getting the above output then check our tutorial Installing Oracle JDK/Java 22 on Windows 10/11 and install Java/JDK on your machine and come back again to this tutorial.

We have also recorded the video instruction for Writing Java Program and Testing in Command Line. Check the video:

Step 1: Writing Java code for Hello World program

First of all open the notepad and write following code in the notepad editor:

// Writing Java Program and testing in the command line

public class HelloWorld{

  public static void main(String [] args){
	System.out.println("Hello, World!");
   }

}

Screen shot of the notepad:

Java Hello World Code

Now save this file as "HelloWorld.java" as shown below:

Save hello world java file in notepad

Please that you have to put the name inside the double quote: "HelloWorld.java". And finally click on the "Save"  button. This will save your code as HelloWorld.java. Now we are done with the coding and proceed with the steps to run the program.

Step 2: Compile HelloWorld.java program

Java code is first compiled into class file, which is a byte code file used by the Java runtime to run the program. So, your have to first compile HelloWorld.java file into HelloWorld.class file. To compile file we will use the javac.exe file which comes with the Java/JDK installer and can be access from the command line. Open the command line and run:

javac -version

If Java/JDK is installed and configured on your system then it should give output like this:

Java compiler version

To compile the HelloWorld.java use this command:

javac HelloWorld.java

Here is the output of the above command:

Compiling Java program in command line

In the above screen you can see the class file (HelloWorld.class) which generated after the compilation of HelloWorld.java file. The java compiler compiles Java code and generates byte code file (.class).

Step 3: Running class file

The next and final step is to run the compiled file with the help of java tool. The java tool instantiate the Java runtime and runs the class file specified in the command line. To run the HelloWorld Java program run following command on the windows terminal:

java HelloWorld

Here is the output of the above command:

Running Hello World Java Program in command line

You can see above program prints the "Hello, World!"  message on the console.

After completing this tutorial you should be well aware of writing code in Java, compiling and running a Java program using command line tool in Windows 10/11. Now the next step for you is to learn more in Java Programming Language. Check the Java programming tutorials for beginners index page

Resource: