An editor, IntelliJ and javac
A Java program is written in a text file, compiled by javac, and run by java. An editor only helps with the first step, and a large IDE is a convenience around the other two, not a replacement for them. This lesson builds a program with two methods, keeps the class files out of the source folder, and then looks inside the class file with the tools the JDK ships.
The edit-build-run loop
Every change goes through the same three commands. The editor writes the file; javac turns it into a class file; the JVM runs the class. A mistake is found by the compiler before the program starts, which is the reason the compiler is not optional in this course.
javac -Xlint:all Build.java
java Build
The class is named after its file, and the run names the class, not the file: java Build asks for Build.class, not for Build.java. Keep the two names equal and the loop never surprises you.
The editor
Any editor that saves plain UTF-8 text works. What a Java-aware editor adds is a parse of the file while you type, so a missing semicolon or a misspelled name is underlined before you run anything, and an outline of the classes and methods in the file.
IntelliJ IDEA is the editor this site uses for Java. The Community edition is free and runs on Linux, macOS and Windows; it reads a folder as a project, compiles on save and can run a class with one click. It does not remove the need to know javac: the build output of a broken project is the compiler output, and reading it is a skill this course teaches.
A terminal, the editor and the compiler in one window is enough for every lesson here. The IDE becomes useful when a project has hundreds of files; the commands stay the same.
javac and the output folder
The program below has a main method and a helper method that returns a string. The helper is static because main is, and a static method can call another static method without an object.
The file Build.java:
public class Build {
public static void main(String[] args) {
System.out.println(greeting("Java"));
System.out.println(greeting("21"));
}
static String greeting(String name) {
return "Hello, " + name;
}
}
-d out tells the compiler to write the class file into the folder out, creating it if needed. Without the flag the class file lands next to the source; with it, the source folder stays clean and the output of a whole project sits in one place.
javac -Xlint:all -d out Build.java
ls out
java -cp out Build
Build.class
Hello, Java
Hello, 21
The run needs -cp out, the class path, because the class is not in the current folder. The class path is the list of folders and jars the JVM searches for a class, and the current folder is its default; lesson 006 takes it apart.
The class file
One source file produced one class file. There is no separate header file and no linker step: the class file holds the bytecode of every method of the class, the names of the methods and fields it calls, and a constant pool of strings and references. It is not compressed text; it is a binary format, and javap is the reader.
ls -l out/Build.class
-rw-r--r-- 1 agent agent 954 Nov 19 21:48 out/Build.class
The size of this file is not the size of the program: most of it is the constant pool and the description of the class, which every class file needs before it holds a single instruction.
javap reads the bytecode back
javap -c prints the bytecode of every method as a list of instructions, one per line, with the constant pool entry each one uses. Read it as a stack machine: getstatic pushes the System.out field, ldc pushes a constant, invokestatic calls the helper, and invokevirtual calls println.
javap -c -cp out Build
Compiled from "Build.java"
public class Build {
public Build();
Code:
0: aload_0
1: invokespecial #1 // Method java/lang/Object."<init>":()V
4: return
public static void main(java.lang.String[]);
Code:
0: getstatic #7 // Field java/lang/System.out:Ljava/io/PrintStream;
3: ldc #13 // String Java
5: invokestatic #15 // Method greeting:(Ljava/lang/String;)Ljava/lang/String;
8: invokevirtual #21 // Method java/io/PrintStream.println:(Ljava/lang/String;)V
11: getstatic #7 // Field java/lang/System.out:Ljava/io/PrintStream;
14: ldc #27 // String 21
16: invokestatic #15 // Method greeting:(Ljava/lang/String;)Ljava/lang/String;
19: invokevirtual #21 // Method java/io/PrintStream.println:(Ljava/lang/String;)V
22: return
static java.lang.String greeting(java.lang.String);
Code:
0: aload_0
1: invokedynamic #29, 0 // InvokeDynamic #0:makeConcatWithConstants:(Ljava/lang/String;)Ljava/lang/String;
6: areturn
}
Two details are worth reading twice. The constructor Build() exists although the source never declares one: the compiler adds a default constructor that calls the constructor of Object. And the string concatenation in greeting is a single invokedynamic instruction, not a chain of StringBuilder calls: since Java 9 the compiler emits one call site and the runtime decides how to concatenate.
javadoc in one line
javadoc reads the same source and writes HTML documentation for every public class and member that has a comment. It runs without a comment at all, and then warns about each one:
javadoc -quiet -d doc Build.java
ls doc/index.html
Build.java:1: warning: no comment
public class Build {
^
Build.java:1: warning: use of default constructor, which does not provide a comment
public class Build {
^
Build.java:2: warning: no comment
public static void main(String[] args) {
^
3 warnings
doc/index.html
The warnings are not errors: the HTML in doc is complete and doc/index.html is its entry point. A comment written as /** ... */ just above a class or a method is what turns a warning into a description.
Exercises
- Build Build.java without -d and compare the folder with the one the lesson made.
- Run java -cp out Build from the parent folder. Why does it still work?
- Add a comment in /** ... */ form above the class and run javadoc again. Which warning disappears?
- Run javap -c -cp out Build and find the instruction that pushes the string Java.
Questions
A new set of questions in a new order every time. Choose one answer for each, then check.
-
What does
-d outdo injavac -d out Build.java?Answer
It writes the class file into the folder out
-d names the output folder for class files, which javac creates when it is missing.
-
What does
javap -cprint?Answer
The bytecode of the methods of a class file
javap reads a class file; -c adds the disassembled bytecode of each method.
-
What does this program print?
public class Q1 { public static void main(String[] args) { System.out.println(greet("Ada")); } static String greet(String name) { return "Hello, " + name; } }Answer
Hello, Ada
greeting returns the concatenation of the literal and the argument, and println writes it.
-
This program is built and run with
java Q2 one two three. What does it print?public class Q2 { public static void main(String[] args) { System.out.println("args: " + args.length); } }Answer
args: 3
The three words after the class name are the three elements of args; the class name itself is not one of them.
-
Which file does
java Buildload?Answer
Build.class, the compiled class
The java command runs a compiled class; compiling a source file takes javac or the source launcher.
-
Where does javac put the class file when the source is built with no
-d?Answer
Next to the source file
Without -d the class file is written into the folder of the source file.
-
What does this program print?
public class Q3 { public static void main(String[] args) { int total = add(2, 3) + add(4, 5); System.out.println(total); } static int add(int a, int b) { return a + b; } }Answer
14
add(2, 3) is 5 and add(4, 5) is 9, so the sum is 14.
-
What did the constructor in the javap output of Build come from?
Answer
The compiler adds a default constructor when the source declares none
A class with no constructor gets a default one that calls the constructor of its superclass.
-
What does
javadoc -quiet -d doc Build.javado with a class that has no comments?Answer
It writes the HTML and warns about each missing comment
Missing comments are warnings, so the documentation is written and the warnings are listed.
-
The program builds, but
java Buildsays it cannot find the class. What is the likely cause?Answer
The class file was written to out and the run needs -cp out
With -d out the class is not in the current folder, so the class path has to name out.
The code on this page was built with OpenJDK 21.0.13 (javac and java) and run on Alpine Linux in a Docker container.
Article author: Arthur Isaev