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 MyFirstJavaProgram.java‘ 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:

MyFirstJavaProgram.java

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 - MyFirstJavaProgram.java
Fig: 1 MyFirstJavaProgram.java 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 MyFirstJavaProgram.java :

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 MyFirstJavaProgram.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:

Coord.java

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 + ")";
    }
}

Main.java

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++){
            System.out.println(coords.get(i).toString());
        }
    }
}

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

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

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.
Subscribe
Notify of
guest

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

1 Comment
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
uday
2 years 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