Chapter 1. Compiling and Running Java

You must crawl before you can walk

Larry | Peng Yang
Mastering Java
4 min readJul 25, 2022

--

How Java code is compiled and loaded

JDK, JRE, JVM

JVM (Java Virtual Machine) is an abstract machine. It is called a virtual machine because it doesn’t physically exist. It is a specification that provides a runtime environment in which Java bytecode can be executed. It can also run those programs which are written in other languages and compiled to Java bytecode.

bytecode is not actually very similar to machine code that would run on a real hardware processor. Instead, computer scientists would call bytecode a type of intermediate representation — a halfway house between source code and machine code.

JRE (Java Runtime Environment) is a software package that provides Java class libraries, Java Virtual Machine (JVM), and other components that are required to run Java applications.

JDK (Java Development Kit) is a software development kit required to develop applications in Java. When you download JDK, JRE is also downloaded with it. In addition to JRE, JDK also contains a number of development tools (compilers, JavaDoc, Java Debugger, etc).

JDK, JRE and JVM

How to run a Java program

Assuming you have the standard JDK installed in the standard location and/or have set its location in your , you should be able to run the command-line JDK tools. Use the commands javac to compile and java to run your program (and, on Windows only, javaw to run a program without a console window).

> javac HelloWorld.java
> java HelloWolrd
Hello, Wolrd

How java classes are loaded and initilizaed in JVM

Unlike C/C++ where linking combines source files from different places and form an executable program, the linking-like step for Java is done when they are loaded into JVM.

Different JVMs load classes in different ways, but the basic rule is only loading classes when they are needed. If there are some other classes that are required by the loaded class, they will also be loaded. The loading process is recursive.

The Java launcher, java, initiates the Java virtual machine. The virtual machine searches for and loads classes. It loads classes in this order:

  • Bootstrap classes — Classes that comprise the Java platform, including the classes in rt.jar and several other important jar files.
  • Extension classes — Classes that use the Java Extension mechanism. These are bundled as .jar files located in the extensions directory.
  • User classes — Classes defined by developers and third parties that do not take advantage of the extension mechanism. You identify the location of these classes using the -classpath option on the command line (the preferred method) or by using the CLASSPATH environment variable.

CLASSPATH is a list of class files in any of a number of directories, JAR files, or ZIP files. Just like the your system uses for finding programs, the is used by the Java runtime to find classes. The default is . .

A class is loaded:

  • when the new bytecode is executed. For example, SomeClass f = new SomeClass();
  • when the bytecodes make a static reference to a class. For example, System.out.

We can use the command to check the classes that are loaded in JVM. Other methods can be found here.

> java <app_name> --verbose:class
[Loaded sun.misc.JavaSecurityProtectionDomainAccess from /usr/local/java/jdk1.6.0_34/jre/lib/rt.jar]
...

A class is initialized when a symbol in the class is first used. When a class is loaded it is not initialized. JVM will initialize superclass and fields in textual order, initialize static, final fields first, and give every field a default value before initialization.

Automating Dependencies, Compilation, Testing, and Deployment

Maven and Gradle are the most two popular tools that can be used to download your dependencies, compile your code, compile and run your tests, package the app, and install or deploy sit.

Maven is controlled by a file called pom.xml (for Project Object Model). A POM file can redefine any of the standard goals. Common Maven goals (predefined by default to do something sensible) include the following:

  • clean: Removes all generated artifacts
  • compile: Compiles all source files
  • test: Compiles and runs all unit tests
  • package: Builds the package
  • install: Installs pom.xml and the package into your local Maven repository for use by your other projects
  • deploy: Tries to install the package (e.g., on an application server)

Most of the steps implicitly invoke the previous ones. For example, packge will compile any missing .class files and run the tests if that hasn’t already been done in this run. One of the most used commands is mvn clean package.

Gradle doesn’t use XML as its scripting language, but rather a Domain-Specific Language (DSL) based on the JVM-based and Java-based scripting language Groovy.

References

--

--