How to Compile Java Program

In this article, we will see how to compile a Java program through the command line. In fact, High-level languages like Java, C, C++, etc. compile a program to its equivalent low-level code which can be understood and executed by the machine.

1. Requirements

First of all we should install JDK, then follow the below steps:

  1. Open a command prompt window and go to the directory where you saved the java program. Assume it’s C:\.
  2. javac (Java Compiler) translates the java source code to the bytecode i.e. .class file. Bytecode is machine language of the Java Virtual Machine (JVM). Bytecode is also referred to as the magic code of Java which is the platform-independent. Type ‘javac‘ and press enter. This runs javac.exe, the compiler. The generalized command to compile any Java program. If there are no errors in your code, the command prompt will take you to the next line.

The file is compiled and you can see MyFirstJavaProgram.class file generated in the same folder. Lets see Example01:
public class MyFirstJavaProgram {
    public static void main(String[] args) {
        System.out.println("This program has been compiled  and run!");

Now go to the directory where the project exits.

Fig: 1 path

In order to open the command prompt in this location, hold down Shift button and right click on the window:

Fig: 2 Open command window

Now you need to check if Java is installed. Let’s compile java command line so type java -version:

Fig: 3 Check java version

If java is not installed you can install the Java Development Kit from their website.

Type javac :

Fig: 4 Command prompt

When we compile java program using javac tool, generally java compiler performs below steps:

we can see the MyFirstJavaProgram.class file has been created:

Fig 5 : Compiled file

When we say compiler adds extra code at the time of compilation, for instance, if you have not written any constructor into your program then the compiler will add one default constructor to your program:

Fig 6 : Default constructor added

So the main purpose of the java compilation is to produce .class file of a program which the machine understands.

Fig 7 : Producing .class file by compiler

By the way you can use any editor to create your project file, such as NotePad, after using a text editor, save the program with a .java extension and follow all the steps above.

Now that you have compiled your program, you can run it by typing java

Fig 8 : Run the compiled program

When we start compiling the source code, each class is placed in its own .class file that contains bytecode. Suppose, if you want to compile multiple Java files at a time, then you can use below command:

javac *.java

Lets see Example02. We have 2 .java files in this example:

public class Coord {
    public int x;
    public int y;
    public Coord(int i, int j){
        this.x = i;
        this.y = j;
    public String toString(){
        return "(" + x + " , " + y + ")";
import java.util.ArrayList;
import java.util.List;
public class Main {
    public static void main(String[] args) {
        List coords = new ArrayList();
        coords.add(new Coord(2, 5));
        coords.add(new Coord(3, 4));
        for (int i=0; i < coords.size(); i++){

This command will convert all java files to .class file.

Fig 9 : Compile multiple .java files

As you see all the classes have been compiled:

Fig 10 : Compiled classes

We come to an end of this article. I hope you understood how to compile a java program and are clear about each and every aspect that I have discussed above.

2. Download the Source Code

You can download the full source code of this example here: How to Compile Java Program
Exit mobile version