Java

How to run a Java Program

Are you a beginner, and don’t know how to create your first project/program in java? Nothing to worry about! We will assist you in creating, compiling, and running a program in java. JDK’s installation on your windows operating system will make it very simple to compile and run a program in java. But before running a program in java, please make sure that Java is installed on your PC and the path is set appropriately on your Windows operating system.

This write-up will provide a detailed understanding of the below-given concepts:

  • How to create a simple program in java.
  • How to compile a program in java.
  • How to run a program in java.

Before heading toward the java coding, we recommend you to visit the following link to understand the basic syntax of the java programming language.

How to create a simple program in java

Let’s create a very basic java program to print a greeting message. Type the below-given command on any text editor like notepad, notepad++, or any IDE like NetBeans, Eclipse, etc.

public class SimpleJavaProgram {
  public static void main(String[] args){
    System.out.println("Hello! Welcome to linuxhint");
  }
}

Save the program as “SimpleJavaProgram” and provide the extension “.java”. Must remember the “public class name” and “filename” should be the same. Otherwise, you will encounter an error.

How to compile a program in java

Now, let’s move a step further to learn how to compile a java program.

First Step
To do so, open the windows command prompt and type the below mentioned command to enter the directory where the java program is saved:

cd Java Program

Here “Java Program” is the directory name where our java program is located.

Pressing the enter button will guide you to the specified directory.

Second Step
Now, type the below command to compile the java program:

javac SimpleJavaProgram.java

Type the above command and press the enter button, if the cursor moves to the next line without showing any error, it means you have successfully compiled your java program as shown in the below snippet:

The cursor moves to the next line to show that our program is compiled successfully.

How to run a program in java

Type the below-given command on the windows command prompt to run a java program:

java SimpleJavaProgram

Following will be the output:

This is how a program can be compiled and run through a command prompt in the Windows operating system.

Conclusion

We can write the code for our java program on any text editor like notepad, notepad++, etc. or any IDE like NetBeans, Eclipse, etc. To compile a java program, type “javac fileName.java” on the windows command prompt while to run the java program, type “java fileName” on the command prompt and press enter. This write-up explained how to create, compile, and run a program in java.

About the author

Anees Asghar

I am a self-motivated IT professional having more than one year of industry experience in technical writing. I am passionate about writing on the topics related to web development.