javac -Xlint: show all compiler warnings (unchecked, deprecation, rawtypes)
- tool
- Java
- command
- javac
- flag
- -Xlint[:key,...]
- default
- only some warnings (such as removal) are printed; unchecked and deprecation are summarised in a Note
javac -Xlint Main.java enables all recommended compiler warnings and prints each one with its location, including the ones javac otherwise only summarises as Note: Main.java uses unchecked or unsafe operations. Note: Recompile with -Xlint:unchecked for details. You can also pick categories: -Xlint:unchecked,deprecation, or everything except some: -Xlint:all,-serial. Combine with -Werror to make warnings fail the build.
Default versus -Xlint versus -Werror
cat > Lint.java <<'EOF'
import java.util.ArrayList;
import java.util.List;
public class Lint {
static List raw() { return new ArrayList(); }
public static void main(String[] args) {
List<String> names = raw();
Integer boxed = new Integer(5);
switch (args.length) {
case 0: System.out.println("none");
case 1: System.out.println("one");
}
System.out.println(names.size() + boxed);
}
}
EOF
echo "== default"
javac -d out Lint.java; echo "exit: $?"
echo "== -Xlint:unchecked,fallthrough"
javac -Xlint:unchecked,fallthrough -d out Lint.java 2>&1 | grep 'warning:'; echo "exit: ${PIPESTATUS[0]}"
echo "== -Xlint"
javac -Xlint -d out Lint.java 2>&1 | grep -E 'warning:|warnings$'
echo "== -Xlint:all,-rawtypes -Werror"
javac -Xlint:all,-rawtypes -Werror -d out Lint.java 2>&1 | grep -E 'warning:|error'; echo "exit: ${PIPESTATUS[0]}"Output
== default
Lint.java:9: warning: [removal] Integer(int) in Integer has been deprecated and marked for removal
Integer boxed = new Integer(5);
^
Note: Lint.java uses unchecked or unsafe operations.
Note: Recompile with -Xlint:unchecked for details.
1 warning
exit: 0
== -Xlint:unchecked,fallthrough
Lint.java:8: warning: [unchecked] unchecked conversion
Lint.java:9: warning: [removal] Integer(int) in Integer has been deprecated and marked for removal
Lint.java:12: warning: [fallthrough] possible fall-through into case
exit: 0
== -Xlint
Lint.java:5: warning: [rawtypes] found raw type: List
Lint.java:5: warning: [rawtypes] found raw type: ArrayList
Lint.java:8: warning: [unchecked] unchecked conversion
Lint.java:9: warning: [removal] Integer(int) in Integer has been deprecated and marked for removal
Lint.java:12: warning: [fallthrough] possible fall-through into case
5 warnings
== -Xlint:all,-rawtypes -Werror
Lint.java:8: warning: [unchecked] unchecked conversion
Lint.java:9: warning: [removal] Integer(int) in Integer has been deprecated and marked for removal
error: warnings found and -Werror specified
1 error
exit: 1By default only the [removal] warning appears, and the unchecked conversion is hidden behind a Note. -Xlint lists the raw types, the unchecked conversion, the removal and the switch fall-through. -Werror turns the run into a failure (exit 1) with error: warnings found and -Werror specified. Disabling rawtypes with -rawtypes removes those two warnings, and in this run javac stopped before it reported the fall-through, so fix warnings in the order they appear and re-run.
Frequent keys
| Key | Warns about |
|---|---|
| all | Every category (then subtract with -key) |
| none | No categories |
| unchecked | Unchecked generic conversions and calls, usually from raw types |
| rawtypes | Use of a generic type without type arguments (List instead of List<String>) |
| deprecation | Use of @Deprecated APIs |
| removal | Use of APIs deprecated for removal (on by default) |
| fallthrough | A switch case that falls into the next one without break |
| cast | Redundant casts |
| static | Static members accessed through an instance |
| serial | Serializable classes without serialVersionUID |
| try | Problems with try-with-resources, such as an unused resource |
| options | Problems with the command-line options themselves (e.g. -source without bootstrap class path) |
javac --help-lint prints the keys your JDK supports. A minus sign disables a key (-Xlint:-options), and keys combine with commas.
In build tools
<configuration>
<compilerArgs>
<arg>-Xlint:all,-serial</arg>
<arg>-Werror</arg>
</compilerArgs>
</configuration>tasks.withType<JavaCompile>().configureEach {
options.compilerArgs.addAll(listOf("-Xlint:all,-serial", "-Werror"))
}Pitfalls
- Warnings are not errors:
-Xlintnever finds problems such as cannot find symbol, which already stop the build. -Werrorwith-Xlint:allcan break the build on a JDK upgrade, because newer JDKs add categories and deprecate APIs. Pin the JDK in CI, or exclude noisy categories explicitly.@SuppressWarningstakes the same key names as-Xlint(unchecked,rawtypes,deprecation...). IDEs and static analysers add names of their own, which javac ignores.- When migrating to a newer target with javac --release,
-Xlint:deprecation,removalshows which APIs to replace before they disappear.
FAQ
What does Note: Recompile with -Xlint:unchecked for details mean?
javac found unchecked generic operations, usually raw types such as List instead of List<String>, but only prints a summary note by default. Compile with -Xlint:unchecked (or -Xlint) to see each place with its line number.
How do I make javac warnings fail the build?
Add -Werror. It turns every reported warning into a reason to fail, so combine it with the -Xlint keys you want: javac -Xlint:all,-serial -Werror.
How do I hide one warning in the code?
Annotate the declaration with @SuppressWarnings("unchecked") (or rawtypes, deprecation, removal, fallthrough...). Put it on the smallest scope possible, ideally a single local variable declaration.