Home » Core Java » How to Compile Java Program

About Firouzeh hejazi

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.

How to Compile Java Program

In this article we will see how to compile Java program. 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. 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
(No Ratings Yet)
Start the discussion Views Tweet it!

Do you want to know how to develop your skillset to become a Java Rockstar?

Subscribe to our newsletter to start Rocking right now!

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

 

Leave a Reply

avatar
  Subscribe  
Notify of