how to compile and execute java program

In most of the programming languages, the program converts to the machine code either by using a compiler or interpreter. The machine code is machine-dependent, which can only run on the generated machine rather than other machine. In this article, we learn how to compile and execute java program. As java is a platform independent programming language, the java compiler doesn’t convert the source code to machine code. There are two stages to execute the java program. These stages include compiler and java virtual machine (JVM) explained below.

Compile and Execute java program.


The Java compiler (javac) is a command-line tool that reads java source code file and compiles it into executable java bytecode. The bytecode gets store in disk with the file extension .class. The bytecode is machine independent that we can run it on any machine with the help of JVM. TO compile the program, we must run the java compiler javac with the command-line name of the source file.

Command line

Compilation of a java program


Java uses both compiler and interpreter. The generated bytecode is interpreted to machine instructions for specific machine. The bytecode generated by the compiler is not machine specific. It generates for java virtual machine that exists only inside the computer memory.

Java virtual machine has three stages as below

Class Loader

Java Class Loader use to load the classes into JVM at run time. We also can load the classes from different resources. In other words, java virtual machine perform the linking process at runtime. The class loaded in the JVM loads the bytecode of .class to the machine at run time according to the need. The class depends on the another class in the program also can loaded through the class loader.

class demo = loadClass(String className, boolean resolveIt);
// className is a name of the class to be loaded//
//resolveIt  decide whether any reference class should be loaded or not//

Bytecode verifier

After the class loader in the JVM loads the bytecode of .class. The bytecode verifier, checks if the bytecode loaded is valid and the instructions are not performing damaging actions.

The following are some of the checks carried out:

  • Variables are initialized before they are used.
  • Method calls match the types of object references.
  • Rules for accessing private data and methods are not violated.
  • Local variable accesses fall within the runtime stack.
  • The run time stack does not overflow.

Just-in-time Compiler

This is the final stage of java program execution. In this stage the Just-in-time compiler converts the loaded bytecode into the machine code. When using a JIT compiler the hardware can execute the machine code as opposed to having the JVM interpret the same sequence of bytecode repeatedly and incurring the penalty of relatively lengthy translation process. this can lead to performance gains in the execution speed unless methods are executed less frequently

Execution of java program


What is variable in JavaScript?

How to define constant in java?

What is the Console in JavaScript.


2 thoughts on “how to compile and execute java program”

Leave a Comment