Java/J2EE and SOA (348 Blogs) Become a Certified Professional
AWS Global Infrastructure

Programming & Frameworks

Topics Covered
  • C Programming and Data Structures (16 Blogs)
  • Comprehensive Java Course (4 Blogs)
  • Java/J2EE and SOA (345 Blogs)
  • Spring Framework (8 Blogs)
SEE MORE

How to Compile and Run your first Java Program?

Last updated on Oct 05,2023 7.1K Views

3 / 4 Blog from Environment Setup

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

helloworld- how to compile java program - Edureka

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.

helloworld file- how to compile java program - Edureka

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

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.

java code- how to compile java program - Edureka

compilation process - how to compile java program - Edureka

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 on your journey, for becoming a besides this java interview questions, we come up with a curriculum which 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 Certification Training Course
Course NameDateDetails
Java Certification Training Course

Class Starts on 27th April,2024

27th April

SAT&SUN (Weekend Batch)
View Details
Java Certification Training Course

Class Starts on 18th May,2024

18th May

SAT&SUN (Weekend Batch)
View Details
Comments
0 Comments

Join the discussion

Browse Categories

webinar REGISTER FOR FREE WEBINAR
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP

Subscribe to our Newsletter, and get personalized recommendations.

image not found!
image not found!

How to Compile and Run your first Java Program?

edureka.co