Lesson · Java Roadmap & Environment
The tools you'll meet early: javap, jar, classpath, stack traces
11 min readTools You Will Meet Early
Four tools that stop Java being a black box: javap reads the bytecode the compiler wrote, jar packages classes, the classpath explains every 'class not found', and stack traces tell you exactly where things broke.
javap — read the code the compiler actually wrote
You write Java; the JVM runs bytecode. javap is the bridge: it disassembles .class files so you can see what the compiler emitted — the desugared, expanded, less magical version of your source.
This is how you *verify* things instead of memorising them. Autoboxing, string concatenation, for-each over collections, lambdas — javap shows the synthetic method calls your syntax sugar becomes. Later chapters on bytecode and lambdas revisit this with a bigger lens; today, just know the tool exists and that it reads .class files, not .java files (run it where the classes are, or point it at the right path).
- javap -c ClassName — disassemble: the actual bytecode instructions per method.
- javap -p ClassName — include private members, which the default view hides.
- javap -v ClassName — verbose: the constant pool, flags, version — the class file's innards.
jar — packaging classes
A jar is a zip archive of .class files (plus resources) with a manifest — Java's unit of distribution. jar cf app.jar *.class creates one; jar tf app.jar lists its contents; unzip it and you'll see it really is just files in directories matching package names.
To make a jar runnable with java -jar, the manifest needs a Main-Class entry. That single line is the difference between a library jar (a bag of classes others depend on) and an executable jar. Build tools like Maven and Gradle generate both kinds for you — but underneath, it is always this zip with a manifest.
Classpath — where the JVM looks for classes
The classpath is an ordered list of directories and jars the class loader searches — separated by ; on Windows, : on Unix. java -cp out com.example.Main says: look in the out directory. Without -cp, the default is the current directory; if that's wrong, nothing resolves.
The mapping that trips everyone up once: a directory entry is the *root*, and packages are subdirectories. Class com.example.Main must live at out/com/example/Main.class. One . in the wrong place (com.example on the classpath instead of its parent) and the JVM searches for a nonexistent path. When something 'can't find the class', print the classpath before doubting the code.
Reading a stack trace
A stack trace is not noise. It is a precise map, and ten seconds with it beats ten minutes of guessing.
The trick that makes you fast: find the first frame that is your code. The top frames are often JDK internals (HashMap.get, ArrayList.sort), and the first line mentioning your package is where your logic met the failure. And remember: the line number points at where the exception surfaced, while the bug may be an earlier caller that fed bad state in.
- The first line names the exception and its message: the what.
- The frames list where execution was, most recent call first: the where.
- The Caused by blocks, when present, chain the underlying failures back toward the origin.
javap reads .class files, not .java files
Point javap at your source and it fails with 'class not found' — it disassembles compiled classes, so run it from the directory holding the .class output (or give it a path). Same for jar: it packages .class files, which is why you compile before you package.
Interview angles
NoClassDefFoundError vs ClassNotFoundException — what's the difference?+
ClassNotFoundException: an explicit dynamic lookup — Class.forName("x.Y") or a class loader call — couldn't resolve the name. NoClassDefFoundError: the class was present at compile time but couldn't be loaded or initialized at runtime (missing jar, or an earlier static initialization failed). Explicit load vs failed implicit link.
How do you inspect what javac actually generated?+
javap. javap -c disassembles a class's methods; javap -v shows the constant pool, flags and file version. It's the standard way to see what syntax sugar (boxing, enhanced for, lambdas) compiles down to.
What is a classpath, precisely?+
An ordered list of directories and jars (separator: ; on Windows, : elsewhere) the class loader searches by fully-qualified name. Directory entries act as package roots — com/example/Main.class lives under the entry that represents com's parent.
How do you read a stack trace efficiently?+
First line: exception + message. Then scan top-down for the first frame in your own package — that's where your code met the failure. Follow any 'Caused by' chain to the origin. Line numbers show where it surfaced, not always where the bug lives.