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

Programming & Frameworks

Topics Covered
  • C Programming and Data Structures (41 Blogs)
  • Comprehensive Java Course (2 Blogs)
  • Java/J2EE and SOA (324 Blogs)
  • Spring Framework (8 Blogs)
SEE MORE

MI-new-launch

myMock Interview Service for Real Tech Jobs

myMock-widget-banner-bg

How to Compile and Run your first Java Program?

Published on Jul 19,2019 635 Views
3 / 4 Blog from Environment Setup

MI-new-launch

myMock Interview Service for Real Tech Jobs

myMock-mobile-banner-bg

myMock Interview Service for Real Tech Jobs

  • Mock interview in latest tech domains i.e JAVA, AI, DEVOPS,etc
  • Get interviewed by leading tech experts
  • Real time assessment report and video recording

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.

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.

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.

Comments
0 Comments

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.