Compile and Run Java program with cmd

Download and install the latest version of the Java Platform, Standard Edition Development Kit (Java SE 6 Update 27). Note the installation directory for later—probably something like C:\Program Files\Java\jdk1.6.0_27\bin.

To make sure that Windows can find the Java compiler and interpreter,

  • Select Start -> Computer -> System Properties -> Advanced system settings -> Environment Variables -> System variables -> PATH.

    [ In Vista, select Start -> My Computer -> Properties -> Advanced -> Environment Variables -> System variables -> PATH. ]

    [ In Windows XP, Select Start -> Control Panel -> System -> Advanced -> Environment Variables -> System variables -> PATH. ]

  • Prepend C:\Program Files\Java\jdk1.6.0_27\bin; to the beginning of the PATH variable

    Compile Your Program

You will use the command to convert your Java program into a form more amenable for execution on a computer.

  • From the command prompt, navigate to the directory containing your .java files, say C:\introcs\hello, by typing the cd command, given below:

    C:\Users\username>cd c:\introcs\hello
    C:\introcs\hello\>

  • Assuming the file, say HelloWorld.java, is in the current working directory, type the command in boldface below it to compile it.

    C:\introcs\hello\>javac HelloWorld.java
    C:\introcs\hello\>

    If everything went well, you should see no error messages.

  • No error messages.

    Execute the Program

You will use the Java command to execute your program.

  • From the Command Prompt, type the Java command, given below:

    C:\introcs\hello\>java HelloWorld
    Hello, World

    If all goes well, you should see the output of the program, as given below:

    Hello, World.
Ebook Download
View all
Learn
View all