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:
- Open a command prompt window and go to the directory where you saved the java program. Assume it’s C:\.
- 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.
In order to open the command prompt in this location, hold down ↵ Shift button and right click on the window:
Now you need to check if Java is installed. Let’s compile java command line so type java -version:
If java is not installed you can install the Java Development Kit from their website.
Type javac MyFirstJavaProgram.java :
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:
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:
So the main purpose of the java compilation is to produce .class file of a program which the machine understands.
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
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.
As you see all the classes have been compiled:
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
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.