how to compile java programs

Let’s learn how to compile java programs.

how to compile java programs

In this post we are going to write, compile and run java program step by step.

First step: open notepad and copy below java code.

public class CompileJavaProgram
{
   public static void main(String[] args)
   {
      System.out.println("hello world java");
   }
}


Save above file as CompileJavaProgram.java in your A or B or C or D drive.

The reason why the file name is “CompileJavaProgram” because file name should be same as class name.

Before going to step 2 we must set path in windows.

For that go to the folder where you have installed or saved on your system and locate “bin” directory and copy the path. Path will be like this,

C:\Program Files\Java\jdk-12.0.2\bin

Now open command prompt or cmd and copy above path and write it in cmd like this.

set path = C:\Program Files\Java\jdk-12.0.2\bin

Step two: compile java program. For this open command prompt or cmd on Windows.

Now enter below command and press enter button.

javac CompileJavaProgram.java

Here after compilation .java file gets translated to byte code or .class file

For more on byte code or .class file refer java overview.

Step four: Now let’s run above java program by typing below command.

java CompileJavaProgram

how to compile java programs