How do I compile and execute a Java class?
Author: Deron Eriksson
Description: This Java tutorial shows how to compile and execute a Java class.
Tutorial created using: Windows XP || JDK 1.6.0_10


With JavaSW, you write your human-readable source code in .java files. Before executing this code, you need to compile this code to bytecode, which is the machine-readable version of your code. This compiled version of your code takes the form of .class files.

The "javac" executable is the Java compiler. The "java" executable is the Java interpreter that runs the bytecode.

I'll use Notepad to create a Java class called 'Howdy'.

Creating a Java file using Notepad

The Howdy class will output a message to the console when it is executed.

Creating a Java class called 'Howdy' in Notepad

The Howdy.java class is shown here.

Howdy.java

class Howdy {
	public static void main(String [] args) {
		System.out.println("Hello there");
	}
}

After saving Howdy.java, in my command prompt window I compile the class via:

javac Howdy.java

Compiling Howdy.java produces the Howdy.class file. I execute this class via:

java Howdy

This produces the result:

Hello there

These actions are shown in the command prompt window below:

Compiling and executing the 'Howdy' Java class