Run and Compile

Steps to run and Compile a Java Program .

Step 1: Open a text editor, like Notepad on windows and TextEdit on Mac. Copy the above program and paste it in the text editor.

Step 2: Save the file as FirstJavaProgram.java. You may be wondering why we have named the file as FirstJavaProgram, the thing is that we should always name the file same as the public class name. In our program, the public class name is FirstJavaProgram, that's why our file name should be FirstJavaProgram.java.

Step 3: In this step, we will compile the program. For this, open command prompt (cmd) on Windows, if you are Mac OS then open Terminal.

To compile the program, type the following command and hit enter.

javac FirstJavaProgram.java

You may get this error when you try to compile the program: 'javac' is not recognized as an internal or external command, operable program or batch file. This error occurs when the java path is not set in your system.

If you get this error then you first need to set the path before compilation.

Set Path in Windows: Open command prompt (cmd), go to the place where you have installed java on your system and locate the bin directory, copy the complete path and write it in the command like this.

set path=C:Program FilesJavajdk1.8.0_121in

Note: Your jdk version may be different. Since I have java version 1.8.0_121 installed on my system, I mentioned the same while setting up the path.

Set Path in Mac OS X

Open Terminal, type the following command and hit return.

export JAVA_HOME=/Library/Java/Home

Type the following command on terminal to confirm the path.

echo $JAVA_HOME

That's it.

Example

The steps above are for setting up the path temporary which means when you close the command prompt or terminal, the path settings will be lost and you will have to set the path again next time you use it.

Step 4: After compilation the .java file gets translated into the .class file(byte code). Now we can run the program. To run the program, type the following command and hit enter:

java FirstJavaProgram

Note that you should not append the .java extension to the file name while running the program.

Closer look to the First Java Program

Now that we have understood how to run a java program, let have a closer look at the program we have written above.

public class FirstJavaProgram {

This is the first line of our java program. Every java application must have at least one class definition that consists of class keyword followed by class name. When I say keyword, it means that it should not be changed, we should use it as it is. However the class name can be anything.

I have made the class public by using public access modifier, I will cover access modifier in a separate post, all you need to know now that a java file can have any number of classes but it can have only one public class and the file name should be same as public class name.

class FirstJavaProgram {

public static void main(String[] args){

System.out.println("Hello World, This is my first program in Java");

}//End of main

}//End of FirstJavaProgram Class

Defines a method name main. Every java application must include the main method. This is the starting point of interpreter to be gin the execution og the program.  A java application can have any number of classes but only one of them must include main method to initiate the execution.

Public : The keyword public is an acces specifier that declares the main method as unprotected and there fore making it accessible to the other classes . This is similar to the C ++ public modifier.

Static : Next appears the keyword static which declares this method as one that belongs to the entire class and not a part if any objects of the class. The main must always be declared as static since the interpreter uses this method before any objects are created.

Void : The type modifier void states that the main method does not return any value .

Leave Comment

Important Topics

Title
Run and Compile
Hello World Program
User Input
Add Numbers
Sum of Two Numbers
Even Numbers
Odd numbers from 1 to n or 1 to 100
Even or Odd number
Average of 3 numbers
Fibonacci Series using loops
Generate random number
Largest of three Numbers
Decimal to octal conversion
Quotient and Remainder
Simple Interest
Compound Interest