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.
- Reading the source: The Java compiler (
javac) reads the source files and reports any syntax errors to the developer. - Bytecode generation: Once the syntax checks pass, the compiler generates bytecode, a platform-independent intermediate form that runs on any system with a JVM.
- Error checking: The compiler checks that the code follows Java's syntax and semantics.
- Optimization (optional): Some compilers optimize the bytecode, though optimization happens more often in the JVM at run time.
Take a simple program, HelloWorld.java:
Compile it with:
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:
- Compilation of the source code, as above.
- Dependency resolution: Tools like Maven or Gradle fetch third-party libraries and make sure the correct versions are included.
- Resource processing: Configuration files, images and other non-code resources are included.
- Unit testing: Automated tests check that changes have not introduced regressions.
- Packaging: Compiled code and resources are bundled into an archive (for example
.jaror.war). - Documentation generation (optional): Tools such as Javadoc can run as part of the build.
- Deployment (optional): Some builds deploy the application to a testing or production environment.
With Maven, a typical build command is:
This command:
- Cleans the project, removing previous build artifacts.
- Compiles the source code.
- Runs specified tests.
- Packages the code into a
.jarfile. - Installs the package to the local repository.
Compiling and building side by side
| Aspect | Compiling | Building |
| Primary Function | Transforms source code into bytecode | Creates a complete deliverable artifact (e.g., jar, war) |
| Scope | Limited to code translation and error checking | Includes compilation, dependency management, packaging, testing, and deployment |
| Tools | javac | Maven, Gradle, Ant, etc. |
| Output | .class files | .jar, .war, .ear, etc. files |
| Error Checking | Syntax and static semantic errors | Often includes automated tests for logic errors |
| Dependency Handling | Not involved | Manages and resolves dependencies |
| Resource Handling | Not involved | Processes and includes resources in the build |
Related reading
- Built-in Java 8 predicate that always returns true?
- Bulk insert with Spring Boot and Spring Data JPA not working
- By default, where does Spring Boot expect views to be stored?
- byte[] to file in Java
- Bytes of a string in Java
- C equivalent of java's instanceof
- C equivalent to Java's Exception.printStackTrace?
- C# Java HashMap equivalent
System design interview problems on this topic
- Design an API Rate Limiter: solution and walkthrough
- Design a Distributed Messaging System: solution and walkthrough
- Design an Online Payment Service: solution and walkthrough
- Design a Key Value Store: solution and walkthrough

OOD Fundamentals
Master object-oriented design from first principles, SOLID, design patterns, and classic interview problems with hands-on coding.
View the courseData 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.