1.1.3 – Executing a java program from command prompt (Windows)

by subbu on December 5, 2018

In the previous sessions we have learned how to install and configure JDK on windows system. Now our computer is ready to compile and execute a java program. Here in this session, we will see how a simple java program can be compiled and executed.

Step 1: Let us first create our personal folder to save the java program under a particular folder. Here I am creating a folder called “progs” under d: local drive to write our first java program.

  • Double click on My Computer (My PC) to pen windows explorer
  • Select new folder option either by using “folder” option on ribbon tab or from popup menu by right click on empty area of explorer


Name the folder as “progs”


Step 2: Go to the command prompt by giving “cmd” command from run dialogue


By default the command prompt takes you to the user folder like “c:\Users\CodingFox>”. Now we need to change to our working folder that we have created as “Progs” under d:\

  • cd\ takes you to root of the same drive
  • d: takes you to the d: Local drive
  • cd Progs takes you to our working folder “d:\Progs”


Now we are in the working folder “d:\Progs”.

Step 3:  We need to write a java program using any text editor like notepad. To open the notepad just type “notepad” at the command prompt and press enter. It opens the notepad as a separate window

Type the following sample program; Make sure that the name of the class Demo must be capitalized

class Demo
  public static void main(String args[])
   System.out.println("Hello World");

Note: Here S in String, S in System and D in Demo must be written in capitals because java is a case sensitive language.


Step 4: Save the program with the name of the class “Demo” and with .java file extension.

Make sure that the program is saved under our working folder “d:\progs”.



Note: Here the name of class (Demo) and the name of program (Demo.java) must have the same name and both must be capitalized

Some times the file may be saved as Demo.java.txt. It may create problem in compiling. So, better place the file name in double quotes to make sure that the file is saved with .java extension but not as Demo.java.txt.

Step 5: Now minimize the notepad and go to the command prompt to compile the program.

Supply the program name (Demo.java) to the java compiler javac

The java compiler compiles the java program (Demo.java) and produces platform independent byte code and which is  saves with .class extension (Demo.class)

This we can see by issuing a DOS command dir


We can also see the byte code by opening the class file “Demo.class”


Note: Never try to modify the byte code generated by the compiler. So close the “Demo.class” file without modifying and saving.

Step 6: Now supply the name of class file (Demo) to the java executable java. It loads the class file and sends to the JVM. The JVM translates the byte code and produces native executable, which would be executed by the system run time.

Note: Only supply the class name (Demo) but not with the class extension (Demo.class)


Never expect execution of program successfully at first try, otherwise you will be disappointed. So expect problems and try to rectify them. After all experimenting is the best way of learning programming.

Previous post:

Next post: