How to Compile and Run your first Java Program?

Last updated on Aug 27,2024 7.4K Views

How to Compile and Run your first Java Program?

edureka.co

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. In this blog, we will discuss how to write, compile and run a java program.

The first step is to create a folder, create a Java Class and write a Java Program. When we write a Java program, 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.

An important step after installing Java into the system is to set a path. You can refer to this How to set a path in Java?’ article to know the exact procedure. 

Let’s create a simple java program. 

Create a java file as HelloWorld.java

public class HelloWorld {
	public static void main(String args[]) {
		System.out.println("Hello World");
	}
}

To compile this program type the command shown below on your command prompt and press enter.

javac HelloWorld.java

This runs javac.exe, the compiler. The generalized command to compile any Java program.

javac <Java_file_name>.java


Once you hit enter, the HelloWorld .class file will be generated. You will find both HelloTesters.java and HelloTesters.class among the files in your working directory.

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

Related Learning: Java Interview Questions for 5 Years Experience

So, when I 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.

Note: Java requires each class placed in its own source file must be same as class name with extension 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

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

With this, we come to an end of this article on the Java Compilation process. I hope you understood how to compile a java program and are clear about each and every aspect that I have discussed above.

Related Article: Top 30 Java Pattern Programs

Check out the Java Training by Edureka, a trusted online learning company with a network of more than 250,000 satisfied learners spread across the globe. We are here to help you with every step of your journey; we have come up with a curriculum that is designed for students and professionals who want to be a Java Developer.

If you want to start a career in the Node JS Field then check out the Node JS Training Course by Edureka, a trusted online learning company with a network of more than 250,000 satisfied learners spread across the globe.

Got a question for us? Please mention it in the comments section of this “how to compile java program” article and we will get back to you as soon as possible.

Upcoming Batches For Java Course Online
Course NameDateDetails
Java Course Online

Class Starts on 1st March,2025

1st March

SAT&SUN (Weekend Batch)
View Details
BROWSE COURSES
REGISTER FOR FREE WEBINAR Building Robust Applications with Spring Framework