1 --- 2 # Copyright (c) 1994, 2024, Oracle and/or its affiliates. All rights reserved. 3 # DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 4 # 5 # This code is free software; you can redistribute it and/or modify it 6 # under the terms of the GNU General Public License version 2 only, as 7 # published by the Free Software Foundation. 8 # 9 # This code is distributed in the hope that it will be useful, but WITHOUT 10 # ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 11 # FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 12 # version 2 for more details (a copy is included in the LICENSE file that 13 # accompanied this code). 14 # 15 # You should have received a copy of the GNU General Public License version 16 # 2 along with this work; if not, write to the Free Software Foundation, 17 # Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 18 # 19 # Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 20 # or visit www.oracle.com if you need additional information or have any 21 # questions. 22 # 23 24 title: 'JAVAC(1) JDK @@VERSION_SHORT@@ | JDK Commands' 25 date: @@COPYRIGHT_YEAR@@ 26 lang: en 27 --- 28 29 ## Name 30 31 javac - read Java declarations and compile them into class files 32 33 ## Synopsis 34 35 `javac` \[*options*\] \[*sourcefiles-or-classnames*\] 36 37 *options* 38 : Command-line options. 39 40 *sourcefiles-or-classnames* 41 : Source files to be compiled (for example, `Shape.java`) or 42 the names of previously compiled classes to be processed for annotations 43 (for example, `geometry.MyShape`). 44 45 ## Description 46 47 The `javac` command reads _source files_ that contain module, package and type 48 declarations written in the Java programming language, and compiles them into _class files_ 49 that run on the Java Virtual Machine. 50 51 The `javac` command can also [process annotations](#annotation-processing) 52 in Java source files and classes. 53 54 Source files must have a file name extension of `.java`. 55 Class files have a file name extension of `.class`. 56 Both source and class files normally have file names that identify the contents. 57 For example, a class called `Shape` would be declared in a source file 58 called `Shape.java`, and compiled into a class file called `Shape.class`. 59 60 There are two ways to specify source files to `javac`: 61 62 - For a small number of source files, you can list their file names on 63 the command line. 64 65 - For a large number of source files, you can use the [`@`*filename*](#option-at) 66 option on the command line to specify an _argument file_ that lists 67 their file names. See [Standard Options] for a description of the 68 option and [Command-Line Argument Files] for a description of 69 `javac` argument files. 70 71 The order of source files specified on the command line or in an 72 argument file is not important. `javac` will compile the files together, 73 as a group, and will automatically resolve any dependencies between 74 the declarations in the various source files. 75 76 `javac` expects that source files are arranged in one or more directory 77 hierarchies on the file system, described in [Arrangement of Source 78 Code]. 79 80 To compile a source file, `javac` needs to find the declaration of 81 every class or interface that is used, extended, or implemented by the 82 code in the source file. This lets `javac` check that the code has the 83 right to access those classes and interfaces. Rather than specifying 84 the source files of those classes and interfaces explicitly, you can 85 use command-line options to tell `javac` where to search for their 86 source files. If you have compiled those source files previously, you 87 can use options to tell `javac` where to search for the corresponding 88 class files. The options, which all have names ending in "path", are 89 described in [Standard Options], and further described in 90 [Configuring a Compilation] and [Searching for Module, Package and Type Declarations]. 91 92 By default, `javac` compiles each source file to a class file in the 93 same directory as the source file. However, it is recommended to 94 specify a separate destination directory with the [`-d`](#option-d) option. 95 96 Command-line [options] and [environment variables] also control how 97 `javac` performs various tasks: 98 99 - Compiling code to run on earlier releases of the JDK. 100 - Compiling code to run under a debugger. 101 - Checking for stylistic issues in Java source code. 102 - Checking for problems in `javadoc` comments (`/** ... */`). 103 - Processing annotations in source files and class files. 104 - Upgrading and patching modules in the compile-time environment. 105 106 `javac` supports [Compiling for Earlier Releases Of The Platform] 107 and can also be invoked from Java code using one of a number of [APIs] 108 109 ## Options 110 111 `javac` provides [standard options], and [extra options] that are either 112 non-standard or are for advanced use. 113 114 Some options take one or more arguments. 115 If an argument contains spaces or other whitespace characters, 116 the value should be quoted according to the conventions of the 117 environment being used to invoke javac. 118 If the option begins with a single dash (`-`) the argument should 119 either directly follow the option name, or should be separated with a 120 colon (`:`) or whitespace, depending on the option. If the option begins with 121 a double dash (`--`), the argument may be separated either by whitespace 122 or by an equals (`=`) character with no additional whitespace. 123 For example, 124 125 -Aname="J. Duke" 126 -proc:only 127 -d myDirectory 128 --module-version 3 129 --module-version=3 130 131 In the following lists of options, an argument of *path* represents 132 a search path, composed of a list of file system locations separated 133 by the platform path separator character, (semicolon `;` on Windows, 134 or colon `:` on other systems.) Depending on the option, the 135 file system locations may be directories, JAR files or JMOD files. 136 137 ### Standard Options 138 139 <a id="option-at">`@`*filename*</a> 140 : Reads options and file names from a file. To shorten or simplify the 141 `javac` command, you can specify one or more files that contain arguments 142 to the `javac` command (except [`-J`](#option-J) options). This lets you to create 143 `javac` commands of any length on any operating system. 144 See [Command-Line Argument Files]. 145 146 <a id="option-A">`-A`*key*\[`=`*value*\]</a> 147 : Specifies options to pass to annotation processors. These options are not 148 interpreted by `javac` directly, but are made available for use by 149 individual processors. The *key* value should be one or more identifiers 150 separated by a dot (`.`). 151 152 <a id="option-add-modules">`--add-modules` *module*`,`*module*</a> 153 : Specifies root modules to resolve in addition to the initial modules, or 154 all modules on the module path if *module* is `ALL-MODULE-PATH`. 155 156 <a id="option-boot-class-path">`--boot-class-path` *path* or `-bootclasspath` *path*</a> 157 : Overrides the location of the bootstrap class files. 158 159 **Note:** This can only be used when compiling for releases prior to JDK 9. 160 As applicable, see the descriptions in [`--release`](#option-release), [`-source`](#option-source), or 161 [`-target`](#option-target) for details. For JDK 9 or later, see [`--system`](#option-system). 162 163 <a id="option-class-path">`--class-path` *path*, `-classpath` *path*, or `-cp` *path*</a> 164 : Specifies where to find user class files and annotation processors. This 165 class path overrides the user class path in the `CLASSPATH` environment 166 variable. 167 168 - If `--class-path`, `-classpath`, or `-cp` are not specified, then the 169 user class path is the value of the `CLASSPATH` environment variable, 170 if that is set, or else the current directory. 171 172 - If not compiling code for modules, if the [`--source-path`](#option-source-path) or -sourcepath` 173 option is not specified, then the user class path is also searched for source files. 174 175 - If the [`-processorpath`](#option-processor-path) option is not specified, then the class path is 176 also searched for annotation processors. 177 178 <a id="option-d">`-d` *directory*</a> 179 : Sets the destination directory (or _class output directory_) for class files. 180 If a class is part of a package, then `javac` puts the class file in a 181 subdirectory that reflects the module name (if appropriate) and package name. 182 The directory, and any necessary subdirectories, will be created if they 183 do not already exist. 184 185 If the `-d` option is not specified, then `javac` puts each class file in 186 the same directory as the source file from which it was generated. 187 188 Except when compiling code for multiple modules, the contents of the 189 class output directory will be organized in a package hierarchy. 190 When compiling code for multiple modules, the contents of the output 191 directory will be organized in a module hierarchy, with the contents of each 192 module in a separate subdirectory, each organized as a package 193 hierarchy. 194 195 **Note:** 196 When compiling code for one or more modules, the class output directory will 197 automatically be checked when searching for previously compiled classes. 198 When not compiling for modules, for backwards compatibility, 199 the directory is _not_ automatically checked for previously compiled classes, 200 and so it is recommended to specify the class output directory as one 201 of the locations on the user class path, using the `--class-path` option or one of 202 its alternate forms. 203 204 <a id="option-deprecation">`-deprecation`</a> 205 : Shows a description of each use or override of a deprecated member or 206 class. Without the `-deprecation` option, `javac` shows a summary of the 207 source files that use or override deprecated members or classes. The 208 `-deprecation` option is shorthand for `-Xlint:deprecation`. 209 210 <a id="option-enable-preview">`--enable-preview`</a> 211 : Enables preview language features. Used in conjunction with either 212 [`-source`](#option-source) or [`--release`](#option-release). 213 214 <a id="option-encoding">`-encoding` *encoding*</a> 215 : Specifies character encoding used by source files, such as EUC-JP and 216 UTF-8. If the `-encoding` option is not specified, then the platform default 217 converter is used. 218 219 <a id="option-endorseddirs">`-endorseddirs` *directories*</a> 220 : Overrides the location of the endorsed standards path. 221 222 **Note:** This can only be used when compiling for releases prior to JDK 9. 223 As applicable, see the descriptions in [`--release`](#option-release), [`-source`](#option-source), or 224 [`-target`](#option-target) for details. 225 226 <a id="option-extdirs">`-extdirs` *directories*</a> 227 : Overrides the location of the installed extensions. 228 `directories` is a list of directories, separated by the platform path separator 229 (`;` on Windows, and `:` otherwise). 230 Each JAR file in the specified directories is searched for class files. 231 All JAR files found become part of the class path. 232 233 If you are compiling for a release of the platform that supports the 234 Extension Mechanism, then this option specifies the directories that 235 contain the extension classes. 236 See [Compiling for Other Releases of the Platform]. 237 238 **Note:** This can only be used when compiling for releases prior to JDK 9. 239 As applicable, see the descriptions in [`--release`](#option-release), [`-source`](#option-source), or 240 [`-target`](#option-target) for details. 241 242 <a id="option-g">`-g`</a> 243 : Generates all debugging information, including local variables. By default, 244 only line number and source file information is generated. 245 246 <a id="option-g-custom">`-g:`\[`lines`, `vars`, `source`\]</a> 247 : Generates only the kinds of debugging information specified by the 248 comma-separated list of keywords. Valid keywords are: 249 250 `lines` 251 : Line number debugging information. 252 253 `vars` 254 : Local variable debugging information. 255 256 `source` 257 : Source file debugging information. 258 259 <a id="option-g-none">`-g:none`</a> 260 : Does not generate debugging information. 261 262 <a id="option-h">`-h` *directory*</a> 263 : Specifies where to place generated native header files. 264 265 When you specify this option, a native header file is generated for each 266 class that contains native methods or that has one or more constants 267 annotated with the [`java.lang.annotation.Native`]( 268 ../../api/java.base/java/lang/annotation/Native.html) 269 annotation. If the class is part of a package, then the compiler puts the 270 native header file in a subdirectory that reflects the module name 271 (if appropriate) and package name. 272 The directory, and any necessary subdirectories, will be created if they 273 do not already exist. 274 275 <a id="option-help">`--help`, `-help` or `-?`</a> 276 : Prints a synopsis of the standard options. 277 278 <a id="option-help-extra">`--help-extra` or `-X`</a> 279 : Prints a synopsis of the set of extra options. 280 281 <a id="option-help-lint">`--help-lint`</a> 282 : Prints the supported keys for the `-Xlint` option. 283 284 <a id="option-implicit">`-implicit:`\[`none`, `class`\]</a> 285 : Specifies whether or not to generate class files for implicitly referenced 286 files: 287 288 - `-implicit:class` --- Automatically generates class files. 289 290 - `-implicit:none` --- Suppresses class file generation. 291 292 If this option is not specified, then the default automatically generates 293 class files. In this case, the compiler issues a warning if any class files 294 are generated when also doing annotation processing. The warning is not 295 issued when the `-implicit` option is explicitly set. 296 See [Searching for Module, Package and Type Declarations]. 297 298 <a id="option-J">`-J`*option*</a> 299 : Passes *option* to the runtime system, where *option* is one of the Java 300 options described on [java](java.html) command. For example, `-J-Xms48m` 301 sets the startup memory to 48 MB. 302 303 **Note:** The `CLASSPATH` environment variable, `-classpath` option, `-bootclasspath` 304 option, and `-extdirs` option do not specify the classes used to run 305 `javac`. Trying to customize the compiler implementation with these options 306 and variables is risky and often does not accomplish what you want. If you 307 must customize the compiler implementation, then use the `-J` option to 308 pass options through to the underlying Java launcher. 309 310 <a id="option-limit-modules">`--limit-modules` *module*`,`*module*\*</a> 311 : Limits the universe of observable modules. 312 313 <a id="option-module">`--module` *module-name* (`,`*module-name*)* or `-m` *module-name* (`,`*module-name*)*</a> 314 : Compiles those source files in the named modules that are newer 315 than the corresponding files in the output directory. 316 317 <a id="option-module-path">`--module-path` *path* or `-p` *path*</a> 318 : Specifies where to find application modules. 319 320 <a id="option-module-source-path">`--module-source-path` *module-source-path*</a> 321 : Specifies where to find source files when compiling code 322 in multiple modules. See [The Module Source Path Option]. 323 324 <a id="option-module-version">`--module-version` *version*</a> 325 : Specifies the version of modules that are being compiled. 326 327 <a id="option-nowarn">`-nowarn`</a> 328 : Generate only mandatory warnings. 329 330 <a id="option-parameters">`-parameters`</a> 331 : Generates metadata for reflection on method parameters. Stores formal 332 parameter names of constructors and methods in the generated class file so 333 that the method `java.lang.reflect.Executable.getParameters` from the 334 Reflection API can retrieve them. 335 336 <a id="option-proc">`-proc:`\[`none`, `only`, `full`\]</a> 337 : Controls whether annotation processing and compilation are done. 338 339 - `-proc:none` means that compilation takes place without annotation 340 processing 341 342 - `-proc:only` means that only annotation processing is done, 343 without any subsequent compilation. 344 345 - `-proc:full` means annotation processing and compilation are done. 346 347 If this option is not used, annotation processing and compilation 348 are done if at least one other option is used to explicitly 349 configure annotation processing. 350 351 <a id="option-processor">`-processor` *class1*\[`,`*class2*`,`*class3*...\]</a> 352 : Names of the annotation processors to run. This bypasses the default 353 discovery process. 354 355 <a id="option-processor-module-path">`--processor-module-path` *path*</a> 356 : Specifies the module path used for finding annotation processors. 357 358 <a id="option-processor-path">`--processor-path` *path* or `-processorpath` *path*</a> 359 : Specifies where to find annotation processors. If this option is not used, 360 then the class path is searched for processors. 361 362 <a id="option-profile">`-profile` *profile*</a> 363 : Checks that the API used is available in the specified profile. 364 This option is deprecated and may be removed in a future release. 365 366 **Note:** This can only be used when compiling for releases prior to JDK 9. 367 As applicable, see the descriptions in [`--release`](#option-release), [`-source`](#option-source), 368 or [`-target`](#option-target) for details. 369 370 <a id="option-release">`--release` *release*</a> 371 : Compiles source code according to the rules of the Java programming language 372 for the specified Java SE release, generating class files which target 373 that release. 374 Source code is compiled against the combined Java SE and JDK API for the 375 specified release. 376 377 The supported values of *release* are the current Java SE release and a 378 limited number of previous releases, detailed in the command-line help. 379 380 For the current release, the Java SE API consists of the `java.*`, 381 `javax.*`, and `org.*` packages that are exported by the Java SE modules in 382 the release; the JDK API consists of the `com.*` and `jdk.*` packages that 383 are exported by the JDK modules in the release, plus the `javax.*` packages 384 that are exported by standard, but non-Java SE, modules in the release. 385 386 For previous releases, the Java SE API and the JDK API are as defined in 387 that release. 388 389 **Note:** When using `--release`, you cannot also use the [`--source`](#option-source)/`-source` or 390 [`--target`](#option-target)/`-target` options. 391 392 **Note:** When using `--release` to specify a release that supports the Java 393 Platform Module System, the `--add-exports` option cannot be used to 394 enlarge the set of packages exported by the Java SE, JDK, and standard 395 modules in the specified release. 396 397 <a id="option-s">`-s` *directory*</a> 398 : Specifies the directory used to place the generated source files. If a 399 class is part of a package, then the compiler puts the source file in a 400 subdirectory that reflects the module name (if appropriate) and package name. 401 The directory, and any necessary subdirectories, will be created if they 402 do not already exist. 403 404 Except when compiling code for multiple modules, the contents of the 405 source output directory will be organized in a package hierarchy. 406 When compiling code for multiple modules, the contents of the source output directory will be 407 organized in a module hierarchy, with the contents of each 408 module in a separate subdirectory, each organized as a package 409 hierarchy. 410 411 <a id="option-source">`--source` *release* or `-source` *release*</a> 412 : Compiles source code according to the rules of the Java programming language 413 for the specified Java SE release. 414 The supported values of *release* are the current Java SE release and a 415 limited number of previous releases, detailed in the command-line help. 416 417 If the option is not specified, the default is to compile source code 418 according to the rules of the Java programming language for the current 419 Java SE release. 420 421 <a id="option-source-path">`--source-path` *path* or `-sourcepath` *path*</a> 422 : Specifies where to find source files. 423 Except when compiling multiple modules together, this is the source code path 424 used to search for class or interface definitions. 425 426 **Note:** Classes found through the class path might be recompiled when their source 427 files are also found. See [Searching for Module, Package and Type Declarations]. 428 429 <a id="option-system">`--system` *jdk* \| `none`</a> 430 : Overrides the location of system modules. 431 432 <a id="option-target">`--target` *release* or `-target` *release*</a> 433 : Generates `class` files suitable for the specified Java SE release. 434 The supported values of *release* are the current Java SE release and a 435 limited number of previous releases, detailed in the command-line help. 436 437 **Note:** The target release must be equal to or higher than the source release. 438 (See [`--source`](#option-source).) 439 440 <a id="option-upgrade-module-path">`--upgrade-module-path` *path*</a> 441 : Overrides the location of upgradeable modules. 442 443 <a id="option-verbose">`-verbose`</a> 444 : Outputs messages about what the compiler is doing. Messages include 445 information about each class loaded and each source file compiled. 446 447 <a id="option-version">`--version` or `-version`</a> 448 : Prints version information. 449 450 <a id="option-Werror">`-Werror`</a> 451 : Terminates compilation when warnings occur. 452 453 ### Extra Options 454 455 <a id="option-add-exports">`--add-exports` *module*`/`*package*`=`*other-module*(`,`*other-module*)\*</a> 456 : Specifies a package to be considered as exported from its defining module 457 to additional modules or to all unnamed modules when the value of 458 *other-module* is `ALL-UNNAMED`. 459 460 <a id="option-add-reads">`--add-reads` *module*`=`*other-module*(`,`*other-module*)\*</a> 461 : Specifies additional modules to be considered as required by a given 462 module. 463 464 <a id="option-default-module-for-created-files">`--default-module-for-created-files` *module-name*</a> 465 : Specifies the fallback target module for files created by annotation 466 processors, if none is specified or inferred. 467 468 <a id="option-disable-line-doc-comments">`--disable-line-doc-comments`</a> 469 : Disables support for documentation comments with lines beginning ///. 470 471 <a id="option-Djava.endorsed.dirs">`-Djava.endorsed.dirs=`*dirs*</a> 472 : Overrides the location of the endorsed standards path. 473 474 **Note:** This can only be used when compiling for releases prior to JDK 9. 475 As applicable, see the descriptions in [`--release`](#option-release), [`-source`](#option-source), 476 or [`-target`](#option-target) for details. 477 478 <a id="option-Djava.ext.dirs">`-Djava.ext.dirs=`*dirs*</a> 479 : Overrides the location of installed extensions. 480 481 **Note:** This can only be used when compiling for releases prior to JDK 9. 482 As applicable, see the descriptions in [`--release`](#option-release), [`-source`](#option-source), 483 or [`-target`](#option-target) for details. 484 485 <a id="option-patch-module">`--patch-module` *module*`=`*path*</a> 486 : Overrides or augments a module with classes and resources in JAR files or 487 directories. 488 489 <a id="option-Xbootclasspath">`-Xbootclasspath:`*path*</a> 490 : Overrides the location of the bootstrap class files. 491 492 **Note:** This can only be used when compiling for releases prior to JDK 9. 493 As applicable, see the descriptions in [`--release`](#option-release), [`-source`](#option-source), 494 or [`-target`](#option-target) for details. 495 496 <a id="option-Xbootclasspath-a">`-Xbootclasspath/a:`*path*</a> 497 : Adds a suffix to the bootstrap class path. 498 499 **Note:** This can only be used when compiling for releases prior to JDK 9. 500 As applicable, see the descriptions in [`--release`](#option-release), [`-source`](#option-source), 501 or [`-target`](#option-target) for details. 502 503 <a id="option-Xbootclasspath-p">`-Xbootclasspath/p:`*path*</a> 504 : Adds a prefix to the bootstrap class path. 505 506 **Note:** This can only be used when compiling for releases prior to JDK 9. 507 As applicable, see the descriptions in [`--release`](#option-release), [`-source`](#option-source), 508 or [`-target`](#option-target) for details. 509 510 <a id="option-Xdiags">`-Xdiags:`\[`compact`, `verbose`\]</a> 511 : Selects a diagnostic mode. 512 513 <a id="option-Xdoclint">`-Xdoclint`</a> 514 : Enables recommended checks for problems in documentation comments. 515 516 <a id="option-Xdoclint-custom">`-Xdoclint:`(`all`|`none`|\[`-`\]*group*)\[`/`*access*\]</a> 517 : Enables or disables specific groups of checks in documentation comments. 518 519 *group* can have one of the following values: 520 `accessibility`, `html`, `missing`, `reference`, `syntax`. 521 522 The variable *access* specifies the minimum visibility level of classes and 523 members that the `-Xdoclint` option checks. It can have one of the 524 following values (in order of most to least visible): 525 `public`, `protected`, `package`, `private`. 526 527 The default *access* level is `private`. 528 529 When prefixed by `doclint:`, the *group* names and `all` can be used with `@SuppressWarnings` 530 to suppress warnings about documentation comments in parts of the code being compiled. 531 532 For more information about these groups of checks, see the [DocLint](javadoc.html#doclint) 533 section of the `javadoc` command documentation. 534 The `-Xdoclint` option is disabled by default in the `javac` command. 535 536 For example, the following option checks classes and members (with all 537 groups of checks) that have the access level of protected and higher (which 538 includes protected and public): 539 540 > `-Xdoclint:all/protected` 541 542 The following option enables all groups of checks for all access levels, 543 except it will not check for HTML errors for classes and members that have the 544 access level of package and higher (which includes package, protected and 545 public): 546 547 > `-Xdoclint:all,-html/package` 548 549 <a id="option-Xdoclint-package">`-Xdoclint/package:`\[`-`\]*packages*(`,`\[`-`\]*package*)\*</a> 550 : Enables or disables checks in specific packages. Each *package* is either 551 the qualified name of a package or a package name prefix followed by `.*`, 552 which expands to all sub-packages of the given package. Each *package* can 553 be prefixed with a hyphen (`-`) to disable checks for a specified package 554 or packages. 555 556 For more information, see the [DocLint](javadoc.html#doclint) 557 section of the `javadoc` command documentation. 558 559 <a id="option-Xlint">`-Xlint`</a> 560 : Enables all recommended warnings. In this release, enabling all available 561 warnings is recommended. 562 563 <a id="option-Xlint-custom">`-Xlint:`\[`-`\]*key*(`,`\[`-`\]*key*)\*</a> 564 : Enables and/or disables warning categories using the one or more of the keys described 565 below separated by commas. The keys `all` and `none` enable or disable all categories 566 (respectively); other keys enable the corresponding category, or disable it if preceded 567 by a hyphen (`-`). 568 569 Supported values for *key* are: 570 571 - `all`: Enables all warning categories. 572 573 - `auxiliaryclass`: Warns about an auxiliary class that is hidden in a 574 source file, and is used from other files. 575 576 - `cast`: Warns about the use of unnecessary casts. 577 578 - `classfile`: Warns about the issues related to classfile contents. 579 580 - `dangling-doc-comments`: Warns about extra or misplaced documentation 581 comments near the beginning of a declaration. 582 583 - `deprecation`: Warns about the use of deprecated items. 584 585 - `dep-ann`: Warns about the items marked as deprecated in `javadoc` but 586 without the `@Deprecated` annotation. 587 588 - `divzero`: Warns about the division by the constant integer 0. 589 590 - `empty`: Warns about an empty statement after `if`. 591 592 - `exports`: Warns about the issues regarding module exports. 593 594 - `fallthrough`: Warns about the falling through from one case of a 595 switch statement to the next. 596 597 - `finally`: Warns about `finally` clauses that do not terminate normally. 598 599 - `identity`: Warns about use of a value-based class where an identity 600 class is expected 601 602 - `incubating`: Warns about the use of incubating modules. 603 604 - `lossy-conversions`: Warns about possible lossy conversions 605 in compound assignment. 606 607 - `missing-explicit-ctor`: Warns about missing explicit constructors in 608 public and protected classes in exported packages. 609 610 - `module`: Warns about the module system-related issues. 611 612 - `opens`: Warns about the issues related to module opens. 613 614 - `options`: Warns about the issues relating to use of command line 615 options. 616 617 - `output-file-clash`: Warns if any output file is overwritten during compilation. 618 This can occur, for example, on case-insensitive filesystems. 619 620 - `overloads`: Warns about the issues related to method overloads. 621 622 - `overrides`: Warns about the issues related to method overrides. 623 624 - `path`: Warns about the invalid path elements on the command line. 625 626 - `preview`: Warns about the use of preview language features. 627 628 - `processing`: Warns about the issues related to annotation processing. 629 630 - `rawtypes`: Warns about the use of raw types. 631 632 - `removal`: Warns about the use of an API that has been marked for 633 removal. 634 635 - `restricted`: Warns about the use of restricted methods. 636 637 - `requires-automatic`: Warns developers about the use of automatic 638 modules in requires clauses. 639 640 - `requires-transitive-automatic`: Warns about automatic modules in 641 requires transitive. 642 643 - `serial`: Warns about the serializable classes that do not provide a 644 serial version ID. Also warns about access to non-public members from a 645 serializable element. 646 647 - `static`: Warns about the accessing a static member using an instance. 648 649 - `strictfp`: Warns about unnecessary use of the `strictfp` modifier. 650 651 - `synchronization`: Warns about synchronization attempts on instances 652 of value-based classes. This key is a deprecated alias for `identity`, 653 which has the same uses and effects. Users are encouraged to use the 654 `identity` category for all future and existing uses of `synchronization`. 655 656 - `text-blocks`: Warns about inconsistent white space characters in text 657 block indentation. 658 659 - `this-escape`: Warns about constructors leaking `this` prior to subclass initialization. 660 661 - `try`: Warns about the issues relating to the use of try blocks (that 662 is, try-with-resources). 663 664 - `unchecked`: Warns about the unchecked operations. 665 666 - `varargs`: Warns about the potentially unsafe `vararg` methods. 667 668 - `none`: Disables all warning categories. 669 670 With the exception of `all` and `none`, the keys can be used with 671 the `@SuppressWarnings` annotation to suppress warnings in a part 672 of the source code being compiled. 673 674 See [Examples of Using -Xlint keys]. 675 676 <a id="option-Xmaxerrs">`-Xmaxerrs` *number*</a> 677 : Sets the maximum number of errors to print. 678 679 <a id="option-Xmaxwarns">`-Xmaxwarns` *number*</a> 680 : Sets the maximum number of warnings to print. 681 682 <a id="option-Xpkginfo">`-Xpkginfo:`\[`always`, `legacy`, `nonempty`\]</a> 683 : Specifies when and how the `javac` command generates `package-info.class` 684 files from `package-info.java` files using one of the following options: 685 686 `always` 687 : Generates a `package-info.class` file for every `package-info.java` 688 file. This option may be useful if you use a build system such as Ant, 689 which checks that each `.java` file has a corresponding `.class` file. 690 691 `legacy` 692 : Generates a `package-info.class` file only if `package-info.java` 693 contains annotations. This option does not generate a 694 `package-info.class` file if `package-info.java` contains only 695 comments. 696 697 **Note:** A `package-info.class` file might be generated but be empty if all the 698 annotations in the `package-info.java` file have 699 `RetentionPolicy.SOURCE`. 700 701 `nonempty` 702 : Generates a `package-info.class` file only if `package-info.java` 703 contains annotations with `RetentionPolicy.CLASS` or 704 `RetentionPolicy.RUNTIME`. 705 706 <a id="option-Xplugin">`-Xplugin:`*name* *args*</a> 707 : Specifies the name and optional arguments for a plug-in to be run. 708 If *args* are provided, *name* and *args* should be quoted or otherwise 709 escape the whitespace characters between the name and all the arguments. 710 For details on the API for a plugin, see the API documentation for 711 [jdk.compiler/com.sun.source.util.Plugin](../../api/jdk.compiler/com/sun/source/util/Plugin.html). 712 713 <a id="option-Xprefer">`-Xprefer:`\[`source`, `newer`\]</a> 714 : Specifies which file to read when both a source file and class file are 715 found for an implicitly compiled class using one of the following options. 716 See [Searching for Module, Package and Type Declarations]. 717 718 - `-Xprefer:newer`: Reads the newer of the source or class files for a 719 type (default). 720 721 - `-Xprefer:source` : Reads the source file. Use `-Xprefer:source` when 722 you want to be sure that any annotation processors can access 723 annotations declared with a retention policy of `SOURCE`. 724 725 <a id="option-Xprint">`-Xprint`</a> 726 : Prints a textual representation of specified types for debugging purposes. 727 This does not perform annotation processing or compilation. The format of 728 the output could change. 729 730 <a id="option-XprintProcessorInfo">`-XprintProcessorInfo`</a> 731 : Prints information about which annotations a processor is asked to process. 732 733 <a id="option-XprintRounds">`-XprintRounds`</a> 734 : Prints information about initial and subsequent annotation processing 735 rounds. 736 737 <a id="option-Xstdout">`-Xstdout` *filename*</a> 738 : Sends compiler messages to the named file. By default, compiler messages go 739 to `System.err`. 740 741 ## Environment Variables 742 743 ### CLASSPATH 744 745 If the [`--class-path`](#option-class-path) option or any of its alternate forms are not specified, 746 the class path will default to the value of the `CLASSPATH` environment 747 variable if it is set. 748 However, it is recommended that this environment variable should _not_ be set, 749 and that the `--class-path` option should be used to provide an explicit 750 value for the class path when one is required. 751 752 ### JDK\_JAVAC\_OPTIONS 753 754 The content of the `JDK_JAVAC_OPTIONS` environment variable, separated by 755 white-spaces ( ) or white-space characters (`\n`, `\t`, `\r`, or `\f`) is 756 prepended to the command line arguments passed to `javac` as a list of 757 arguments. 758 759 The encoding requirement for the environment variable is the same as the 760 `javac` command line on the system. `JDK_JAVAC_OPTIONS` environment variable 761 content is treated in the same manner as that specified in the command line. 762 763 Single quotes (`'`) or double quotes (`"`) can be used to enclose arguments 764 that contain whitespace characters. All content between the open quote and the 765 first matching close quote are preserved by simply removing the pair of quotes. 766 In case a matching quote is not found, the launcher will abort with an error 767 message. `@`*files* are supported as they are specified in the command line. 768 However, as in `@`*files*, use of a wildcard is not supported. 769 770 **Examples of quoting arguments containing white spaces:** 771 772 > `export JDK_JAVAC_OPTIONS='@"C:\white spaces\argfile"'` 773 774 > `export JDK_JAVAC_OPTIONS='"@C:\white spaces\argfile"'` 775 776 > `export JDK_JAVAC_OPTIONS='@C:\"white spaces"\argfile'` 777 778 ## Command-Line Argument Files 779 780 An argument file can include command-line options and source file names in any 781 combination. The arguments within a file can be separated by spaces or new line 782 characters. If a file name contains embedded spaces, then put the whole file 783 name in double quotation marks. 784 785 File names within an argument file are relative to the current directory, not 786 to the location of the argument file. Wildcards (`*`) are not allowed in these 787 lists (such as for specifying `*.java`). Use of the at sign (`@`) to 788 recursively interpret files is not supported. The `-J` options are not supported 789 because they're passed to the launcher, which does not support argument files. 790 791 When executing the `javac` command, pass in the path and name of each argument 792 file with the at sign (`@`) leading character. When the `javac` command 793 encounters an argument beginning with the at sign (`@`), it expands the 794 contents of that file into the argument list. 795 796 797 ### Examples of Using javac @filename 798 799 Single Argument File 800 : You could use a single argument file named `argfile` to hold all `javac` 801 arguments: 802 803 > `javac @argfile` 804 805 This argument file could contain the contents of both files shown in the 806 following **Two Argument Files** example. 807 808 Two Argument Files 809 : You can create two argument files: one for the `javac` options and the 810 other for the source file names. Note that the following lists have no 811 line-continuation characters. 812 813 Create a file named `options` that contains the following: 814 815 **Linux and macOS:** 816 817 ``` 818 -d classes 819 -g 820 -sourcepath /java/pubs/ws/1.3/src/share/classes 821 ``` 822 823 **Windows:** 824 825 ``` 826 -d classes 827 -g 828 -sourcepath C:\java\pubs\ws\1.3\src\share\classes 829 ``` 830 831 Create a file named `sources` that contains the following: 832 833 ``` 834 MyClass1.java 835 MyClass2.java 836 MyClass3.java 837 ``` 838 839 Then, run the `javac` command as follows: 840 841 > `javac @options @sources` 842 843 Argument Files with Paths 844 : The argument files can have paths, but any file names inside the files are 845 relative to the current working directory (not `path1` or `path2`): 846 847 > `javac @path1/options @path2/sources` 848 849 ## Arrangement of Source Code 850 851 In the Java language, classes and interfaces can be organized into 852 packages, and packages can be organized into modules. `javac` expects 853 that the physical arrangement of source files in directories of the 854 file system will mirror the organization of classes into packages, and 855 packages into modules. 856 857 It is a widely adopted convention that module names and package names 858 begin with a lower-case letter, and that class names begin with an 859 upper-case letter. 860 861 ### Arrangement of Source Code for a Package 862 863 When classes and interfaces are organized into a package, the package 864 is represented as a directory, and any subpackages are represented as 865 subdirectories. 866 867 For example: 868 869 - The package `p` is represented as a directory called `p`. 870 871 - The package `p.q` -- that is, the subpackage `q` of package `p` -- 872 is represented as the subdirectory `q` of directory `p`. The 873 directory tree representing package `p.q` is therefore `p\q` 874 on Windows, and `p/q` on other systems. 875 876 - The package `p.q.r` is represented as the directory tree `p\q\r` 877 (on Windows) or `p/q/r` (on other systems). 878 879 Within a directory or subdirectory, `.java` files represent classes 880 and interfaces in the corresponding package or subpackage. 881 882 For example: 883 884 - The class `X` declared in package `p` is represented by the file 885 `X.java` in the `p` directory. 886 887 - The class `Y` declared in package `p.q` is represented by the file 888 `Y.java` in the `q` subdirectory of directory `p`. 889 890 - The class `Z` declared in package `p.q.r` is represented by the file 891 `Z.java` in the `r` subdirectory of `p\q` (on Windows) or `p/q` 892 (on other systems). 893 894 In some situations, it is convenient to split the code into 895 separate directories, each structured as described above, and 896 the aggregate list of directories specified to `javac`. 897 898 ### Arrangement of Source Code for a Module 899 900 In the Java language, a module is a set of packages designed for 901 reuse. In addition to `.java` files for classes and interfaces, each 902 module has a source file called `module-info.java` which: 903 904 1. declares the module's name; 905 906 2. lists the packages exported by the module (to allow reuse by other 907 modules); 908 909 3. lists other modules required by the module (to reuse their exported 910 packages). 911 912 When packages are organized into a module, the module is represented 913 by one or more directories representing the packages in the module, 914 one of which contains the `module-info.java` file. It may be convenient, 915 but it is not required, to use a single directory, named after the module, 916 to contain the `module-info.java` file alongside the directory tree which 917 represents the packages in the module (i.e., the _package hierarchy_ 918 described above). The exact arrangement of source code for a module 919 is typically dictated by the conventions adopted by a development 920 environment (IDE) or build system. 921 922 For example: 923 924 - The module `a.b.c` may be represented by the directory `a.b.c`, on all 925 systems. 926 927 - The module's declaration is represented by the file 928 `module-info.java` in the `a.b.c` directory. 929 930 - If the module contains package `p.q.r`, then the `a.b.c` directory 931 contains the directory tree `p\q\r` (on Windows) or `p/q/r` 932 (on other systems). 933 934 The development environment may prescribe some directory hierarchy 935 between the directory named for the module and the source files to 936 be read by `javac`. 937 938 For example: 939 940 - The module `a.b.c` may be represented by the directory `a.b.c` 941 942 - The module's declaration and the module's packages may be in 943 some subdirectory of `a.b.c`, such as `src\main\java` (on Windows) 944 or `src/main/java` (on other systems). 945 946 947 ## Configuring a Compilation 948 949 This section describes how to configure `javac` to perform a basic compilation. 950 951 See [Configuring the Module System] for additional details for use when compiling 952 for a release of the platform that supports modules. 953 954 ### Source Files 955 956 * Specify the source files to be compiled on the command line. 957 958 If there are no compilation errors, the corresponding class files will 959 be placed in the [output directory]. 960 961 Some systems may limit the amount you can put on a command line; 962 to work around those limits, you can use [argument files](#command-line-argument-files). 963 964 When compiling code for modules, you can also specify source files indirectly, 965 by using the [`--module`](#option-module) or `-m` option. 966 967 ### Output Directory 968 969 * Use the [`-d`](#option-d) option to specify an output directory in which to put the compiled class files. 970 971 This will normally be organized in a [package hierarchy](#package-hierarchy), 972 unless you are compiling source code from multiple modules, in which case it will be 973 organized as a [module hierarchy](#module-hierarchy). 974 975 When the compilation has been completed, if you are compiling one or more modules, 976 you can place the output directory on the module path for the Java [launcher](java.html); 977 otherwise, you can place the place the output directory on the class path 978 for the Java launcher. 979 980 ### Precompiled Code 981 982 The code to be compiled may refer to libraries beyond what is provided by the platform. 983 If so, you must place these libraries on the class path or module path. 984 If the library code is not in a module, place it on the class path; 985 if it is in a module, place it on the module path. 986 987 * Use the [`--class-path`](#option-class-path) option to specify libraries to be placed on the class path. 988 Locations on the class path should be organized in a [package hierarchy](#package-hierarchy). 989 You can also use alternate forms of the option: `-classpath` or `-cp`. 990 991 * Use the [`--module-path`](#option-module-path) option to specify libraries to be placed on the module path. 992 Locations on the module path should either be modules or directories of modules. 993 You can also use an alternate form of the option: `-p`. 994 995 See [Configuring the Module System] for details on how to modify the default 996 configuration of library modules. 997 998 **Note**: the options for the class path and module path are not mutually 999 exclusive, although it is not common to specify the class path when compiling 1000 code for one or more modules. 1001 1002 ### Additional Source Files 1003 1004 The code to be compiled may refer to types in additional source files that are not 1005 specified on the command line. 1006 If so, you must put those source files on either the source path or module path. 1007 You can only specify one of these options: if you are not compiling code for a module, 1008 or if you are only compiling code for a single module, use the source path; 1009 if you are compiling code for multiple modules, use the module source path. 1010 1011 * Use the [`--source-path`](#option-source-path) option to specify the locations of additional source 1012 files that may be read by javac. 1013 Locations on the source path should be organized in a [package hierarchy](#package-hierarchy). 1014 You can also use an alternate form of the option: `-sourcepath`. 1015 1016 * Use the [`--module-source-path`](#option-module-source-path) option one or more times to specify the location 1017 of additional source files in different modules that may be read by javac, 1018 or when compiling source files in multiple modules. 1019 You can either specify the locations for each module [individually](#module-specific-form), 1020 or you can organize the source files so that you can specify the locations all 1021 [together](#module-pattern-form). For more details, see [The Module Source Path Option]. 1022 1023 If you want to be able to refer to types in additional source files but do not 1024 want them to be compiled, use the [`-implicit`](#option-implicit) option. 1025 1026 **Note**: if you are compiling code for multiple modules, you must always specify 1027 a module source path, and all source files specified on the command line must be 1028 in one of the directories on the module source path, or in a subdirectory thereof. 1029 1030 1031 ### Example of Compiling Multiple Source Files 1032 1033 This example compiles the `Aloha.java`, `GutenTag.java`, `Hello.java`, and 1034 `Hi.java` source files in the `greetings` package. 1035 1036 **Linux and macOS:** 1037 1038 ``` 1039 % javac greetings/*.java 1040 % ls greetings 1041 Aloha.class GutenTag.class Hello.class Hi.class 1042 Aloha.java GutenTag.java Hello.java Hi.java 1043 ``` 1044 1045 **Windows:** 1046 1047 ``` 1048 C:\>javac greetings\*.java 1049 C:\>dir greetings 1050 Aloha.class GutenTag.class Hello.class Hi.class 1051 Aloha.java GutenTag.java Hello.java Hi.java 1052 ``` 1053 1054 ### Example of Specifying a User Class Path 1055 1056 After changing one of the source files in the previous example, recompile it: 1057 1058 **Linux and macOS:** 1059 1060 ``` 1061 pwd 1062 /examples 1063 javac greetings/Hi.java 1064 ``` 1065 1066 **Windows:** 1067 1068 ``` 1069 C:\>cd 1070 \examples 1071 C:\>javac greetings\Hi.java 1072 ``` 1073 1074 Because `greetings.Hi` refers to other classes in the `greetings` package, the 1075 compiler needs to find these other classes. The previous example works because 1076 the default user class path is the directory that contains the package 1077 directory. If you want to recompile this file without concern for which 1078 directory you are in, then add the examples directory to the user class path by 1079 setting `CLASSPATH`. This example uses the `-classpath` option. 1080 1081 **Linux and macOS:** 1082 1083 > `javac -classpath /examples /examples/greetings/Hi.java` 1084 1085 **Windows:** 1086 1087 > `C:\>javac -classpath \examples \examples\greetings\Hi.java` 1088 1089 If you change `greetings.Hi` to use a banner utility, then that utility also 1090 needs to be accessible through the user class path. 1091 1092 **Linux and macOS:** 1093 1094 ``` 1095 javac -classpath /examples:/lib/Banners.jar \ 1096 /examples/greetings/Hi.java 1097 ``` 1098 1099 **Windows:** 1100 1101 ``` 1102 C:\>javac -classpath \examples;\lib\Banners.jar ^ 1103 \examples\greetings\Hi.java 1104 ``` 1105 1106 To execute a class in the `greetings` package, the program needs access to the 1107 `greetings` package, and to the classes that the `greetings` classes use. 1108 1109 **Linux and macOS:** 1110 1111 > `java -classpath /examples:/lib/Banners.jar greetings.Hi` 1112 1113 **Windows:** 1114 1115 > `C:\>java -classpath \examples;\lib\Banners.jar greetings.Hi` 1116 1117 ## Configuring the Module System 1118 1119 If you want to include additional modules in your compilation, use the 1120 [`--add-modules`](#option-add-modules) option. 1121 This may be necessary when you are compiling code that is not in a module, 1122 or which is in an automatic module, and the code refers to API in the additional 1123 modules. 1124 1125 If you want to restrict the set of modules in your compilation, use the 1126 [`--limit-modules`](#option-limit-modules) option. 1127 This may be useful if you want to ensure that the code you are compiling 1128 is capable of running on a system with a limited set of modules installed. 1129 1130 If you want to break encapsulation and specify that additional packages 1131 should be considered as exported from a module, use the [`--add-exports`](#option-add-exports) option. 1132 This may be useful when performing white-box testing; relying on access 1133 to internal API in production code is strongly discouraged. 1134 1135 If you want to specify that additional packages 1136 should be considered as required by a module, use the [`--add-reads`](#option-add-reads) option. 1137 This may be useful when performing white-box testing; relying on access 1138 to internal API in production code is strongly discouraged. 1139 1140 You can patch additional content into any module using the 1141 [`--patch-module`](#option-patch-module) option. See [Patching a Module] for more details. 1142 1143 ## Searching for Module, Package and Type Declarations 1144 1145 To compile a source file, the compiler often needs information about a module 1146 or type, but the declaration is not in the source files specified on the command 1147 line. 1148 1149 `javac` needs type information for every class or interface used, 1150 extended, or implemented in the source file. This includes classes and 1151 interfaces not explicitly mentioned in the source file, but that provide 1152 information through inheritance. 1153 1154 For example, when you create a subclass of `java.awt.Window`, you are also 1155 using the ancestor classes of `Window`: `java.awt.Container`, 1156 `java.awt.Component`, and `java.lang.Object`. 1157 1158 When compiling code for a module, the compiler also needs to have available 1159 the declaration of that module. 1160 1161 A successful search may produce a class file, a source file, or both. If 1162 both are found, then you can use the [`-Xprefer`](#option-Xprefer) option to instruct the compiler 1163 which to use. 1164 1165 If a search finds and uses a source file, then by default `javac` 1166 compiles that source file. This behavior can be altered with 1167 [`-implicit`](#option-implicit). 1168 1169 The compiler might not discover the need for some type information until after 1170 annotation processing completes. When the type information is found in a source 1171 file and no [`-implicit`](#option-implicit) option is specified, the compiler gives a warning that 1172 the file is being compiled without being subject to annotation processing. To 1173 disable the warning, either specify the file on the command line (so that it 1174 will be subject to annotation processing) or use the [`-implicit`](#option-implicit) option to 1175 specify whether or not class files should be generated for such source files. 1176 1177 The way that `javac` locates the declarations of those types 1178 depends on whether the reference exists within code for a module or not. 1179 1180 ### Searching Package Oriented Paths 1181 1182 When searching for a source or class file on a path composed of package oriented 1183 locations, `javac` will check each location on the path in turn for the 1184 possible presence of the file. The first occurrence of a particular file 1185 shadows (hides) any subsequent occurrences of like-named files. This shadowing 1186 does not affect any search for any files with a different name. This can be 1187 convenient when searching for source files, which may be grouped in different 1188 locations, such as shared code, platform-specific code and generated code. 1189 It can also be useful when injecting alternate versions of a class file into 1190 a package, to debugging or other instrumentation reasons. But, it can also 1191 be dangerous, such as when putting incompatible different versions of a library 1192 on the class path. 1193 1194 ### Searching Module Oriented Paths 1195 1196 Prior to scanning any module paths for any package or type declarations, 1197 `javac` will lazily scan the following paths and locations to determine 1198 the modules that will be used in the compilation. 1199 1200 * The module source path (see the [`--module-source-path`](#option-module-source-path) option) 1201 * The path for upgradeable modules (see the [`--upgrade-module-path`](#option-upgrade-module-path) option) 1202 * The system modules (see the [`--system`](#option-system) option) 1203 * The user module path ( see the [`--module-path`](#option-module-path) option) 1204 1205 For any module, the first occurrence of the module during the scan completely 1206 shadows (hides) any subsequent appearance of a like-named module. While locating 1207 the modules, `javac` is able to determine the packages exported by the module 1208 and to associate with each module a package oriented path for the contents of 1209 the module. For any previously compiled module, this path will typically be a 1210 single entry for either a directory or a file that provides an internal 1211 directory-like hierarchy, such as a JAR file. Thus, when searching for a type 1212 that is in a package that is known to be exported by a module, `javac` can 1213 locate the declaration directly and efficiently. 1214 1215 ### Searching for the Declaration of a Module 1216 1217 If the module has been previously compiled, the module declaration is 1218 located in a file named `module-info.class` in the root of the package hierarchy 1219 for the content of the module. 1220 1221 If the module is one of those currently being compiled, the module declaration 1222 will be either the file named `module-info.class` in the root of the 1223 package hierarchy for the module in the class output directory, or the 1224 file named `module-info.java` in one of the locations on the source path 1225 or one the module source path for the module. 1226 1227 ### Searching for the Declaration of a Type When the Reference is not in a Module 1228 1229 When searching for a type that is referenced in code that is not in a module, 1230 `javac` will look in the following places: 1231 1232 * The platform classes (or the types in exported packages of the platform modules) 1233 (This is for compiled class files only.) 1234 1235 * Types in exported packages of any modules on the module path, if applicable. 1236 (This is for compiled class files only.) 1237 1238 * Types in packages on the class path and/or source path: 1239 1240 * If both are specified, `javac` looks for compiled class files on the class path 1241 and for source files on the source path. 1242 1243 * If the class path is specified, but not source path, `javac` looks for both 1244 compiled class files and source files on the class path. 1245 1246 * If the class path is not specified, it defaults to the current directory. 1247 1248 When looking for a type on the class path and/or source path, if both a compiled 1249 class file and a source file are found, the most recently modified file will 1250 be used by default. 1251 If the source file is newer, it will be compiled and will may override any 1252 previously compiled version of the file. You can use the [`-Xprefer`](#option-Xprefer) option 1253 to override the default behavior. 1254 1255 ### Searching for the Declaration of a Type When the Reference is in a Module 1256 1257 When searching for a type that is referenced in code in a module, 1258 `javac` will examine the declaration of the enclosing module to determine 1259 if the type is in a package that is exported from another module that is 1260 readable by the enclosing module. 1261 If so, `javac` will simply and directly go to the definition of that module 1262 to find the definition of the required type. 1263 Unless the module is another of the modules being compiled, `javac` will 1264 only look for compiled class files files. In other words, `javac` will 1265 not look for source files in platform modules or modules on the module path. 1266 1267 If the type being referenced is not in some other readable module, 1268 `javac` will examine the module being compiled to try and find the 1269 declaration of the type. 1270 `javac` will look for the declaration of the type as follows: 1271 1272 * Source files specified on the command line or on the source path or 1273 module source path. 1274 1275 * Previously compiled files in the output directory. 1276 1277 1278 ## Directory Hierarchies 1279 1280 `javac` generally assumes that source files and compiled class files will be 1281 organized in a file system directory hierarchy or in a type of file that 1282 supports in an internal directory hierarchy, such as a JAR file. 1283 Three different kinds of hierarchy are supported: a _package hierarchy_, 1284 a _module hierarchy_, and a _module source hierarchy_. 1285 1286 While `javac` is fairly relaxed about the organization of source code, 1287 beyond the expectation that source will be organized in one or package 1288 hierarchies, and can generally accommodate organizations prescribed by 1289 development environments and build tools, Java tools in general, 1290 and `javac` and the Java launcher in particular, are more stringent 1291 regarding the organization of compiled class files, and will be 1292 organized in package hierarchies or module hierarchies, as appropriate. 1293 1294 The location of these hierarchies are specified to `javac` with command-line 1295 options, whose names typically end in "path", like [`--source-path`](#option-source-path) or 1296 [`--class-path`](#option-class-path). Also as a general rule, path options whose name includes the 1297 word `module`, like [`--module-path`](#option-module-path), are used to specify module hierarchies, 1298 although some module-related path options allow a package hierarchy to be 1299 specified on a per-module basis. All other path options are used to specify 1300 package hierarchies. 1301 1302 ### Package Hierarchy 1303 1304 In a package hierarchy, directories and subdirectories are used 1305 to represent the component parts of the package name, with the source 1306 file or compiled class file for a type being stored as a file with an 1307 extension of `.java` or `.class` in the most nested directory. 1308 1309 For example, in a package hierarchy, the source file for a class 1310 `com.example.MyClass` will be stored in the file _com/example/MyClass.java_ 1311 1312 ### Module Hierarchy 1313 1314 In a module hierarchy, the first level of directories are named 1315 for the modules in the hierarchy; within each of those directories 1316 the contents of the module are organized in package hierarchies. 1317 1318 For example, in a module hierarchy, the compiled class file for a type called 1319 `com.example.MyClass` in a module called `my.library` will be stored in 1320 _my.library/com/example/MyClass.class_. 1321 1322 The various output directories used by `javac` (the class output directory, 1323 the source output directory, and native header output directory) 1324 will all be organized in a module hierarchy when multiple modules are being compiled. 1325 1326 ### Module Source Hierarchy 1327 1328 Although the source for each individual module should always be 1329 organized in a package hierarchy, it may be convenient to group 1330 those hierarchies into a module source hierarchy. This is similar 1331 to a module hierarchy, except that there may be intervening directories 1332 between the directory for the module and the directory that is 1333 the root of the package hierarchy for the source code of the module. 1334 1335 For example, in a module source hierarchy, the source file for a type called 1336 `com.example.MyClass` in a module called `my.library` may be stored in a 1337 file such as 1338 _my.library/src/main/java/com/example/MyClass.java_. 1339 1340 ## The Module Source Path Option 1341 1342 The [`--module-source-path`](#option-module-source-path) option has two forms: a _module-specific form_, 1343 in which a package path is given for each module containing code to be compiled, 1344 and a _module-pattern_ form, in which the source path for each module is specified 1345 by a pattern. 1346 The module-specific form is generally simpler to use when only a small number of 1347 modules are involved; the module-pattern form may be more convenient when the 1348 number of modules is large and the modules are organized in a regular manner that 1349 can be described by a pattern. 1350 1351 Multiple instances of the `--module-source-path` option may be given, each one 1352 using either the module-pattern form or the module-specific form, subject to the 1353 following limitations: 1354 1355 * the module-pattern form may be used at most once 1356 * the module-specific form may be used at most once for any given module 1357 1358 If the module-specific form is used for any module, the associated search path 1359 overrides any path that might otherwise have been inferred from the module-pattern form. 1360 1361 ### Module-specific form 1362 1363 The module-specific form allows an explicit search path to be given for any specific module. 1364 This form is: 1365 1366 * `--module-source-path` *module-name*`=`*file-path* (*path-separator* *file-path*)* 1367 1368 The path separator character is `;` on Windows, and `:` otherwise. 1369 1370 **Note:** this is similar to the form used for the [`--patch-module`](#option-patch-module) option. 1371 1372 ### Module-pattern form 1373 1374 The module-pattern form allows a concise specification of the module source path 1375 for any number of modules organized in regular manner. 1376 1377 * `--module-source-path` *pattern* 1378 1379 The pattern is defined by the following rules, which are applied in order: 1380 1381 * The argument is considered to be a series of segments separated by the path 1382 separator character (`;` on Windows, and `:` otherwise). 1383 1384 * Each segment containing curly braces of the form 1385 1386 string1{alt1 ( ,alt2 )* } string2 1387 1388 is considered to be replaced by a series of segments formed by "expanding" the braces: 1389 1390 string1 alt1 string2 1391 string1 alt2 string2 1392 and so on... 1393 1394 The braces may be nested. 1395 1396 This rule is applied for all such usages of braces. 1397 1398 * Each segment must have at most one asterisk (`*`). 1399 If a segment does not contain an asterisk, it is considered to be as though the 1400 file separator character and an asterisk are appended. 1401 1402 For any module _M_, the source path for that module is formed from the series 1403 of segments obtained by substituting the module name _M_ for the asterisk in 1404 each segment. 1405 1406 **Note**: in this context, the asterisk is just used as a special marker, to 1407 denote the position in the path of the module name. It should not be confused 1408 with the use of `*` as a file name wildcard character, as found on most 1409 operating systems. 1410 1411 ## Patching Modules 1412 1413 javac allows any content, whether in source or compiled form, to be patched 1414 into any module using the [`--patch-module`](#option-patch-module) option. 1415 You may want to do this to compile alternative implementations of a class 1416 to be patched at runtime into a JVM, or to inject additional classes into 1417 the module, such as when testing. 1418 1419 The form of the option is: 1420 1421 * `--patch-module` *module-name*`=`*file-path* (*path-separator* *file-path* )* 1422 1423 The path separator character is `;` on Windows, and `:` otherwise. 1424 The paths given for the module must specify the root of a 1425 package hierarchy for the contents of the module 1426 1427 The option may be given at most once for any given module. 1428 Any content on the path will hide any like-named content later in the path 1429 and in the patched module. 1430 1431 When patching source code into more than one module, the [`--module-source-path`](#option-module-source-path) 1432 must also be used, so that the output directory is organized in a module hierarchy, 1433 and capable of holding the compiled class files for the modules being compiled. 1434 1435 ## Annotation Processing 1436 1437 The `javac` command provides direct support for annotation processing. 1438 1439 The API for annotation processors is defined in the 1440 `javax.annotation.processing` and `javax.lang.model` packages and subpackages. 1441 1442 ### How Annotation Processing Works 1443 1444 Annotation processing is requested by using an option to configure 1445 annotation processing, such as [`-processor`](#option-processor), 1446 [`--processor-path`](#option-processor-path), 1447 [`--processor-module-path`](#option-processor-module-path) or by 1448 explicitly enabling processing with the [`-proc:full`](#option-proc) 1449 or [`-proc:only`](#option-proc) options. Annotation processing is 1450 disabled using the [`-proc:none`](#option-proc) option. 1451 1452 If annotation processing is requested, the compiler searches for any 1453 annotation processors that are available. 1454 1455 The search path can be specified with the 1456 [`-processorpath`](#option-processor-path) option. If no path is 1457 specified, then the user class path is used. Processors are located by 1458 means of service provider-configuration files named 1459 `META-INF/services/javax.annotation.processing.Processor` on the 1460 search path. Such files should contain the names of any 1461 annotationation processors to be used, listed one per 1462 line. Alternatively, processors can be specified explicitly, using the 1463 [`-processor`](#option-processor) option. 1464 1465 After scanning the source files and classes on the command line to determine 1466 what annotations are present, the compiler queries the processors to determine 1467 what annotations they process. When a match is found, the processor is called. 1468 A processor can claim the annotations it processes, in which case no further 1469 attempt is made to find any processors for those annotations. After all of the 1470 annotations are claimed, the compiler does not search for additional 1471 processors. 1472 1473 If any processors generate new source files, then another round of annotation 1474 processing occurs: Any newly generated source files are scanned, and the 1475 annotations processed as before. Any processors called on previous rounds are 1476 also called on all subsequent rounds. This continues until no new source files 1477 are generated. 1478 1479 After a round occurs where no new source files are generated, the annotation 1480 processors are called one last time, to give them a chance to complete any 1481 remaining work. Finally, unless the [`-proc:only`](#option-proc) option is used, the compiler 1482 compiles the original and all generated source files. 1483 1484 If you use an annotation processor that generates additional source 1485 files to be included in the compilation, you can specify a default 1486 module to be used for the newly generated files, for use when a 1487 module declaration is not also generated. In this case, use the 1488 [`--default-module-for-created-files`](#option-default-module-for-created-files) option. 1489 1490 ### Compilation Environment and Runtime Environment. 1491 1492 The declarations in source files and previously compiled class files are analyzed 1493 by `javac` in a _compilation environment_ that is distinct from the 1494 _runtime environment_ used to execute `javac` itself. Although there is a 1495 deliberate similarity between many `javac` options and like-named options for the 1496 Java [launcher](java.html), such as `--class-path`, `--module-path` and so 1497 on, it is important to understand that in general the `javac` options just affect 1498 the environment in which the source files are compiled, and do not affect 1499 the operation of `javac` itself. 1500 1501 The distinction between the compilation environment and runtime environment 1502 is significant when it comes to using annotation processors. 1503 Although annotations processors process elements (declarations) that exist 1504 in the compilation environment, the annotation processor itself is executed 1505 in the runtime environment. If an annotation processor has dependencies on 1506 libraries that are not in modules, the libraries can be placed, along with the 1507 annotation processor itself, on the processor path. 1508 (See the [`--processor-path`](#option-processor-path) option.) 1509 If the annotation processor and its dependencies are in modules, you should 1510 use the processor module path instead. 1511 (See the [`--processor-module-path`](#option-processor-module-path) option.) 1512 When those are insufficient, it may be necessary to provide further 1513 configuration of the runtime environment. This can be done in two ways: 1514 1515 1. If `javac` is invoked from the command line, options can be passed to the 1516 underlying runtime by prefixing the option with [`-J`](#option-J). 1517 1518 2. You can start an instance of a Java Virtual Machine directly and use 1519 command line options and API to configure an environment in which 1520 `javac` can be invoked via one of its [APIs]. 1521 1522 ## Compiling for Earlier Releases of the Platform 1523 1524 `javac` can compile code that is to be used on other releases of the platform, 1525 using either the [`--release`](#option-release) option, or the [`--source`](#option-source)/`-source` and 1526 [`--target`](#option-target)/`-target` options, together with additional options to specify the 1527 platform classes. 1528 1529 Depending on the desired platform release, there are some restrictions on some 1530 of the options that can be used. 1531 1532 * When compiling for JDK 8 and earlier releases, you cannot use any option 1533 that is intended for use with the module system. 1534 This includes all of the following options: 1535 1536 * [`--module-source-path`](#option-module-source-path), 1537 [`--upgrade-module-path`](#option-upgrade-module-path), 1538 [`--system`](#option-system), 1539 [`--module-path`](#option-module-path), 1540 [`--add-modules`](#option-add-modules), 1541 [`--add-exports`](#option-add-exports), 1542 `--add-opens`, 1543 [`--add-reads`](#option-add-reads), 1544 [`--limit-modules`](#option-limit-modules), 1545 [`--patch-module`](#option-patch-module) 1546 1547 If you use the `--source`/`-source` or `--target`/`-target` options, 1548 you should also set the appropriate platform classes using the 1549 boot class path family of options. 1550 1551 * When compiling for JDK 9 and later releases, you cannot use any option 1552 that is intended to configure the boot class path. 1553 This includes all of the following options: 1554 1555 * [`-Xbootclasspath/p:`](#option-Xbootclasspath-p), 1556 [`-Xbootclasspath`](#option-Xbootclasspath), 1557 [`-Xbootclasspath/a:`](#option-Xbootclasspath-a), 1558 [`-endorseddirs`](#option-endorseddirs), 1559 [`-Djava.endorsed.dirs`](#option-Djava.endorsed.dirs), 1560 [`-extdirs`](#option-extdirs), 1561 [`-Djava.ext.dirs`](#option-Djava.ext.dirs), 1562 [`-profile`](#option-profile) 1563 1564 If you use the `--source`/`-source` or `--target`/`-target` options, 1565 you should also set the appropriate platform classes using the `--system` 1566 option to give the location of an appropriate installed release of JDK. 1567 1568 When using the `--release` option, only the supported documented API for that 1569 release may be used; you cannot use any options to break encapsulation to 1570 access any internal classes. 1571 1572 ## APIs 1573 1574 The `javac` compiler can be invoked using an API in three different ways: 1575 1576 The [Java Compiler API](../../api/java.compiler/javax/tools/JavaCompiler.html) 1577 : This provides the most flexible way to invoke the compiler, 1578 including the ability to compile source files provided in 1579 memory buffers or other non-standard file systems. 1580 1581 The [ToolProvider API](../../api/java.base/java/util/spi/ToolProvider.html) 1582 : A `ToolProvider` for `javac` can be obtained by calling 1583 `ToolProvider.findFirst("javac")`. This returns an object 1584 with the equivalent functionality of the command-line tool. 1585 1586 **Note**: This API should not be confused with the like-named 1587 API in the [`javax.tools`](../../api/java.compiler/javax/tools/ToolProvider.html) 1588 package. 1589 1590 The `javac` [Legacy API](../../api/jdk.compiler/com/sun/tools/javac/Main.html) 1591 : This API is retained for backward compatibility only. 1592 All new code should use either the Java Compiler API or the ToolProvider API. 1593 1594 **Note:** All other classes and methods found in a package with names that start with 1595 `com.sun.tools.javac` (subpackages of `com.sun.tools.javac`) are strictly 1596 internal and subject to change at any time. 1597 1598 ## Examples of Using -Xlint keys 1599 1600 `cast` 1601 : Warns about unnecessary and redundant casts, for example: 1602 1603 > `String s = (String) "Hello!"` 1604 1605 `classfile` 1606 : Warns about issues related to class file contents. 1607 1608 `deprecation` 1609 : Warns about the use of deprecated items. For example: 1610 1611 ``` 1612 java.util.Date myDate = new java.util.Date(); 1613 int currentDay = myDate.getDay(); 1614 ``` 1615 1616 The method `java.util.Date.getDay` has been deprecated since JDK 1.1. 1617 1618 `dep-ann` 1619 : Warns about items that are documented with the `@deprecated` Javadoc 1620 comment, but do not have the `@Deprecated` annotation, for example: 1621 1622 ``` 1623 /** 1624 * @deprecated As of Java SE 7, replaced by {@link #newMethod()} 1625 */ 1626 public static void deprecatedMethod() { } 1627 public static void newMethod() { } 1628 ``` 1629 1630 `divzero` 1631 : Warns about division by the constant integer 0, for example: 1632 1633 > `int divideByZero = 42 / 0;` 1634 1635 `empty` 1636 : Warns about empty statements after `if`statements, for example: 1637 1638 ``` 1639 class E { 1640 void m() { 1641 if (true) ; 1642 } 1643 } 1644 ``` 1645 1646 `fallthrough` 1647 : Checks the switch blocks for fall-through cases and provides a warning 1648 message for any that are found. Fall-through cases are cases in a switch 1649 block, other than the last case in the block, whose code does not include a 1650 `break` statement, allowing code execution to fall through from that case to 1651 the next case. For example, the code following the case 1 label in this 1652 switch block does not end with a `break` statement: 1653 1654 ``` 1655 switch (x) { 1656 case 1: 1657 System.out.println("1"); 1658 // No break statement here. 1659 case 2: 1660 System.out.println("2"); 1661 } 1662 ``` 1663 1664 If the `-Xlint:fallthrough` option was used when compiling this code, then 1665 the compiler emits a warning about possible fall-through into case, with 1666 the line number of the case in question. 1667 1668 `finally` 1669 : Warns about `finally` clauses that cannot be completed normally, for 1670 example: 1671 1672 ``` 1673 public static int m() { 1674 try { 1675 throw new NullPointerException(); 1676 } catch (NullPointerException e) { 1677 System.err.println("Caught NullPointerException."); 1678 return 1; 1679 } finally { 1680 return 0; 1681 } 1682 } 1683 ``` 1684 1685 The compiler generates a warning for the `finally` block in this example. 1686 When the `int` method is called, it returns a value of 0. A `finally` block 1687 executes when the `try` block exits. In this example, when control is 1688 transferred to the `catch` block, the `int` method exits. However, the 1689 `finally` block must execute, so it's executed, even though control was 1690 transferred outside the method. 1691 1692 `options` 1693 : Warns about issues that related to the use of command-line options. See 1694 [Compiling for Earlier Releases of the Platform]. 1695 1696 `overrides` 1697 : Warns about issues related to method overrides. For example, consider the 1698 following two classes: 1699 1700 ``` 1701 public class ClassWithVarargsMethod { 1702 void varargsMethod(String... s) { } 1703 } 1704 1705 public class ClassWithOverridingMethod extends ClassWithVarargsMethod { 1706 @Override 1707 void varargsMethod(String[] s) { } 1708 } 1709 ``` 1710 1711 The compiler generates a warning similar to the following:. 1712 1713 ``` 1714 warning: [override] varargsMethod(String[]) in ClassWithOverridingMethod 1715 overrides varargsMethod(String...) in ClassWithVarargsMethod; overriding 1716 method is missing '...' 1717 ``` 1718 1719 When the compiler encounters a `varargs` method, it translates the 1720 `varargs` formal parameter into an array. In the method 1721 `ClassWithVarargsMethod.varargsMethod`, the compiler translates the 1722 `varargs` formal parameter `String... s` to the formal parameter 1723 `String[] s`, an array that matches the formal parameter of the method 1724 `ClassWithOverridingMethod.varargsMethod`. Consequently, this example 1725 compiles. 1726 1727 `path` 1728 : Warns about invalid path elements and nonexistent path directories on the 1729 command line (with regard to the class path, the source path, and other 1730 paths). Such warnings cannot be suppressed with the `@SuppressWarnings` 1731 annotation. For example: 1732 1733 - **Linux and macOS:** 1734 `javac -Xlint:path -classpath /nonexistentpath Example.java` 1735 1736 - **Windows:** 1737 `javac -Xlint:path -classpath C:\nonexistentpath Example.java` 1738 1739 `processing` 1740 : Warns about issues related to annotation processing. The compiler generates 1741 this warning when you have a class that has an annotation, and you use an 1742 annotation processor that cannot handle that type of annotation. For example, 1743 the following is a simple annotation processor: 1744 1745 **Source file AnnoProc.java**: 1746 1747 ``` 1748 import java.util.*; 1749 import javax.annotation.processing.*; 1750 import javax.lang.model.*; 1751 import javax.lang.model.element.*; 1752 1753 @SupportedAnnotationTypes("NotAnno") 1754 public class AnnoProc extends AbstractProcessor { 1755 public boolean process(Set<? extends TypeElement> elems, RoundEnvironment renv){ 1756 return true; 1757 } 1758 1759 public SourceVersion getSupportedSourceVersion() { 1760 return SourceVersion.latest(); 1761 } 1762 } 1763 ``` 1764 1765 **Source file AnnosWithoutProcessors.java**: 1766 1767 ``` 1768 @interface Anno { } 1769 1770 @Anno 1771 class AnnosWithoutProcessors { } 1772 ``` 1773 1774 The following commands compile the annotation processor `AnnoProc`, then 1775 run this annotation processor against the source file 1776 `AnnosWithoutProcessors.java`: 1777 1778 ``` 1779 javac AnnoProc.java 1780 javac -cp . -Xlint:processing -processor AnnoProc -proc:only AnnosWithoutProcessors.java 1781 ``` 1782 1783 When the compiler runs the annotation processor against the source file 1784 `AnnosWithoutProcessors.java`, it generates the following warning: 1785 1786 ``` 1787 warning: [processing] No processor claimed any of these annotations: Anno 1788 ``` 1789 1790 To resolve this issue, you can rename the annotation defined and used in 1791 the class `AnnosWithoutProcessors` from `Anno` to `NotAnno`. 1792 1793 `rawtypes` 1794 : Warns about unchecked operations on raw types. The following statement 1795 generates a `rawtypes` warning: 1796 1797 > `void countElements(List l) { ... }` 1798 1799 The following example does not generate a `rawtypes` warning: 1800 1801 > `void countElements(List<?> l) { ... }` 1802 1803 `List` is a raw type. However, `List<?>` is an unbounded wildcard 1804 parameterized type. Because `List` is a parameterized interface, always 1805 specify its type argument. In this example, the `List` formal argument is 1806 specified with an unbounded wildcard (`?`) as its formal type parameter, 1807 which means that the `countElements` method can accept any instantiation of 1808 the `List` interface. 1809 1810 `serial` 1811 : Warns about missing `serialVersionUID` definitions on serializable classes. 1812 For example: 1813 1814 ``` 1815 public class PersistentTime implements Serializable 1816 { 1817 private Date time; 1818 1819 public PersistentTime() { 1820 time = Calendar.getInstance().getTime(); 1821 } 1822 1823 public Date getTime() { 1824 return time; 1825 } 1826 } 1827 ``` 1828 1829 The compiler generates the following warning: 1830 1831 ``` 1832 warning: [serial] serializable class PersistentTime has no definition of 1833 serialVersionUID 1834 ``` 1835 1836 If a serializable class does not explicitly declare a field named 1837 `serialVersionUID`, then the serialization runtime environment calculates a 1838 default `serialVersionUID` value for that class based on various aspects of 1839 the class, as described in the Java Object Serialization Specification. 1840 However, it's strongly recommended that all serializable classes explicitly 1841 declare `serialVersionUID` values because the default process of computing 1842 `serialVersionUID` values is highly sensitive to class details that can 1843 vary depending on compiler implementations. As a result, this might cause 1844 an unexpected `InvalidClassExceptions` during deserialization. To guarantee 1845 a consistent `serialVersionUID` value across different Java compiler 1846 implementations, a serializable class must declare an explicit 1847 `serialVersionUID` value. 1848 1849 `static` 1850 : Warns about issues relating to the use of static variables, for example: 1851 1852 ``` 1853 class XLintStatic { 1854 static void m1() { } 1855 void m2() { this.m1(); } 1856 } 1857 ``` 1858 1859 The compiler generates the following warning: 1860 1861 ``` 1862 warning: [static] static method should be qualified by type name, 1863 XLintStatic, instead of by an expression 1864 ``` 1865 1866 To resolve this issue, you can call the `static` method `m1` as follows: 1867 1868 > `XLintStatic.m1();` 1869 1870 Alternately, you can remove the `static` keyword from the declaration of 1871 the method `m1`. 1872 1873 `this-escape` 1874 : Warns about constructors leaking 1875 `this` prior to subclass initialization. 1876 For example, this class: 1877 1878 ``` 1879 public class MyClass { 1880 public MyClass() { 1881 System.out.println(this.hashCode()); 1882 } 1883 } 1884 ``` 1885 1886 generates the following warning: 1887 1888 ``` 1889 MyClass.java:3: warning: [this-escape] possible 'this' escape 1890 before subclass is fully initialized 1891 System.out.println(this.hashCode()); 1892 ^ 1893 ``` 1894 1895 A 'this' escape warning is generated when a constructor does something 1896 that might result in a subclass method being invoked before the 1897 constructor returns. 1898 In such cases the subclass method would be operating on an incompletely 1899 initialized instance. 1900 In the above example, a subclass of `MyClass` that overrides 1901 `hashCode()` to incorporate its own fields would likely produce 1902 an incorrect result when invoked as shown. 1903 1904 Warnings are only generated if a subclass could exist that is outside 1905 of the current module (or package, if no module) being compiled. 1906 So, for example, constructors in final and non-public classes do not 1907 generate warnings. 1908 1909 `try` 1910 : Warns about issues relating to the use of `try` blocks, including 1911 try-with-resources statements. For example, a warning is generated for the 1912 following statement because the resource `ac` declared in the `try` block 1913 is not used: 1914 1915 ``` 1916 try ( AutoCloseable ac = getResource() ) { // do nothing} 1917 ``` 1918 1919 `unchecked` 1920 : Gives more detail for unchecked conversion warnings that are mandated by 1921 the Java Language Specification, for example: 1922 1923 ``` 1924 List l = new ArrayList<Number>(); 1925 List<String> ls = l; // unchecked warning 1926 ``` 1927 1928 During type erasure, the types `ArrayList<Number>` and `List<String>` 1929 become `ArrayList` and `List`, respectively. 1930 1931 The `ls` command has the parameterized type `List<String>`. When the `List` 1932 referenced by `l` is assigned to `ls`, the compiler generates an unchecked 1933 warning. At compile time, the compiler and JVM cannot determine whether `l` 1934 refers to a `List<String>` type. In this case, `l` does not refer to a 1935 `List<String>` type. As a result, heap pollution occurs. 1936 1937 A heap pollution situation occurs when the `List` object `l`, whose static 1938 type is `List<Number>`, is assigned to another `List` object, `ls`, that 1939 has a different static type, `List<String>`. However, the compiler still 1940 allows this assignment. It must allow this assignment to preserve backward 1941 compatibility with releases of Java SE that do not support generics. Because 1942 of type erasure, `List<Number>` and `List<String>` both become `List`. 1943 Consequently, the compiler allows the assignment of the object `l`, which 1944 has a raw type of `List`, to the object `ls`. 1945 1946 `varargs` 1947 : Warns about unsafe use of variable arguments (`varargs`) methods, in 1948 particular, those that contain non-reifiable arguments, for example: 1949 1950 ``` 1951 public class ArrayBuilder { 1952 public static <T> void addToList (List<T> listArg, T... elements) { 1953 for (T x : elements) { 1954 listArg.add(x); 1955 } 1956 } 1957 } 1958 ``` 1959 1960 A non-reifiable type is a type whose type information is not fully available 1961 at runtime. 1962 1963 The compiler generates the following warning for the definition of the 1964 method `ArrayBuilder.addToList`: 1965 1966 ``` 1967 warning: [varargs] Possible heap pollution from parameterized vararg type T 1968 ``` 1969 1970 When the compiler encounters a varargs method, it translates the `varargs` 1971 formal parameter into an array. However, the Java programming language 1972 does not permit the creation of arrays of parameterized types. In the method 1973 `ArrayBuilder.addToList`, the compiler translates the `varargs` formal 1974 parameter `T...` elements to the formal parameter `T[]` elements, an array. 1975 However, because of type erasure, the compiler converts the `varargs` 1976 formal parameter to `Object[]` elements. Consequently, there's a 1977 possibility of heap pollution.