How to Compile Java Programs through Windows CMD

Although many people compile Java programs within the IDE, the Windows command line terminal is a powerful compiling option.

When starting out with Java, many users get confused about compiling the code. Frequently, a programing IDE adds additional layers of confusion and complexity that complicates debugging and learning. Learning how to compile directly from within the Windows shell is an essential skill to master. These steps will walk you through installing the Java SDK and compiling your code from within the Windows terminal. These commands are case-sensitive.

In this screencast I walkthrough the required steps. Refer to the text and images below for more specific details.

1. First things first, you need to download the JDK from Oracle to have the latest version of Java.

Java JDK download

2. Follow the instructions to install the JDK. It is important to make note of the path of the JDK install.

JDK Path

3.Create a central directory to hold all your Java files. For example, I created a folder at C:\java and placed my .java files and projects within this folder.

Java Folder

4. Click the Windows Start icon and search for System and hit enter. Select Advanced system settings on the left and then select Environment Variables.

Windows Environment Variables

5. Under System variables scroll down to the variable Path.

Windows 7 System Path Variable

6. Hit Edit at the beginning of the Variable value. We need to place the path of the Java compiler into Windows’s path. The compiler should be located in the JDK bin folder. For example, from the install I recorded that my bin folder was at the following location: C:\Program Files (x86)\Java\jdk1.7.0_06\bin; but your location may be different. Be sure to include a semi-colon at the end of the string you just added. Hit OK and close this.

If this is done incorrectly, you will get the following error when attempting to compile:

‘javac’ is not recognized as an internal or external command, operable program or batch file

7. Next, we need to open a terminal window or CMD shell. Click the Windows icon and search for “CMD” then hit enter. Change to the directory of your personal java files. For example, I would input cd \java since that is the personal java folder I created above. Now our current directory within the CMD shell is c:\java.

Java CD CMD

8. Type in javac JavaFileName.java where the “JavaFileName.java” is the name of the java file you want to compile. This file can be created with any text editor or IDE and contains your actual code. After hitting the enter key, you should see a new blank line in the CMD with nothing in it Now check within your personal Java folder. If the compile was successful, you should see a new .class file.

9. To actually run the interpreter and see output type in java YourFileName without the .java extension and your program should execute. My example simply outputs some text.

 

About Chris Luongo

Chris is a self-taught web designer and developer out of Atlanta, GA. This geek enjoys coffee, cold brews, bike riding, and twisting any form of tech into submission.
View more articles by Chris Luongo

The Conversation

Follow the reactions below and share your own thoughts.

3 Responses to “How to Compile Java Programs through Windows CMD”

  1. January 22, 2013 at 9:31 pm, MohMoh said:

    I want to know about to insert the checkpoint in java program for use system failure
    checkpoint is used in fault recovery system and save the state of java program time periodly.
    please, help and give advise to me.

    Reply

  2. May 18, 2013 at 4:21 am, Jb said:

    This was helpful thanks a bunch

    Reply

  3. July 23, 2013 at 5:04 am, Arun S.R said:

    Thank you so much.

    Reply

Leave a Reply