Notes on the Java platform.
These are notes on the Java platform and tools.
The Java Community Process defines standards for these areas:
- The Java Language
- The Java Virtual Machine
- Java Platform
Each Java Specification Request (JSR) includes a Technology Compatibility Kit (TCK) that enables developers to test that an implementation meets the specifications of the JSR.
Similarly, the Java Compatibility Kit (JCK) tests that Java Platform implementations are compatible with standards.
The Java Virtual Machine
Each Java Virtual Machine (JVM) runs compiled binary Java bytecode. This bytecode is platform-independent. Bytecode is enclosed in .class files.
Components of the JVM
- Compiler - Interprets bytecode, and automatically applies JIT compilation (using C1 fast compiler and C2 optimized compiler)
- Runtime - Loads class files into memory, concurrency, interacts with the operating system (threads, memory allocation, sockets), works with external monitoring, handles logging
- Garbage Collector - Memory management
G1 is now the default garbage collector for OpenJDK 10 and above. It implements a parallel mark-sweep-compact algorithm. The default pause time goal for G1 is 200 milliseconds, but this is adjustable.
Java ARchives (JARs)
A JAR is a ZIP file archive that should contain a directory named META-INF, and inside that a manifest file, which will be named MANIFEST.MF. The manifest file provides data about the files that are contained in the JAR.
Java Class Loaders
The JVM does not find classes itself. Instead, Java Class Loaders look for the appropriate class file when the class is first used. By default, a JVM will use three class loaders: the boot class loader (for core libraries that are supplied with the JVM), the extension class loader (for libraries in the extensions directory), and a system class loader, which looks on the classpath, a list of directories and JARs. A classpath can specify any combination of directories, paths to individual JARs, and paths with wildcards to load multiple JARs. Web application servers use additional class loaders. The boot class loader is written in platform-specific native code, and all other class loaders are written in Java.
The system class loader looks for class files in the locations that are specified by the classpath. Each location can either be a directory, the path to a specific JAR file, or a directory with a wildcard. In the last case, the loader uses all of the JAR files in the specified directory.
The classpath is either defined by a CLASSPATH environment variable, or set by a command-line option to the java run-time, or specified in a manifest file. It is a colon-separatedlist on UNIX-like systems, a semi-colon separated list on Windows, and space separated list in manifest files. This example sets the classpath at the command-line on a UNIX-like system:
java -classpath ".:../java-classes/*:/usr/local/lib/java-classes/*"
The default value of classpath is ., which means that it checks the current directory.
Java uses the concept of Properties objects to hold configuration information. A Properties object is an instance of the java.util.Properties class. Each Properties object can hold multiple properties. A property is a key-value pair that is held within a Properties object.
Properties objects are stored as .properties files, usually alongside the the .class files for the application. Developers write code in the application to load properties objects from the appropriate properties files when the application starts.
The Java platform itself uses a Properties object for global configuration. This object provides system properties. The system properties are accessed through methods on the System class.
Agents are plugins for the JVM. For example, JRebel is an agent that enables hot-code reloading.
Each distribution of Java includes a Java Virtual Machine (JVM), and a Java Developers Kit (JDK) which provides the tools and class libraries. Compatibility tests verify whether distribution comply with Java standards.
Most distributions use the HotSpot JVM. For example, both Amazon Corretto and Azul Zulu include versions of the HotSpot JVM. OpenJ9 is an alternative Open Source JVM that is maintained by IBM and the Eclipse Foundation. Most Java distributions now use versions of the OpenJDK tools and libraries.
Android uses the Java language, but Android software development kits (SDKs) are not fully compatible with Java standards.
Free, Open Source Distributions
- AdoptOpenJDK - Provides free versions of the OpenJDK with either HotSpot or OpenJ9 Java Virtual Machines
- Amazon Corretto - OpenJDK with HotSpot Java Virtual Machine, supported by AWS
- Eclipse OpenJ9 - OpenJDK with the OpenJ9 JVM
- Red Hat OpenJDK - Java distributions for JBoss users (Linux and Windows only)
- SapMachine - OpenJDK and JVM, provided by SAP for their customers
- Zulu - OpenJDK with the Zulu JVM, maintained by Azul Systems
- IBM Java SDK
- JamaicaVM - A proprietary Java implementation for real-time systems.
- Oracle JDK - Proprietary builds of HotSpot and OpenJDK, supported by Oracle
- Zing - High-performance proprietary Java implementation maintained by Azul Systems
OpenJDK packages include a JVM (such as HotSpot or OpenJ9), a standard library (the Java Class Library), and various tools, including:
- javac compiler turns .java source files into .class files.
- javap de-compiler reads the contents of class files.
- jar tool works with .jar files, which contain the compiled .class files.
- jarsigner tool is for digitally signing JAR files.
- jdb is a command-line debugging client for the JVM
- jshell command-line shell, which was introduced in Java 9.
- jlink tool create image files that include both code modules and a Java run-time. This was introduced in Java 9.
JDK 9 introduced jaotc, an experimental alternative compiler to javac that produces Ahead Of Time Compiled code, rather than generic bytecode. This is based on Graal.
Monitoring and Debugging
Java Virtual Machines accept connections from debuggers, which may either be on the same system, or connecting from a remote system. The OpenJDK includes jdb, a command-line debugger, and IDEs for Java include graphical debuggers.
Java Flight Recorder and Java Mission Control provide a data collection and profiling system for the OracleJDK.
Popular Third-Party Software
- CheckStyle - Code style checks
- JaCoCo - Test coverage
- PMD - Code quality checks for Java and Salesforce.com
- SonarQube - Code analysis framework
- SpotBugs - Static analysis of code
- Dropwizard - Framework for REST APIs
- Ratpack - Toolkit for lean, asynchronous HTTP services
- Spring Batch - Batch processing
- Spring Boot - Opinionated Web framework
- Apache Tomcat - De-facto standard Web server for Java
- Eclipse Jetty - Often embedded in applications
- Google Guice - Dependency injection framework
- Hibernate - Database toolkit and ORM
- Micrometer - Metrics collection agent library
- resilience4j - Fault tolerance
- Thymeleaf - Templating
- DZone Java Zone - News and tips
- Baeldung - Articles and a weekly newsletter on Java and Spring topics