JVM

  • Java Virtual Machine
  • https://www.freecodecamp.org/news/jvm-tutorial-java-virtual-machine-architecture-explained-for-beginners/
  • Engine that runs bytecode and make java platform independent
  • Components:
    • Class loader: responsible for loading java class files into memory
    • Execution engine: executes the java byte code. Its components are:
      • Just In Time (JIT) compiler: compiles frequently executed byte code into optimized machine code
      • Interpreter: executes non-compiled byte code
    • Runtime Data Areas:
      • Method Area: compiled class files
      • Heap Area: objects
      • Stack Area: local variables, results of intermediate operations
      • Program counter (PC) register: address of the next instruction to be executed
      • Native Method Stack: Helps in executing native methods
  • JVM Components:

JRE

  • Java Runtime Environment
  • contains JVM + standard libraries:
    • java.lang
    • java.util

JDK

  • Java Development Kit
  • contains JRE + Tools that are required to compile, debug, and run a program developed using the Java platform

|400

JDK vs JVM vs JRE

  • JVM
    • loads, verifies, runs Java bytecode
    • converting bytecode to machine-specific code
    • can also run other programming language Java bytecode.
    • necessary in both JDK and JRE
    • performs memory management and security
  • JRE
    • You can only run your java program
    • include libraries like java.lang and java.util
    • JDK
      • Java Development Kit
      • Superset of JRE
      • platform specific
      • contains:
        • JRE
        • compiler (javac)
        • debugger (jdb)
        • archiver (jar) etc.

Interpreter

- reads and executes the bytecode instructions line by line

  • it is comparatively slower since it reads instruction line by line
  • when a method is called multiple times, every time a new interpretation is required
  • Java is both compiled (via javac) and interpreted (via java) language

JIT

JNI

  • Java Native Interface
  • programming framework that acts as a bridge for permitting the supporting packages for other programming languages such as C, C++, and so on.
  • native keyword to indicate that the method implementation will be provided by a native library.
  • Native Method Libraries:
    • written in other languages
    • generally *.so or *.dll files
    • Can be loaded by JNI

GC

JVM based languages

  • Scala
  • Kotlin (official language of Android Development)
  • Groovy (Gradle build scripts uses groovy DSL language)
  • Clojure

Heap

Java command line

javac

  • Compile java code
  • Outputs class files
javac Test.java
 
# include external jars in classpath
javac -cp <class-path-for-external-jars> Test.java
 
# output to a directory
javac -d <directory-path> Test.java

java

  • Runs the class files
# do not use extension .class
java Test
 
# for external jars
java -cp <class-path-for-external-jars> Test.java

javap

  • Standard Java Disassembler
  • Disassembles a class file
  • To make a .class file readable for humans, you should disassemble it
# do not use extension .class
javap -p Test

Environment variables

  • JAVA_HOME:
    • point to JDK folder
    • $JAVA_HOME/bin should exist having java executable
  • JAVA_OPTS:
    • java options
    • example: -Xmx2G -Xms512M
  • CLASSPATH:
    • example:
# WINDOWS, separate by semi-colon
$ set CLASSPATH=C:\dependency\framework.jar;C:\location\otherFramework.jar
$ set CLASSPATH=C:\dependency\framework.jar;C:\location\*.jar
 
# Linux/Unix, separate by colon
$ export CLASSPATH=/dependency/framework.jar:/location/otherFramework.jar
$ export CLASSPATH=/dependency/framework.jar:/location/*.jar

ClassPath

  • Using classpath you can specify the java compiler where to look for user defined classes and packages.
  • Ways to set classpath:
    • Command line -cp or -classpath
    • Environment variable: $CLASSPATH or %CLASSPATH%

Class Loader and Types

  • It is part of JRE which dynamically loads Java classes into the JVM
  • Usually classes are loaded on demand, JVM will only load class required for execution
  • The on-demand approach means that the class will be loaded on its first invocation/reference
  • 3 Built in class loaders
    • Bootstrap/Primordial Class Loader
      • loads JDK internal classes
      • loads core classes and rt.jar(removed in java 9 and merged with other libraries)
      • for example java.lang.* package classes
    • Extensions Class Loader
      • loads classes from the JDK extensions directory
      • usually $JAVA_HOME/lib/ext directory
      • extension of standard java core classes
    • System Class Loader
      • loads classes from the current classpath
      • See ClassPath
  • Custom Class loaders are required when:
    • need to load class from FTP server
    • need to load from third party web service
    • need to load class at runtime
  • Delegation Model
    • When the JVM encounters a request to load a class, it first checks if the class has already been loaded.
    • If not, it delegates the request to a parent class loader. If the parent class loader returns nothing, it then attempts to find the class in its own classpath
    • If class is not loaded then system delegates to extension which delegates to Bootstrap
      • If Bootstrap does not find, Extension attempts
      • If Extension does not find, then it goes back to system to attempt
flowchart BT

bootstrap["Bootstrap"]
extension["Extension"]
system["System"]

extension --> bootstrap;
system --> extension
// Application
System.out.println(MyClassLoader.class.getClassLoader());
 
// Extension
System.out.println(DriverManager.class.getClassLoader());
 
// Bootstrap (prints null)
System.out.println(java.util.ArrayList.class.getClassLoader());
 
/* Output
jdk.internal.loader.ClassLoaders$AppClassLoader@73d16e93 jdk.internal.loader.ClassLoaders$PlatformClassLoader@1fb700ee
null
*/

JAR files

  • Java Archive
  • In order for it to be runnable, we need to specify the Main class in Manifest
  • Inside jar, there must be /META-INF/MANIFEST.MF file, which should specify the main class
  • !important to have a newline at the end, otherwise it will not work
Manifest-version: 1.0
Main-Class: Test
 
  • Creating jar file
jar cfm test.jar META-INF/MANIFEST.MF Test.class
  • Running jar file
java -jar test.jar

Bytecode

  • Intermediate representation of a Java program after the source code compilation
  • stored in .class files
  • Bytecode instruction:
    • one-byte operation code: opcode
    • zero or more operands
  • Runtime bytecode generation refers to the dynamic creation and execution of code within a running program
  • Uses of Runtime bytecode generation:
    • Java’s Dynamic Proxy API
    • Mockito utilize bytecode generation to dynamically create proxy (mock) objects
    • Apache Spark creates custom functions and expressions for distributed data processing
  • Libraries to generate/write/modify bytecode instructions
    • ASM
    • cglib
    • Javassist
    • ByteBuddy (better and easier to use)