How to Compile and Run Java Programs

When JDK is installed on your machine, it provides two commands for you to compile and run Java programs.

javac” – Compiles a Java program stored a file named with the program class name.

java -cp . class_name” – Runs a compiled Java program. “-cp .” specifies the current directory as the class path.

Let’s try these commands with a very simple Java program. Use NotePad to enter the following Java program into a file called

class Hello {

public static void main(String[] a) {

System.out.println("Hello world!");



Then compile this program in a command window with the “javac” command:

To execute the program, use the java command:
C:\>\progra~1\java\jdk1.6.0_02\bin\java Hello

Hello world!

Congratulations, you have successfully entered, compiled and executed your first Java program.

About the Author

Akash Padhiyar

Visit Website


  1. mental_rotation August 25, 2010

    I would like to exchange links with your site
    Is this possible?

  2. Author

Leave a Comment

Your email address will not be published. Required fields are marked *


Time limit is exhausted. Please reload CAPTCHA.