How to Compile and Run your Java Program in Command Line

In this tutorial, we are going to see how to write, compile and run a java program. Compiling and running a java program are very easy tasks to perform after installing JDK. Here are the steps:

Java program:
public class MyClass 
  public static void main(String[] args)
    System.out.println("Welcome To StackHowTo!");


Welcome To StackHowTo!

How to compile and run the above program
Prerequisite: You must install Java on your system. You can get it on this link.

Step 1: Open a text editor, like Notepad on Windows, and TextEdit on Mac. Copy the above program and paste it into the text editor.

You can also use an IDE like Eclipse to run the java program but we will see this part in the next tutorials. To keep things simple, we’ll only be using the text editor and the command prompt (or terminal) for this tutorial.

Step 2: Save the file as You might be wondering why we named the file as MyClass, the point is, we always have to name the file the same as the name of the public class. In our program, the name of the public class is MyClass, that’s why our filename must be

Step 3: In this step, we’ll compile the program. To do this, open the Command Prompt (CMD) in Windows, if you are on Mac OS, open Terminal.

To compile the program, type the following command and press Enter.



You may get this error when trying to compile the program: “javac is not recognized as internal or external command”. This error occurs when the java path is not defined in your system.

If you get this error, you must first set the path before compilation.

Define the path in Windows:

Open Command Prompt (CMD), go to where you installed java on your system, and locate the bin directory.

Copy the full path and write it in the command line like this.

set path=C:\Program Files\Java\jre1.8.0_121\bin

Note: your jdk version may be different. Since java version 1.8.0_121 is installed on my system, I used the same when setting the path.


Define the path in Mac OS X:

Open the Terminal, type the following command and hit return.

export JAVA_HOME=/Library/Java/Home

Type the below command on the terminal to confirm the path.

The above steps are for setting up the temporary path, which means when you close the command prompt or terminal, the path settings will be lost and you will have to set the path again the next time you use it. In this tutorial you will find the configuration of the permanent path.

Step 4: After compilation, the .java file is translated to the .class file (byte code). We can now run the program. To run the program, type the below command and press Enter:

java MyClass

You will be able to see the result displayed on the output.

Note that you should not add the .java extension to the file name when running the program.
mcqMCQPractice competitive and technical Multiple Choice Questions and Answers (MCQs) with simple and logical explanations to prepare for tests and interviews.Read More

Leave a Reply

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