Sub topic 2
Building by Hand: javac, the Classpath, and JAR Files
Before you use Maven or Gradle, it helps to build a small program yourself. Then you can see which steps a build tool takes over for you. This chapter uses only the tools that come with the JDK.
The Program We Will Build
Save this class as src/main/java/com/example/App.java:
package com.example;
public class App {
public static void main(String[] args) {
System.out.println(greeting("World"));
}
static String greeting(String name) {
return "Hello, " + name + "!";
}
}The folders com/example match the package name com.example. Java expects this match.
Four Key Terms
| Term | Meaning |
|---|---|
| `javac` | The Java compiler. A compiler is a program that turns your source code into a form the computer can run. |
| Class file | A `.class` file holds bytecode. Bytecode is the compiled form of a class that the Java Virtual Machine (JVM) runs. |
| Classpath | The classpath is the list of folders and files where Java looks for class files. |
| JAR file | A JAR (Java ARchive) is one zip file that holds many class files plus a manifest. A manifest is a small text file that describes the JAR, such as which class to start. |
The Manual Build, Step by Step
Run these commands from the project's top folder:
mkdir -p out
javac -d out src/main/java/com/example/App.java
java -cp out com.example.App
jar --create --file app.jar --main-class com.example.App -C out .
java -jar app.jarHere is what each line does:
mkdir -p outmakes a folder to hold the compiled output.javac -d out ...compiles the source. The-d outoption putsApp.classinout/com/example/.java -cp out com.example.Appruns the program. The-cp outoption sets the classpath to theoutfolder. You give the full class name, not a file path.jar --create ...packs everything inoutintoapp.jar. The--main-classoption writes the start class into the manifest. The-C out .option means "change intooutand add all of its files."java -jar app.jarruns the JAR. Java reads the manifest to findmain.
Both run commands print Hello, World!.
Where Doing It by Hand Gets Hard
This works for one file. Real projects soon become harder to build:
- They have hundreds of source files to list for
javac. - They use libraries, so you must download each JAR and add it to the classpath yourself.
- Tests must be compiled and run as a separate step.
- Every developer has to type the same commands in the same order.
Maven and Gradle do all of these steps for you from a single build file.
Exercise
You compiled App.java into the out folder. Why does java -cp out com.example.App work, but java com.example.App run from the project folder fail?
Show answer
Without -cp out, the classpath defaults to the current folder. Java looks for com/example/App.class there and does not find it. The -cp out option tells Java to search the out folder, which holds the compiled class.
Exercise
What does the --main-class option add to app.jar, and why does java -jar app.jar need it?
Show answer
It writes the start class (com.example.App) into the JAR's manifest. java -jar reads the manifest to learn which class's main method to run. Without that entry, Java cannot tell where the program starts.