Warnings, errors and -Xlint:all
javac writes three kinds of message. An error stops the build and no class file is written. A warning names something legal but probably wrong, and the build continues. A note is extra information about a message that came before it. Every message has the same shape, and reading that shape is most of the skill.
The shape of a diagnostic
A diagnostic starts with the file, the line and the column, then the kind, then the message: File.java:3: error: .... The source line follows, and a caret under it points at the token the compiler is talking about. When the message needs more room, indented detail lines follow the caret. The last line counts the errors and the warnings.
The compiler stops after the first stage that fails. A syntax error hides every type error in the same file, so fix the first error, build again, and read the next one; a build that reports twenty errors is usually one cause and nineteen consequences.
Raw types and unchecked calls
Generics exist since Java 5, and old code still uses a List without a type argument. The compiler accepts it, because it must compile old code, and warns: the list is a raw type, and every call that puts something into it is unchecked. The program below is correct Java and prints its list, with four warnings because there are four separate raw uses.
The file Unchecked.java:
import java.util.ArrayList;
import java.util.List;
public class Unchecked {
public static void main(String[] args) {
List names = new ArrayList();
names.add("one");
names.add(2);
System.out.println(names);
}
}
javac -Xlint:all Unchecked.java
java Unchecked
Unchecked.java:6: warning: [rawtypes] found raw type: List
List names = new ArrayList();
^
missing type arguments for generic class List<E>
where E is a type-variable:
E extends Object declared in interface List
Unchecked.java:6: warning: [rawtypes] found raw type: ArrayList
List names = new ArrayList();
^
missing type arguments for generic class ArrayList<E>
where E is a type-variable:
E extends Object declared in class ArrayList
Unchecked.java:7: warning: [unchecked] unchecked call to add(E) as a member of the raw type List
names.add("one");
^
where E is a type-variable:
E extends Object declared in interface List
Unchecked.java:8: warning: [unchecked] unchecked call to add(E) as a member of the raw type List
names.add(2);
^
where E is a type-variable:
E extends Object declared in interface List
4 warnings
[one, 2]
The square brackets name the warning group, which is what -Xlint:all turns on; without the flag javac prints the first two lines of each warning and drops the detail. The fix is one type argument, and the generics part of the course explains why the compiler cannot check a raw list: at run time the type argument is gone, so the check has to happen at compile time or not at all.
A deprecated constructor
A deprecated member is one the library wants you to stop using. The compiler prints a [removal] warning, which is stronger than the older [deprecation]: the member is marked for removal in a future release. The constructor new Integer(42) is such a member, because Integer.valueOf(42) does the same and may reuse a cached object.
public class Deprecated {
public static void main(String[] args) {
Integer boxed = new Integer(42);
System.out.println(boxed);
}
}
javac -Xlint:all Deprecated.java
java Deprecated
Deprecated.java:3: warning: [removal] Integer(int) in Integer has been deprecated and marked for removal
Integer boxed = new Integer(42);
^
1 warning
42
The warning names the member as the compiler sees it, Integer(int) in Integer, and the caret points at the call, not at the variable. The program still runs and prints 42, which is why a warning is easy to ignore and worth reading: a later JDK removes the constructor, and then the same line is an error.
Turning warnings into errors
-Werror makes every warning a build failure. The class file is not written, and the shell sees status 1. A project that builds with this flag cannot accumulate warnings, which is the reason the flag exists:
javac -Xlint:all -Werror Deprecated.java
Deprecated.java:3: warning: [removal] Integer(int) in Integer has been deprecated and marked for removal
Integer boxed = new Integer(42);
^
error: warnings found and -Werror specified
1 error
1 warning
Both counts are on the last two lines: the warning is counted, and so is the error that it became. The fix is the one the warning suggests, and with it the same flags build a clean class:
public class Boxed {
public static void main(String[] args) {
Integer boxed = Integer.valueOf(42);
System.out.println(boxed);
}
}
javac -Xlint:all -Werror Boxed.java
java Boxed
42
A clean build prints nothing
Silence is the good result. A file with no warning and no error produces no output at all, and the commands below show that this is not the same as a failed build: the class file appears and runs.
public class Quiet {
public static void main(String[] args) {
int answer = 6 * 7;
System.out.println(answer);
}
}
javac -Xlint:all Quiet.java
java Quiet
42
If javac prints nothing and no class file appears, the command was given a file that does not exist; javac says that too, with error: file not found. The two outcomes are easy to tell apart once the build has a status: 0 means a class file was written.
A real error
A misspelled method is an error, not a warning, and the message names both the symbol and where the compiler looked for it. The detail lines are the useful half: symbol says what was not found, location says which type should have had it.
public class Symbol {
public static void main(String[] args) {
System.out.printline("typo");
}
}
javac -Xlint:all Symbol.java
Symbol.java:3: error: cannot find symbol
System.out.printline("typo");
^
symbol: method printline(String)
location: variable out of type PrintStream
1 error
System.out is a PrintStream, and that class has println and print, not printline. The compiler does not suggest a spelling, but the location line points at the class whose documentation answers the question.
Exercises
- Add a type argument to the list in Unchecked.java and build again. How many warnings are left, and which ones?
- Replace new Integer(42) with Integer.valueOf(42) and build with -Werror. What does the build print?
- Add two more errors to Broken.java from the previous lesson and count how many of them javac reports in one build.
- Build Quiet.java with no flag and with -Xlint:all -Werror. Compare the two commands and their status.
Questions
A new set of questions in a new order every time. Choose one answer for each, then check.
-
What does
-Xlint:allturn on?Answer
Every compiler warning group
-Xlint:all enables all warning groups; errors are always reported.
-
What does
-Werrordo to a warning?Answer
It makes the build fail and writes no class file
With -Werror a warning is an error, so the build stops with status 1.
-
Which warning group does a
Listwith no type argument produce?Answer
[rawtypes]
The declaration of a generic type without its type arguments is a raw type, group rawtypes.
-
What does this program print?
import java.util.ArrayList; import java.util.List; public class Q1 { public static void main(String[] args) { List names = new ArrayList(); names.add("one"); names.add(2); System.out.println(names); } }Answer
[one, 2]
A raw list accepts both values, and its toString writes them in brackets, separated by a comma and a space.
-
What does this program print?
public class Q2 { public static void main(String[] args) { Integer boxed = new Integer(42); System.out.println(boxed + 1); } }Answer
43
The deprecated constructor still returns an Integer, and unboxing it for the addition gives 42 + 1.
-
What is the first line of the compiler output for this program?
public class Q3 { public static void main(String[] args) { System.out.printline("typo"); } }Answer
error: cannot find symbol
PrintStream has no printline method, and an unknown method is an error, not a warning.
-
What does this program print?
public class Q4 { public static void main(String[] args) { System.out.println(6 * 7); } }Answer
42
The multiplication is evaluated before println sees the value.
-
What does this program print?
import java.util.ArrayList; import java.util.List; public class Q5 { public static void main(String[] args) { List<String> names = new ArrayList<>(); names.add("a"); names.add("b"); System.out.println(names + " size " + names.size()); } }Answer
[a, b] size 2
The typed list holds two strings, and the concatenation joins its toString with the size.
-
A build with
-Werrorprints one warning and then1 error. What happened?Answer
The warning became the error, and no class file was written
Under -Werror each warning is also an error, so the two counts describe the same message.
-
What does the caret line under a source line point at?
Answer
The token the compiler is talking about
The caret is placed under the column the diagnostic refers to, which is why the source line is printed above it.
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