Compiling A Simple Java Program Using Command Prompt (JDK 1.7.0_02)

Compiling a simple java program requires a few steps. After searching many forums and websites, I was finally able to compile my first simple program using Windows 7 Command Prompt.

Note: This tutorial is for beginners. If you are an experienced programmer, this would be peanuts for you.
You will need to download and install the following:

I.  Java:

II. Eclipse or any other Integrated Development Environment (IDE) of your choice:

III. Java Development Kit (JDK) :

public class UsingCMD {
    public static void main(String[] args) {
        System.out.println("Let's learn Java!");

Obviously, I can easily run the code above inside Eclipse. But I decided to take on what seemed like a challenge for the first time and compile the program using Windows Command Prompt. Let’s get on with it:

You need to edit a variable name called “Path” in “Environment Variables” in Windows. I was not able to compile and run the program from command prompt until I edited the environment variables as you will see in the steps below.
On the other hand, Oracle says it’s optional:

You can run the JDK without setting the PATH environment variable, or you can optionally set it so that you can conveniently run the JDK executable files (javac.exe, java.exe, javadoc.exe, and so forth) from any directory without having to type the full path of the command. If you do not set the PATH variable, you need to specify the full path to the executable file every time you run it, such as:
C:\> “C:\Program Files\Java\jdk1.7.0\bin\javac”

It is useful to set the PATH variable permanently so it will persist after rebooting. Source

1- Go to Control Panel – System and Security:

2- System:

3- Advanced System Settings (top left side):

4- A “System Properties” dialogue box will appear. In the “Advanced” tab, click “Environment Variables…”:

5- Under “System variables” section, look for the variable named “Path”. Click on it and click “Edit…”:

6- When the following appears, press the “Home” key using your keyboard:

7- At this point, the typing cursor should appear to the left hand side of the text, like so:

8- Now copy the path of your JDK’s bin directory followed by a semi-colon ;
The default path is C:\Program Files\Java\jdk1.7.0_02\bin.
Your JDK version could be different from this, don’t worry about it.

So mine would go like this:
C:\Program Files\Java\jdk1.7.0_02\bin;C:\Program Files (x86)\….
Notice the semi-colon and no spaces in between. The semi-colon separates different paths.

Click OK on all three windows. You’re done editing the environment variable.

9- Fire up command prompt from the start menu by typing cmd into the search box:

10- Type in or copy/paste your program’s source code path. That is, where your .java file resides. In my case, it resides in H:\ so I only need to type “H:”

**Note that if you are navigating to a specific folder, you need to use the command “cd” which is short for “change directory”. For example, in order to tell Command Prompt to be in the path
D:\Users\YourName\MyFolder , please type:

cd D:\Users\YourName\MyFolder
and press Enter

Just to make sure that this is clear. If your source code file resides in the root of a local disk or flash drive, etc, then you only need to type the letter of that drive in Command Prompt, for example, D:

**Also note: In Command Prompt, you cannot use Ctrl+V to paste text. Instead, right-click and choose “Paste”.

Let’s continue:

11- Notice how the path changed from C:\Windows\system32 to H:\ as shown below:

12- Now you can execute the compiler javac. This in turn will create a file called UsingCMD.class which contains the bytecode. Type in javac and press enter:

13- Nothing seems to happen once you press Enter. Actually something does happen, but you can’t see it. As I mentioned in step 12, a .class file with the name UsingCMD.class has been created in the same location of the source code.
In my case, in H:\

See the UsingCMD.class file that has been “silently” created. Go to the directory and you will see it:

14- Finally, we want to run the program. Type java UsingCMD and press enter:

15- You will get this:

Congratulations! You have compiled and ran your program successfully.


  • Edit environment variables
  • Change path in Command Prompt
  • Execute the compiler javac
  • Run the program
  • Optional: Delete .class file that was created by the compiler.

Kindly let me know if there’s anything I can improve in this tutorial. I hope all the steps and instructions are clear.

Thank you for reading.



One thought on “Compiling A Simple Java Program Using Command Prompt (JDK 1.7.0_02)

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s