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