javac, Classpaths & JARs by Hand
Maven and Gradle never compile Java themselves. They compute a list of source files and a classpath, call the Java compiler, and pack the result into a JAR. Doing those steps once by hand makes every later lesson easier: when a build fails with "package does not exist" or a program dies with NoClassDefFoundError, you'll know which list was wrong.
Three tools from the JDK
| Tool | Input | Output |
|---|---|---|
javac |
.java sources + a classpath of already-compiled classes/JARs |
.class files, one per class, in folders that mirror the packages |
jar |
a folder of .class files and resources |
a ZIP file with a META-INF/MANIFEST.MF |
java |
a classpath + a main class (or -jar with a manifest Main-Class) |
a running program |
The classpath is an ordered list of folders and JARs. The compiler searches it for the classes your code refers to; the JVM searches it again at run time to load them. The two lists are often different: a library you only need at run time (a logging backend, a JDBC driver) doesn't have to be on the compile classpath. Build tools model exactly this difference with scopes (Maven) and configurations (Gradle).
Compile, package and run
This session builds a two-package program that uses Apache Commons Lang, a JAR that is already in the local Maven repository:
mkdir -p byhand/src/demo/util byhand/src/demo/app && cd byhand
cat > src/demo/util/Text.java <<'SRC'
package demo.util;
import org.apache.commons.lang3.StringUtils;
public class Text {
public static String shout(String s) {
return StringUtils.upperCase(s) + "!";
}
}
SRC
cat > src/demo/app/Main.java <<'SRC'
package demo.app;
import demo.util.Text;
public class Main {
public static void main(String[] args) {
System.out.println(Text.shout("built by hand"));
}
}
SRC
LANG3=$HOME/.m2/repository/org/apache/commons/commons-lang3/3.21.0/commons-lang3-3.21.0.jar
javac --release 21 -d out -cp "$LANG3" $(find src -name '*.java')
find out -type f | sort
jar --create --file app.jar --main-class demo.app.Main -C out .
jar --list --file app.jar
java -cp "app.jar:$LANG3" demo.app.Mainout/demo/app/Main.class
out/demo/util/Text.class
META-INF/
META-INF/MANIFEST.MF
demo/
demo/app/
demo/app/Main.class
demo/util/
demo/util/Text.class
BUILT BY HAND!
--release 21compiles against the Java 21 API and emits Java 21 class files, whatever JDK you run. Maven'smaven.compiler.releaseand Gradle's toolchain setting end up as this flag.-d outputsdemo/util/Text.classanddemo/app/Main.classin package folders. Maven calls this foldertarget/classes, Gradlebuild/classes/java/main.--main-classwritesMain-Class: demo.app.Maininto the manifest.
When the classpath is wrong
Leave the library off the classpath and you get the two classic failures, one per phase:
mkdir -p broken/src/demo && cd broken
cat > src/demo/Main.java <<'SRC'
package demo;
import org.apache.commons.lang3.StringUtils;
public class Main {
public static void main(String[] args) {
System.out.println(StringUtils.capitalize("ada"));
}
}
SRC
LANG3=$HOME/.m2/repository/org/apache/commons/commons-lang3/3.21.0/commons-lang3-3.21.0.jar
echo "--- compile without the library"
javac --release 21 -d out src/demo/Main.java
echo "--- compile with it, run without it"
javac --release 21 -d out -cp "$LANG3" src/demo/Main.java
java -cp out demo.Main
echo "--- run with it"
java -cp "out:$LANG3" demo.Main--- compile without the library
src/demo/Main.java:3: error: package org.apache.commons.lang3 does not exist
import org.apache.commons.lang3.StringUtils;
^
src/demo/Main.java:7: error: cannot find symbol
System.out.println(StringUtils.capitalize("ada"));
^
symbol: variable StringUtils
location: class Main
2 errors
--- compile with it, run without it
Exception in thread "main" java.lang.NoClassDefFoundError: org/apache/commons/lang3/StringUtils
at demo.Main.main(Main.java:7)
Caused by: java.lang.ClassNotFoundException: org.apache.commons.lang3.StringUtils
at java.base/jdk.internal.loader.BuiltinClassLoader.loadClass(BuiltinClassLoader.java:641)
at java.base/jdk.internal.loader.ClassLoaders$AppClassLoader.loadClass(ClassLoaders.java:188)
at java.base/java.lang.ClassLoader.loadClass(ClassLoader.java:526)
... 1 more
--- run with it
Ada
The compiler's error ("package … does not exist") means the compile classpath is missing something. NoClassDefFoundError means the code compiled, but the runtime classpath is missing something. In a Maven or Gradle build, the first one points at a missing dependency or a wrong scope; the second usually at a fat JAR or a start script that forgot a dependency.
Why order matters: the first match wins
The JVM loads each class from the first classpath entry that contains it. If two JARs both contain org/example/Util.class (two versions of the same library, say), the one listed first silently wins. That is why build tools work hard to put exactly one version of each library on the classpath, and why the conflict rules you'll meet later matter. A small model of the lookup:
# Each classpath entry maps class names to the version of the JAR that contains them.
classpath = [
("target/classes", {"demo.Main": "app"}),
("text-util-1.0.jar", {"util.Text": "1.0"}),
("text-util-2.0.jar", {"util.Text": "2.0", "util.Text2": "2.0"}),
]
def load(name):
for entry, classes in classpath:
if name in classes:
return f"{name:10} <- {entry} (version {classes[name]})"
return f"{name:10} <- NoClassDefFoundError"
for cls in ["demo.Main", "util.Text", "util.Text2", "util.Gone"]:
print(load(cls))demo.Main <- target/classes (version app)
util.Text <- text-util-1.0.jar (version 1.0)
util.Text2 <- text-util-2.0.jar (version 2.0)
util.Gone <- NoClassDefFoundError
util.Text comes from version 1.0 but util.Text2 from 2.0: a mixed program that may fail with NoSuchMethodError the moment 2.0 code calls a method that 1.0's Text lacks. A build tool that picks one version per library avoids this.
Note: Since Java 9 there is also a module path (
--module-path,module-info.java). Both Maven and Gradle support it, but most applications still run on the classpath, and this course stays there.
Test yourself
A build compiles fine, but java fails with NoClassDefFoundError: org/apache/commons/lang3/StringUtils. What is missing?
- The library on the compile classpath
- The library on the runtime classpath
- A
packagedeclaration - The
--releaseflag
Two JARs on the classpath contain the same class. Which one does the JVM load?
- The newest one
- The one in the first classpath entry that contains it
- Both, merged
- It refuses to start
What does javac --release 21 guarantee?
- It uses JDK 21 to run
- The code is compiled against the Java 21 API and emits Java 21 class files
- It enables preview features
- It only compiles files changed since release 21