How to compile and run java program

Let’s learn how to compile and run java program.

How to compile and run java program

In this post we are going to learn how to run java program in command prompt using notepad. To execute java program in cmd you need to have java installed in your system. Click here to install latest version of java.

Step one: open text editor like notepad on windows, copy below java code and paste it on text editor.

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

Step two: save above file as CompileJavaProgram.java. The reason why the file name is “CompileJavaProgram” because file name should be same as class name.

Step three: compile java program. For this open command prompt or cmd on Windows. Now enter below command and press enter button.

javac CompileJavaProgram.java

Here javac command reads source files written in the Java programming language, and compiles them into bytecode class files.(Source – oracle).

Set path(Windows): Before going to next step we must set path in windows. Open command prompt and go to the folder where you have installed java on your system and locate “bin” directory and copy complete path and paste the path like this,

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

NOTE: my java version is 12.0.2 installed on my laptop. You may install different version.

Step four: Here after compilation .java file gets translated to byte code or .class file. For more on byte code or .class file refer java overview.

Now we can run java program in cmd using notepad or how to compile java program in cmd. Type the below command and hit enter.

java CompileJavaProgram

How to compile and run java program