Core Java

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.

How to Compile Java -
Fig: 1 path

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

How to Compile Java - Open command 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 :

How to Compile Java - Command prompt
Fig: 4 Command prompt

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

  • Syntax checking
  • Adding extra code
  • Converting source code to byte code i.e. from .java file to .class file

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

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

Want to know how to develop your skillset to become a Java Rockstar?

Join our newsletter to start rocking!

To get you started we give you our best selling eBooks for FREE!


1. JPA Mini Book

2. JVM Troubleshooting Guide

3. JUnit Tutorial for Unit Testing

4. Java Annotations Tutorial

5. Java Interview Questions

6. Spring Interview Questions

7. Android UI Design


and many more ....


Receive Java & Developer job alerts in your Area

I have read and agree to the terms & conditions


Firouzeh hejazi

A self-motivated and hard-working developer with more than 4 years of extensive experience in developing software in java platforms. A multi-skilled and problem-solving engineer who has participated in implementing a range of applications in different roles. Possess a MSc in Knowledge Engineering and Decision Science.
Notify of

This site uses Akismet to reduce spam. Learn how your comment data is processed.

1 Comment
Newest Most Voted
Inline Feedbacks
View all comments
1 year ago

Artboard Studio is a cloud-based design tool for freelancers, agencies, corporations, and small companies. It is a unique tool that allows users to design, collaborate, animate and present their projects using an extensive library of models and assets. Users do not need to download any software, everything is fine in the browser.

Back to top button