In this blog, we will cover the following points
- What is JVM, JRE & JDK
- Steps to run a java file
JVM (Java Virtual machine)
- JVM is responsible for converting Byte code to the machine-specific code.
- JVM is also platform-dependent and provides core java functions like memory management, garbage collection, security, etc.
JRE (Java Runtime Environment)
- Java Runtime Environment is the implementation of JVM, it provides platform to execute java programs.
- JRE consists of JVM and java binaries and other classes to execute any program successfully.
- JRE doesn’t contain any development tools like java compiler, debugger etc.
- If you want to execute any java program, you should have JRE installed but we don’t need JDK for running any java program.
- We can also say JRE is for running the java programs.
JDK (Java Development Kit)
- Java Development Kit is the core component of Java Environment.
- It includes the Java Runtime Environment (JRE), an interpreter/loader (Java), a compiler (javac), an archiver (jar), a documentation generator (Javadoc) and other tools needed in Java development.
JRE = JVM + Lib Classes
JDK = JRE + Dev Tools
What are steps to run a Java file
- sample.java is compiled to sample.class as Bytecode using javac command.
- eg: javac sample.java
eg: java sample
JVM is responsible for following 3 steps
- The Classloader reads the .class file, generates the corresponding binary data and saves it in the method area.
- For each .class file, JVM stores the following information in the method area.
- Fully qualified name of the loaded class and its immediate parent class.
- Whether .class file is related to Class or Interface or Enum
- Modifier, Variables and Method information, etc.
- After loading the .class file, JVM creates an object of type Class to represent this file in the heap memory
Linking (Byte Code Verifier)
- Verification: It ensures the correctness of .class file i.e. it checks whether this file is properly formatted and generated by a valid compiler or not. If verification fails, we get run-time exception java.lang.VerifyError
- Preparation: JVM allocates memory for class variables and initializing the memory to default values.
- In this phase, all static variables are assigned with their values defined in the code and static block(if any).
- This is executed from top to bottom in a class and from parent to child in class hierarchy.
- In general, there are three class loaders :
- Bootstrap class loader: Every JVM implementation must have a bootstrap class loader, capable of loading trusted classes. It loads core java API classes present in JAVA_HOME/jre/lib directory.
- Extension class loader: It is the child of the bootstrap class loader. It loads the classes present in the extensions directories JAVA_HOME/jre/lib/ext(Extension path) or any other directory specified by the java.ext.dirs system property.
- System/Application class loader: It is a child of extension class loader. It is responsible to load classes from the application classpath. It internally uses Environment Variable which mapped to java.class.path.