Loading
Difference Between JDK, JRE, and JVM in Java

Q1. Why does the error 'javac' is not recognized as an internal or external command occur in Java ?
This error occurs when the Java Development Kit (JDK) is not installed or not properly configured on the system.

The javac command is the Java compiler, which is part of the JDK. If only the Java Runtine Environment (JRE) is installed, the system can run Java programs but cannot compile them, because the JRE does not include development tools like javac.

To resolve this issue:

  • Install the JDK instead of only the JRE.
  • Ensure that the JDK's bin directory is correctly added to the system's PATH environment variable.
The JDK includes the compiler, debugger, and other tools required for Java development.


Q2. How can the same compiled Java program run on different operating systems without modification?
Java programs are compiled into platform-independent bytecode, stored in .class files, rather than directly into machine-specific code.

This bytecode is executed by the Java Virtual Machine (JVM), which is available for different operating systems such as Windows, macOS, and Linux. Each JVM converts the bytecode into native machine instructions specific to the underlying operating system.

Because of this design, the same compiled Java code can run on any platform that has a compatible JVM installed. This concept is known as "Write Once, Run Anywhere (WORA)", which is a core principle of Java.


Q3. Who manages memory in Java, and how is unused memory reclaimed?
Memory management in Java is handled automatically by the Java Virtual Machine (JVM).

The JVM includes a component called the Garbage Collector (GC), which continuously monitors objects usage in memory. When objects are no longer referenced by the program, the garbage collector automatically reclaims their memory.

Java uses different memory areas such as:

  • Heap memory for objects and class instances
  • Stack memory for method calls and local varibles
Because of automatic garbage collection, developers do not need to manually free memory, reducing the risk of memory leaks and making Java programs more stable compared to languages like C or C++.


Q4. When should JDK be installed instead of JRE ?
The choice between JDK and JRE depends on the intended use of the system.

JRE (Java Runtime Environment) is sufficient when the system only needs to run Java applications.
JDK (Java Development Kit) is required when the sytem needs to develop, compile, debug, or test Java programs.

For productions or end-user machines, installing the JRE is usually enough. For developer mahcines, the JDK is neccessary beacause it includes development tools such as the Java copiler, debugger and documentation tools.


Q5. Why does a Java application copiled with a newer JDK fail on an older JVM ?
Java bytecode compiled using a newer JDK version may include language features and bytecode instructions that are not supported by older JVM versions.

For example, bytecode generated by JDK 17 may not be compatible with JVM 8, causing runtime errors or applications startup failures.

To prevent this issue:
 
  • Ensure that the runtime JVM version matches or exceeds the JDK version used for compilation.
  • Use the --release flag during compilation to generate bytecode compatible with a specific Java version.
Maintaining version compatibility is essential for stable deployment in production environments.