cft
Become a CreatorSign inGet Started

How to Compile and Run a Java Program

A Java compiler is software that takes a source code of Java written by a developer as input and then translates the entire source code into a platform-independent Java file(also known as .class file which you will study in detail further in this article) which can be used to execute the program. Java compilers include the Java Programming Language Compiler (javac), the Eclipse Compiler for Java (ECJ)


user

Ankit Dixit

4 months ago | 6 min read
Follow

compile-run-java-program-taapg

Introduction:

Dear reader, We know most of you are able to write Java code efficiently but have you ever wondered how Java compiles and runs a program internally?

Compiler

Before jumping into the compilation and execution process of java we first need to understand what compilers are.

As we all know a computer cannot understand the source code and can only execute the code in binary form therefore we need to translate the source code to machine code which can be done by a compiler.

So What is a Compiler(Java)?

A Java compiler is software that takes a source code of Java written by a developer as input and then translates the entire source code into a platform-independent Java file(also known as .class file which you will study in detail further in this article) which can be used to execute the program. Java compilers include the Java Programming Language Compiler (javac), the Eclipse Compiler for Java (ECJ)(if you are interested more in knowing about different compilers then this link contains a list of different Java compilers).

Here’s a brief demonstration of how the compilation process works:-

How to Compile a Java Program?

  • For compiling and executing a java program first, you need to install an IDE on your device. Also whenever you install an IDE such as eclipse you have to install JDK along with it.
  • Now create a source file and let’s name it as First.Java

Remember:- It is always recommended to give your Java source files the same name as the class they contain because when Java source code is compiled, every class has its own output file named after the class.

  • To compile a java program, execute the compiler, javac, specifying the name of the source file on the command line like this:- C:\>javac First.java 
  • Before moving on, give it a try….Did you observe anything?

You might be getting this error:-

This error occurs when the path variable is not set in environment variables.

So to avoid this error first you have to copy the path of JDK/bin folder. After that go to environment variables and inside system variables paste the copied path in the path variable.

You will get a lot of articles and videos regarding how to set path variables but do you know why are we doing so?

This is because when you want to compile your source code you have to use a command called javac classname. But how will windows know what this javac is?

When you save your file in the Java directory i.e where java is installed by you, it's not necessary to set a path. But if you are saving your file outside the directory, it is fairly necessary to set the path beforehand as Windows doesn't know where this javac is located. If we write this path variable in environment variables then Windows will provide this variable accessible to every program running on the operating system. So being inside one folder you can access the contents of some other folder if the path of that folder is present as an environment variable.

If you want to avoid all the hassle you can directly use this compiler by InterviewBit.

To know more about our compiler you can run this simple java code by yourself and check the output:-

class Main {

public static void main(String args[]) {

System.out.println("Hello, Coders! Welcome to interview bit");

}

}

You can check the code link from here.

Now it's time to give you an overview of  Components in Java.

So there are basically 3 components in Java:-

  1. JDK(Java Development Kit)
  2. JRE(Java Runtime Environment)
  3. JVM(Java Virtual Machine)

Many of you must be familiar with these three words and their theoretical definition but when asked in an interview can you explain their roles individually as well as when combined together?

Don’t panic, this article is a solution for all your doubts.

So let’s begin with the heart of java i.e Java Virtual Machine(JVM),

Java Virtual Machine(JVM)

  • JVM stands for Java Virtual Machine which provides a runtime environment for the execution of bytecode. Jvm is actually responsible for running or executing your code. 

Bytecode is an intermediate code during the process of compilation containing a highly optimized set of instructions designed to be executed by the Java Virtual Machine (JVM). (We will discuss it further in this article again so that you can get acquainted with it).

  • Java works on Write Once Run Anywhere principle and JVM is responsible for making this possible by making Java platform independent.

Remember:-Jvm makes Java platform-independent but JVM individually is platform-dependent.

  • Each operating system has a different JVM but the output produced after execution of bytecode is the same across all operating systems which shows the platform independence of java.
  • Due to JVM no matter what the platform is the programmer can run his code on any platform such as Linux, Windows, Mac, etc.(The detailed working of JVM is discussed later in this article.)

Java Runtime Environment(JRE)

  • Once you have compiled the program you have to execute it and for the execution of program, JRE is used.
  • JRE is nothing but JVM+class libraries which are the bare minimum for running a Java application.
  • These libraries include deployment technologies like Java Web Start(The Java Web Start software allows you to download and run Java applications from the web), some base libraries like lang, util, etc. So if you just want to run Java programs, but not develop them, JRE is what you need.

Java Development Kit(JDK)

  • Java Development Kit(JDK) is a component that comes into picture when you want to develop a java application or a java program.
  • JDK is simply a combination of JRE+some development tools such as compiler, debugger.
  • Therefore, when JDK is installed by you, JRE comes bundled with it.

Java Virtual Machine(JVM)

Basically, we have to look at two things:-

1)Developing java programs2)Executing them

For developing java programs JDK is used and for executing them we require Java Virtual Machine(JVM) which is a part of JRE.So our java programs are running inside JRE but they are actually executed by JVM.

How JDK Compile and Run Java Code?

Now that we have a fair idea about the components of Java, let’s discuss Java compiling process.

Observe the flow diagram carefully, after that we will discuss each and every step carefully.

Java is a hybrid language which means it is compiled as well as interpreted so both compiler, as well as, the interpreter take part in the execution process of a java program.

  • The first step is to compile your java source code using javac command (javac is the primary java compiler included in JDK) or directly click on run if you are using the InterviewBit compiler.
  •  The compiler will read this source code and check for any errors. After that, it will generate a .class file which will have byte code. For execution, a class file is given as input to JVM and JVM is responsible to load and execute the file.
  • As discussed earlier Bytecode is an intermediate code during the process of compilation containing a highly optimized set of instructions designed to be executed by the Java Virtual Machine (JVM). This byte code is interpreted by JVM(Java Virtual Machine) so byte code is not an executable program but it's an error-free program.
  • After compilation .class files are generated corresponding to every .java file that you had.
  • This class file should come inside the memory because only then it can be executed by the CPU.So the process of bringing a class file in the memory is called loading and in Java, since everything is written inside a class this process is called class loading and it is performed by the class loader.

Note that not all class files are loaded at once, these are loaded as and when required by JVM.

  • After the bytecode verifier checks for errors in the compiled Java code and ensures that your code works according to Java standards then it's sent to the interpreter.
  • Now CPU will start executing the program.

You might be wondering, can CPU understand the byte code?The answer is NO! Therefore byte code should be converted to machine code which is done by an interpreter or JIT(Just In Time) compiler.So the question arises: what's this JIT compiler?

JIT(Just In Time) Compiler

JIT compiler is part of the JVM due to which selected portions of bytecode are compiled into executable code.JIT compiler is called only in particular situations for eg:- whenever there is redundant code present in the program the JIT compiler removes it from the bytecode and sends only the non-redundant code to the interpreter thus making the process faster.

So this was the complete compilation and execution process of a Java Program in Detail.

Hope you all liked the article and understood all the concepts.

Upvote


user
Created by

Ankit Dixit

Follow

Software Engineer

I am a Software Developer and I love to share programming knowledge.


people
Post

Upvote

Downvote

Comment

Bookmark

Share


Related Articles