JDK, JRE, and JVM
JDK, JRE, and JVM are essential components in the world of Java programming, each serving a specific role in the development and execution of Java applications:
JDK (Java Development Kit)
The JDK is a software package provided by Oracle (and other vendors) that includes all the tools and libraries necessary for Java application development. It contains the following key components:
Java Compiler (javac): This tool is used to compile Java source code (.java files) into bytecode (.class files), which can be executed by the Java Virtual Machine (JVM).
Java Runtime Environment (JRE): The JRE is also included in the JDK. It is needed for running Java applications but not for developing them. The JRE consists of the JVM and essential libraries.
Development Tools: The JDK includes various development tools like debugging tools, JavaDoc for generating documentation, and more.
Developers use the JDK to write, compile, and package Java applications.
JRE (Java Runtime Environment):
The JRE is a subset of the JDK and is required to run Java applications. It includes the following components:
Java Virtual Machine (JVM): The JVM is the runtime engine responsible for executing Java bytecode. It interprets and translates bytecode into machine-specific code that the underlying operating system can understand. Each platform (e.g., Windows, Linux, macOS) has its own JVM implementation
Java Standard Library: The JRE includes a set of core libraries and classes that provide essential functionality to Java applications. These libraries offer features like input/output operations, networking, and more.
End-users who want to run Java applications need the JRE installed on their system. It allows them to execute Java programs without the need for development tools.
JVM (Java Virtual Machine):
The JVM is the runtime environment in which Java bytecode is executed. It is an integral part of both the JDK and the JRE. The JVM performs several crucial tasks:
Loading: It loads compiled Java bytecode (class files) into memory.
Verification: The JVM checks the bytecode for security and integrity to prevent potential security vulnerabilities.
Execution: The bytecode is executed by the JVM, which translates it into machine code specific to the underlying hardware and operating system.
Memory Management: The JVM manages memory allocation and garbage collection to ensure efficient memory usage.
Security: The JVM enforces Java's security model, including access control, class-loading restrictions, and more.
Java applications are often referred to as WORA, which stands for "Write Once Run Anywhere." This concept signifies that a programmer can write Java code on one system and anticipate it to run on any other Java-enabled system without the need for modifications. This seamless portability is made possible due to the presence of the Java Virtual Machine (JVM).
When a .java file is compiled, it produces .class files that contain bytecode and share the same class names as the corresponding .java files. When these .class files are executed, they undergo a series of steps that collectively define the functionality of the JVM (Java Virtual Machine).
Java bytecode
Java bytecode serves as the instruction set for the Java Virtual Machine (JVM). It functions akin to an assembler, providing a symbolic representation of Java code, much like how C++ code can be represented in an alias form. When a Java program is compiled, it results in the generation of Java bytecode. To put it succinctly, Java bytecode can be thought of as machine code encapsulated in a .class file. The utilization of Java bytecode is what enables Java to attain platform independence, ensuring that Java programs can run on different systems without modification.
How does it works?
When we develop a program in Java, the initial step involves compiling the code, resulting in the creation of bytecode. This bytecode serves as an intermediary representation of the code. Importantly, when we intend to execute this .class file on different platforms, we can readily do so. Following the initial compilation, it's the Java Virtual Machine (JVM), rather than the specific processor of the platform, that takes charge of running the bytecode.
In essence, this implies that we only need a basic Java installation on any platform where we wish to execute our code. The JVM plays a crucial role by managing the resources required for executing the bytecode. It communicates with the processor to allocate the necessary resources. It's worth noting that JVMs operate in a stack-based manner, using a stack implementation to interpret and execute the bytecode instructions.
Advantage of Java Bytecode
What is path ?
After installing Java on your computer, it's important to configure the PATH environment variable. This configuration allows you to conveniently execute Java-related executables (such as javac.exe, java.exe, javadoc.exe, etc.) from any directory without the need to specify the full path to the command.
C:\javac DemoClass.java
If you don't set the PATH environment variable, you would indeed have to specify the full path each time you run a Java command, like this:
C:\Java\jdk1.7.0\bin\javac DemoClass.java
Without the PATH variable configuration, this full path specification is necessary for running Java executables.
What is Classpath?
The classpath is a system environment variable utilized by both the Java compiler and the JVM (Java Virtual Machine).
It serves as a reference to help these components locate the necessary class files.
C:\Program Files\Java\jdk1.6.0\lib