Skip to main content
This lesson covers the essentials of building a Java application: how source code becomes JVM bytecode, how to package applications for distribution, and how build automation tools streamline and standardize the process for projects of any size.

Simple build process

A minimal Java build has three steps:
  1. Write source code (.java)
  2. Compile to JVM bytecode (.class)
  3. Run on a Java Virtual Machine (JVM)
Example source file:
Compile the source into bytecode:
This produces MyClass.class. Run the program with the Java launcher (specify the class name without .class):
You can inspect the JVM instructions (bytecode) with javap -c:
Because JVM implementations exist for many platforms, Java bytecode is cross-platform: compile once, run anywhere that provides a compatible JVM.

Packaging: JAR and WAR

Applications typically include many classes plus resources (configuration, images, web assets). Packaging these into archives makes distribution and deployment simpler.
  • JAR (Java ARchive): Bundles class files, libraries, and resources into a single compressed archive.
  • WAR (Web ARchive): Layout for web applications; contains web-specific directories and static files.
Create a simple JAR by specifying the output file and files to include:
When creating a JAR, the jar tool generates a default META-INF/MANIFEST.MF. To make a JAR executable (so you can run java -jar MyApp.jar) you must include a Main-Class entry. Two common approaches:
  1. Use the -e option to set the entry point:
  1. Or provide a custom manifest file (e.g., manifest.txt):
Include that manifest when building the JAR:
Run the packaged JAR on any machine with a Java Runtime Environment (JRE):
If your application depends on external libraries, ensure they are available at runtime. Either use the Class-Path manifest entry, supply required JARs on the -cp/--class-path, or build a single “fat” or “uber” JAR that bundles dependencies.

Generating documentation

Document source with Javadoc. Running javadoc on source files generates an HTML documentation site:
This creates an HTML site in the doc directory for other developers to browse.

When builds get bigger

Larger projects and team workflows typically require more steps:
  • Compiling multiple modules
  • Running unit and integration tests
  • Static analysis and linting
  • Security and vulnerability scanning
  • Packaging, signing artifacts, and publishing to artifact repositories
Manual command sequences become error-prone when projects grow or many developers contribute concurrently. Build automation tools provide repeatable, auditable, and CI/CD-friendly workflows.
A slide titled "Build Process" showing user and document icons on the left feeding through gears and package/build icons in the center to produce suitcase (artifact) icons and final documents on the right. Dotted lines connect the stages to illustrate a pipeline from source files to built outputs.

Build tools: Ant, Maven, Gradle

Common Java build tools let you describe build steps in configuration files and run them consistently across environments. Below is an example Ant build file that compiles code, generates Javadoc, and creates a JAR. Targets declare dependencies so Ant executes them in the required order.
Run the default (main) target:
Or run specific targets, for example only compile and jar:
Build tools let teams automate and standardize repetitive steps (compile, test, package, publish). Integrate these tools with CI/CD pipelines to enforce consistency and reduce manual errors.

Gradle example

A minimal Gradle build script (Groovy DSL) that applies plugins and defines the main class:
Common Gradle commands:
  • Build the application:
  • Run the application:
Gradle offers a rich plugin ecosystem (Docker, publishing, test reporting) and is widely used for modern Java builds, multi-module projects, and CI environments.

Summary

Key takeaways:
  • Source (.java) -> compile with javac -> bytecode (.class) executed by the JVM.
  • Use the java launcher to run classes; use javadoc to generate API documentation.
  • Package applications into JAR/WAR archives; include a Main-Class manifest entry to make JARs executable.
  • Ensure external libraries are available at runtime via classpath entries or by creating a fat/uber JAR.
  • Use build tools (Ant, Maven, Gradle) to automate compilation, testing, packaging, and publishing in a repeatable way.
A presentation slide titled "Summary" showing bullet points about Java topics. It lists: Java, Java Runtime Environment, Java Development Kit, compiling a Java application, packaging to JARs, and build tools.
Further reading and references: Practice these steps locally or in a sandbox environment to reinforce learning and to see how CI/CD and container workflows automate many of these tasks in a DevOps pipeline.

Watch Video

Practice Lab