Java
Software Development
Building
Compiling
Programming

Building vs. compiling in Java

Codemia Knowledge Hub ·

Compiling is one step: it turns Java source code (.java files) into bytecode (.class files). Building is the whole process of producing a deliverable artifact such as a jar or war, and it includes compiling plus dependency resolution, resource processing, testing and packaging.

What compiling does

Compiling transforms source code in a high-level language into a lower-level form. In Java, the result is bytecode that the Java Virtual Machine (JVM) can execute.

  1. Reading the source: The Java compiler (javac) reads the source files and reports any syntax errors to the developer.
  2. Bytecode generation: Once the syntax checks pass, the compiler generates bytecode, a platform-independent intermediate form that runs on any system with a JVM.
  3. Error checking: The compiler checks that the code follows Java's syntax and semantics.
  4. Optimization (optional): Some compilers optimize the bytecode, though optimization happens more often in the JVM at run time.

Take a simple program, HelloWorld.java:

java
1public class HelloWorld {
2    public static void main(String[] args) {
3        System.out.println("Hello, World!");
4    }
5}

Compile it with:

bash
javac HelloWorld.java

This produces HelloWorld.class, which contains the bytecode.

What building adds

A build creates the final artifact, usually a jar or war file that can be deployed or shared. It typically covers:

  1. Compilation of the source code, as above.
  2. Dependency resolution: Tools like Maven or Gradle fetch third-party libraries and make sure the correct versions are included.
  3. Resource processing: Configuration files, images and other non-code resources are included.
  4. Unit testing: Automated tests check that changes have not introduced regressions.
  5. Packaging: Compiled code and resources are bundled into an archive (for example .jar or .war).
  6. Documentation generation (optional): Tools such as Javadoc can run as part of the build.
  7. Deployment (optional): Some builds deploy the application to a testing or production environment.

With Maven, a typical build command is:

bash
mvn clean install

This command:

  • Cleans the project, removing previous build artifacts.
  • Compiles the source code.
  • Runs specified tests.
  • Packages the code into a .jar file.
  • Installs the package to the local repository.

Compiling and building side by side

AspectCompilingBuilding
Primary FunctionTransforms source code into bytecodeCreates a complete deliverable artifact (e.g., jar, war)
ScopeLimited to code translation and error checkingIncludes compilation, dependency management, packaging, testing, and deployment
ToolsjavacMaven, Gradle, Ant, etc.
Output.class files.jar, .war, .ear, etc. files
Error CheckingSyntax and static semantic errorsOften includes automated tests for logic errors
Dependency HandlingNot involvedManages and resolves dependencies
Resource HandlingNot involvedProcesses and includes resources in the build

Course
Intermediate
27 lessons
14 hours
OOD Fundamentals

Master object-oriented design from first principles, SOLID, design patterns, and classic interview problems with hands-on coding.

View the course
Data Structures & Algorithms practice on Codemia

Step through 300+ coding interview problems with animated visualisers that show the data structure changing as the code runs. The Blind 75 are free.

Practice algorithms