How to run Java program

Now, we will learn how can write and execute a simple JAVA program. Before the execution of any java program, you need to understand the requirements of the JAVA. After that, we will learn how to run a Java program. Whenever anyone starts the learning of java, he/she starts from the java hello world program. Actually it’s not a mandatory thing in java but it is the simplest program in java. We will also start with hello world in java

Requirement for Program

1. Install the JDK in your system if you don’t have installed it. You can download the JDK and
install it.
2. Set path of the jdk/bin directory.

You can take reference from YouTube or google by simply search “how to set path variables of java in the system”.After the completion of requirements. Now you can start with the JAVA program. The process of Java programming is divided into three steps:
1. Write the JAVA code in any text editor like notepad. Now save the file with JAVA extension.
For example HelloWorld.java
2. Now compile the java program. Compile it you have written “javac HelloWorld.java” in
command prompt (cmd) on Windows.
3. Execute the program by typing “java HelloWorld”.

1. The first step (Creation of JAVA program)

class HelloJavaProgram
{  
    public static void main(String args[])
    {  
     	System.out.println("Hello Java Program");  
    }  
} 

It’s a simple java program with a class name HelloJavaProgram. It is containing some keywords we will discuss later. Now save this file as HelloJavaProgram.java

2. Second Step (Compile JAVA program)

To compile a java program, we must follow some syntax in the command prompt (cmd).

Syntax: javac filename(with its extension)
Example: javac HelloJavaProgram.java

NOTE: If you get an error when you try to compile the program: “javac’ is not recognized as an internal or external command, operable program or batch file “.
Then you need to set the path before compilation because this error occurs when java path is not set in your system.
After the compilation of the Java program, java compiler converted the source code into byte code.
JAVA compiler translated the .java file into the .class file(byte code). The compiler will create the same file with a different extension. This is how the java compiler works

You can see the byte code file (FirstJavaProgram.java) in your system at the same location where your source file (FirstJavaProgram.class) was saved.

java hello world program

3. The third step (Execution)

After compilation of the .java file now you can run the program. To compile a java program, we must follow some syntax in the command prompt (cmd).

Syntax: java filename (without its extension)
Example: java HelloJavaProgram

NOTE: While running the program then you should not append the .java extension to the file name.

A detailed discussion of FirstJavaProgram: FirstJavaProgram has two primary components: class definition and the main method. We used many keywords in an example like class, public, static, etc.

NOTE: Keyword is predefined in JAVA libraries and it should not be changed, we have to use it as it is.

Class definition

Every Java application must have at least one class. We can declare a class by class keyword followed by the class name. In this example, we used the class keyword followed by FirstJavaProgram. Here FirstJavaProgram is an identifier. All the code of the program will be placed between the curly brace.

main method

Every JAVA application contains one main method. The main method contains many keywords, let’s break it down to understand it:

public

This makes the main method public so that JVM can call it from anywhere. It should be public.

static

JVM doesn’t need to create an object to call the main method. Because the static method is accessible through the class.

void

The void is the return type of this method, the main method doesn’t return anything.

main

Its entry point of JVM to run java application. It is the main method.

String[] args

It is a command-line argument we can provide those values from the command prompt. We can discuss this later.

System.out.println(“First Java Program”):

It is used to print the text. Where the System is a class. out is the object of class and println is a method of System class. This method prints the contents inside the double-quotes. We will discuss it later.

Leave a Comment