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 For more information about these groups of checks, see the Do‐
505 cLint section of the javadoc command documentation. The -Xdo‐
506 clint option is disabled by default in the javac command.
507
508 For example, the following option checks classes and members
509 (with all groups of checks) that have the access level of pro‐
510 tected and higher (which includes protected and public):
511
512 -Xdoclint:all/protected
513
514 The following option enables all groups of checks for all access
515 levels, except it will not check for HTML errors for classes and
516 members that have the access level of package and higher (which
517 includes package, protected and public):
518
519 -Xdoclint:all,-html/package
520
521 -Xdoclint/package:[-]packages(,[-]package)*
522 Enables or disables checks in specific packages. Each package
523 is either the qualified name of a package or a package name pre‐
524 fix followed by .*, which expands to all sub-packages of the
525 given package. Each package can be prefixed with a hyphen (-)
526 to disable checks for a specified package or packages.
527
528 For more information, see the DocLint section of the javadoc
529 command documentation.
530
531 -Xlint Enables all recommended warnings. In this release, enabling all
532 available warnings is recommended.
533
534 -Xlint:[-]key(,[-]key)*
535 Supplies warnings to enable or disable, separated by comma.
536 Precede a key by a hyphen (-) to disable the specified warning.
537
538 Supported values for key are:
539
540 • all: Enables all warnings.
541
542 • auxiliaryclass: Warns about an auxiliary class that's hidden
543 in a source file, and is used from other files.
544
545 • cast: Warns about the use of unnecessary casts.
546
547 • classfile: Warns about the issues related to classfile con‐
548 tents.
549
550 • deprecation: Warns about the use of deprecated items.
551
552 • dep-ann: Warns about the items marked as deprecated in javadoc
553 but without the @Deprecated annotation.
554
555 • divzero: Warns about the division by the constant integer 0.
556
557 • empty: Warns about an empty statement after if.
558
559 • exports: Warns about the issues regarding module exports.
560
561 • fallthrough: Warns about the falling through from one case of
562 a switch statement to the next.
563
564 • finally: Warns about finally clauses that do not terminate
565 normally.
566
567 • module: Warns about the module system-related issues.
568
569 • opens: Warns about the issues related to module opens.
570
571 • options: Warns about the issues relating to use of command
572 line options.
573
574 • overloads: Warns about the issues related to method overloads.
575
576 • overrides: Warns about the issues related to method overrides.
577
578 • path: Warns about the invalid path elements on the command l
579 ine.
580
581 • processing: Warns about the issues related to annotation pro‐
582 cessing.
583
584 • rawtypes: Warns about the use of raw types.
585
586 • removal: Warns about the use of an API that has been marked
587 for removal.
588
589 • requires-automatic: Warns developers about the use of automat‐
590 ic modules in requires clauses.
591
592 • requires-transitive-automatic: Warns about automatic modules
593 in requires transitive.
594
595 • serial: Warns about the serializable classes that do not pro‐
596 vide a serial version ID. Also warns about access to non-pub‐
597 lic members from a serializable element.
598
599 • static: Warns about the accessing a static member using an in‐
600 stance.
601
602 • try: Warns about the issues relating to the use of try blocks
603 ( that is, try-with-resources).
604
605 • unchecked: Warns about the unchecked operations.
606
607 • varargs: Warns about the potentially unsafe vararg methods.
608
609 • none: Disables all warnings.
610
611 See Examples of Using -Xlint keys.
612
613 -Xmaxerrs number
614 Sets the maximum number of errors to print.
615
616 -Xmaxwarns number
617 Sets the maximum number of warnings to print.
618
619 -Xpkginfo:[always, legacy, nonempty]
620 Specifies when and how the javac command generates package-in‐
621 fo.class files from package-info.java files using one of the
622 following options:
623
624 always Generates a package-info.class file for every package-in‐
625 fo.java file. This option may be useful if you use a
626 build system such as Ant, which checks that each .java
627 file has a corresponding .class file.
628
629 legacy Generates a package-info.class file only if package-in‐
630 fo.java contains annotations. This option does not gen‐
631 erate a package-info.class file if package-info.java con‐
632 tains only comments.
633
634 Note: A package-info.class file might be generated but be
635 empty if all the annotations in the package-info.java
636 file have RetentionPolicy.SOURCE.
637
638 nonempty
639 Generates a package-info.class file only if package-in‐
640 fo.java contains annotations with RetentionPolicy.CLASS
641 or RetentionPolicy.RUNTIME.
642
643 -Xplugin:name args
644 Specifies the name and optional arguments for a plug-in to be
645 run. If args are provided, name and args should be quoted or
646 otherwise escape the whitespace characters between the name and
647 all the arguments. For details on the API for a plugin, see the
648 API documentation for jdk.compiler/com.sun.source.util.Plugin.
649
650 -Xprefer:[source, newer]
651 Specifies which file to read when both a source file and class
652 file are found for an implicitly compiled class using one of the
653 following options. See Searching for Module, Package and Type
654 Declarations.
655
656 • -Xprefer:newer: Reads the newer of the source or class files
657 for a type (default).
658
659 • -Xprefer:source : Reads the source file. Use -Xprefer:source
660 when you want to be sure that any annotation processors can
661 access annotations declared with a retention policy of SOURCE.
662
663 -Xprint
664 Prints a textual representation of specified types for debugging
665 purposes. This does not perform annotation processing or compi‐
666 lation. The format of the output could change.
667
668 -XprintProcessorInfo
669 Prints information about which annotations a processor is asked
670 to process.
671
672 -XprintRounds
673 Prints information about initial and subsequent annotation pro‐
674 cessing rounds.
675
676 -Xstdout filename
677 Sends compiler messages to the named file. By default, compiler
678 messages go to System.err.
679
681 CLASSPATH
682 If the --class-path option or any of its alternate forms are not speci‐
683 fied, the class path will default to the value of the CLASSPATH envi‐
684 ronment variable if it is set. However, it is recommended that this
685 environment variable should not be set, and that the --class-path op‐
686 tion should be used to provide an explicit value for the class path
687 when one is required.
688
689 JDK_JAVAC_OPTIONS
690 The content of the JDK_JAVAC_OPTIONS environment variable, separated by
691 white-spaces ( ) or white-space characters (\n, \t, \r, or \f) is
692 prepended to the command line arguments passed to javac as a list of
693 arguments.
694
695 The encoding requirement for the environment variable is the same as
696 the javac command line on the system. JDK_JAVAC_OPTIONS environment
697 variable content is treated in the same manner as that specified in the
698 command line.
699
700 Single quotes (') or double quotes (") can be used to enclose arguments
701 that contain whitespace characters. All content between the open quote
702 and the first matching close quote are preserved by simply removing the
703 pair of quotes. In case a matching quote is not found, the launcher
704 will abort with an error message. @files are supported as they are
705 specified in the command line. However, as in @files, use of a wild‐
706 card is not supported.
707
708 Examples of quoting arguments containing white spaces:
709
710 export JDK_JAVAC_OPTIONS='@"C:\white spaces\argfile"'
711
712 export JDK_JAVAC_OPTIONS='"@C:\white spaces\argfile"'
713
714 export JDK_JAVAC_OPTIONS='@C:\"white spaces"\argfile'
715
717 An argument file can include command-line options and source file names
718 in any combination. The arguments within a file can be separated by
719 spaces or new line characters. If a file name contains embedded spa‐
720 ces, then put the whole file name in double quotation marks.
721
722 File names within an argument file are relative to the current directo‐
723 ry, not to the location of the argument file. Wildcards (*) are not
724 allowed in these lists (such as for specifying *.java). Use of the at
725 sign (@) to recursively interpret files is not supported. The -J op‐
726 tions are not supported because they're passed to the launcher, which
727 does not support argument files.
728
729 When executing the javac command, pass in the path and name of each ar‐
730 gument file with the at sign (@) leading character. When the javac
731 command encounters an argument beginning with the at sign (@), it ex‐
732 pands the contents of that file into the argument list.
733
734 Examples of Using javac @filename
735 Single Argument File
736 You could use a single argument file named argfile to hold all
737 javac arguments:
738
739 javac @argfile
740
741 This argument file could contain the contents of both files
742 shown in the following Two Argument Files example.
743
744 Two Argument Files
745 You can create two argument files: one for the javac options and
746 the other for the source file names. Note that the following
747 lists have no line-continuation characters.
748
749 Create a file named options that contains the following:
750
751 Linux and macOS:
752
753 -d classes
754 -g
755 -sourcepath /java/pubs/ws/1.3/src/share/classes
756
757 Windows:
758
759 -d classes
760 -g
761 -sourcepath C:\java\pubs\ws\1.3\src\share\classes
762
763 Create a file named sources that contains the following:
764
765 MyClass1.java
766 MyClass2.java
767 MyClass3.java
768
769 Then, run the javac command as follows:
770
771 javac @options @sources
772
773 Argument Files with Paths
774 The argument files can have paths, but any file names inside the
775 files are relative to the current working directory (not path1
776 or path2):
777
778 javac @path1/options @path2/sources
779
781 In the Java language, classes and interfaces can be organized into
782 packages, and packages can be organized into modules. javac expects
783 that the physical arrangement of source files in directories of the
784 file system will mirror the organization of classes into packages, and
785 packages into modules.
786
787 It is a widely adopted convention that module names and package names
788 begin with a lower-case letter, and that class names begin with an up‐
789 per-case letter.
790
791 Arrangement of Source Code for a Package
792 When classes and interfaces are organized into a package, the package
793 is represented as a directory, and any subpackages are represented as
794 subdirectories.
795
796 For example:
797
798 • The package p is represented as a directory called p.
799
800 • The package p.q -- that is, the subpackage q of package p -- is rep‐
801 resented as the subdirectory q of directory p. The directory tree
802 representing package p.q is therefore p\q on Windows, and p/q on oth‐
803 er systems.
804
805 • The package p.q.r is represented as the directory tree p\q\r (on Win‐
806 dows) or p/q/r (on other systems).
807
808 Within a directory or subdirectory, .java files represent classes and
809 interfaces in the corresponding package or subpackage.
810
811 For example:
812
813 • The class X declared in package p is represented by the file X.java
814 in the p directory.
815
816 • The class Y declared in package p.q is represented by the file Y.java
817 in the q subdirectory of directory p.
818
819 • The class Z declared in package p.q.r is represented by the file
820 Z.java in the r subdirectory of p\q (on Windows) or p/q (on other
821 systems).
822
823 In some situations, it is convenient to split the code into separate
824 directories, each structured as described above, and the aggregate list
825 of directories specified to javac.
826
827 Arrangement of Source Code for a Module
828 In the Java language, a module is a set of packages designed for reuse.
829 In addition to .java files for classes and interfaces, each module has
830 a source file called module-info.java which:
831
832 1. declares the module's name;
833
834 2. lists the packages exported by the module (to allow reuse by other
835 modules);
836
837 3. lists other modules required by the module (to reuse their exported
838 packages).
839
840 When packages are organized into a module, the module is represented by
841 one or more directories representing the packages in the module, one of
842 which contains the module-info.java file. It may be convenient, but it
843 is not required, to use a single directory, named after the module, to
844 contain the module-info.java file alongside the directory tree which
845 represents the packages in the module (i.e., the package hierarchy de‐
846 scribed above). The exact arrangement of source code for a module is
847 typically dictated by the conventions adopted by a development environ‐
848 ment (IDE) or build system.
849
850 For example:
851
852 • The module a.b.c may be represented by the directory a.b.c, on all
853 systems.
854
855 • The module's declaration is represented by the file module-info.java
856 in the a.b.c directory.
857
858 • If the module contains package p.q.r, then the a.b.c directory con‐
859 tains the directory tree p\q\r (on Windows) or p/q/r (on other sys‐
860 tems).
861
862 The development environment may prescribe some directory hierarchy be‐
863 tween the directory named for the module and the source files to be
864 read by javac.
865
866 For example:
867
868 • The module a.b.c may be represented by the directory a.b.c
869
870 • The module's declaration and the module's packages may be in some
871 subdirectory of a.b.c, such as src\main\java (on Windows) or
872 src/main/java (on other systems).
873
875 This section describes how to configure javac to perform a basic compi‐
876 lation.
877
878 See Configuring the Module System for additional details for use when
879 compiling for a release of the platform that supports modules.
880
881 Source Files
882 • Specify the source files to be compiled on the command line.
883
884 If there are no compilation errors, the corresponding class files will
885 be placed in the output directory.
886
887 Some systems may limit the amount you can put on a command line; to
888 work around those limits, you can use argument files.
889
890 When compiling code for modules, you can also specify source files in‐
891 directly, by using the --module or -m option.
892
893 Output Directory
894 • Use the -d option to specify an output directory in which to put the
895 compiled class files.
896
897 This will normally be organized in a package hierarchy, unless you are
898 compiling source code from multiple modules, in which case it will be
899 organized as a module hierarchy.
900
901 When the compilation has been completed, if you are compiling one or
902 more modules, you can place the output directory on the module path for
903 the Java launcher; otherwise, you can place the place the output direc‐
904 tory on the class path for the Java launcher.
905
906 Precompiled Code
907 The code to be compiled may refer to libraries beyond what is provided
908 by the platform. If so, you must place these libraries on the class
909 path or module path. If the library code is not in a module, place it
910 on the class path; if it is in a module, place it on the module path.
911
912 • Use the --class-path option to specify libraries to be placed on the
913 class path. Locations on the class path should be organized in a
914 package hierarchy. You can also use alternate forms of the option:
915 -classpath or -cp.
916
917 • Use the --module-path option to specify libraries to be placed on the
918 module path. Locations on the module path should either be modules
919 or directories of modules. You can also use an alternate form of the
920 option: -p.
921
922 See Configuring the Module System for details on how to modify the
923 default configuration of library modules.
924
925 Note: the options for the class path and module path are not mutually
926 exclusive, although it is not common to specify the class path when
927 compiling code for one or more modules.
928
929 Additional Source Files
930 The code to be compiled may refer to types in additional source files
931 that are not specified on the command line. If so, you must put those
932 source files on either the source path or module path. You can only
933 specify one of these options: if you are not compiling code for a mod‐
934 ule, or if you are only compiling code for a single module, use the
935 source path; if you are compiling code for multiple modules, use the
936 module source path.
937
938 • Use the --source-path option to specify the locations of additional
939 source files that may be read by javac. Locations on the source path
940 should be organized in a package hierarchy. You can also use an al‐
941 ternate form of the option: -sourcepath.
942
943 • Use the --module-source-path option one or more times to specify the
944 location of additional source files in different modules that may be
945 read by javac, or when compiling source files in multiple modules.
946 You can either specify the locations for each module individually, or
947 you can organize the source files so that you can specify the loca‐
948 tions all together. For more details, see The Module Source Path Op‐
949 tion.
950
951 If you want to be able to refer to types in additional source files but
952 do not want them to be compiled, use the -implicit option.
953
954 Note: if you are compiling code for multiple modules, you must always
955 specify a module source path, and all source files specified on the
956 command line must be in one of the directories on the module source
957 path, or in a subdirectory thereof.
958
959 Example of Compiling Multiple Source Files
960 This example compiles the Aloha.java, GutenTag.java, Hello.java, and
961 Hi.java source files in the greetings package.
962
963 Linux and macOS:
964
965 % javac greetings/*.java
966 % ls greetings
967 Aloha.class GutenTag.class Hello.class Hi.class
968 Aloha.java GutenTag.java Hello.java Hi.java
969
970 Windows:
971
972 C:\>javac greetings\*.java
973 C:\>dir greetings
974 Aloha.class GutenTag.class Hello.class Hi.class
975 Aloha.java GutenTag.java Hello.java Hi.java
976
977 Example of Specifying a User Class Path
978 After changing one of the source files in the previous example, recom‐
979 pile it:
980
981 Linux and macOS:
982
983 pwd
984 /examples
985 javac greetings/Hi.java
986
987 Windows:
988
989 C:\>cd
990 \examples
991 C:\>javac greetings\Hi.java
992
993 Because greetings.Hi refers to other classes in the greetings package,
994 the compiler needs to find these other classes. The previous example
995 works because the default user class path is the directory that con‐
996 tains the package directory. If you want to recompile this file with‐
997 out concern for which directory you are in, then add the examples di‐
998 rectory to the user class path by setting CLASSPATH. This example uses
999 the -classpath option.
1000
1001 Linux and macOS:
1002
1003 javac -classpath /examples /examples/greetings/Hi.java
1004
1005 Windows:
1006
1007 C:\>javac -classpath \examples \examples\greetings\Hi.java
1008
1009 If you change greetings.Hi to use a banner utility, then that utility
1010 also needs to be accessible through the user class path.
1011
1012 Linux and macOS:
1013
1014 javac -classpath /examples:/lib/Banners.jar \
1015 /examples/greetings/Hi.java
1016
1017 Windows:
1018
1019 C:\>javac -classpath \examples;\lib\Banners.jar ^
1020 \examples\greetings\Hi.java
1021
1022 To execute a class in the greetings package, the program needs access
1023 to the greetings package, and to the classes that the greetings classes
1024 use.
1025
1026 Linux and macOS:
1027
1028 java -classpath /examples:/lib/Banners.jar greetings.Hi
1029
1030 Windows:
1031
1032 C:\>java -classpath \examples;\lib\Banners.jar greetings.Hi
1033
1035 If you want to include additional modules in your compilation, use the
1036 --add-modules option. This may be necessary when you are compiling
1037 code that is not in a module, or which is in an automatic module, and
1038 the code refers to API in the additional modules.
1039
1040 If you want to restrict the set of modules in your compilation, use the
1041 --limit-modules option. This may be useful if you want to ensure that
1042 the code you are compiling is capable of running on a system with a
1043 limited set of modules installed.
1044
1045 If you want to break encapsulation and specify that additional packages
1046 should be considered as exported from a module, use the --add-exports
1047 option. This may be useful when performing white-box testing; relying
1048 on access to internal API in production code is strongly discouraged.
1049
1050 If you want to specify that additional packages should be considered as
1051 required by a module, use the --add-reads option. This may be useful
1052 when performing white-box testing; relying on access to internal API in
1053 production code is strongly discouraged.
1054
1055 You can patch additional content into any module using the --patch-mod‐
1056 ule option. See [Patching a Module] for more details.
1057
1059 To compile a source file, the compiler often needs information about a
1060 module or type, but the declaration is not in the source files speci‐
1061 fied on the command line.
1062
1063 javac needs type information for every class or interface used, extend‐
1064 ed, or implemented in the source file. This includes classes and in‐
1065 terfaces not explicitly mentioned in the source file, but that provide
1066 information through inheritance.
1067
1068 For example, when you create a subclass of java.awt.Window, you are al‐
1069 so using the ancestor classes of Window: java.awt.Container, ja‐
1070 va.awt.Component, and java.lang.Object.
1071
1072 When compiling code for a module, the compiler also needs to have
1073 available the declaration of that module.
1074
1075 A successful search may produce a class file, a source file, or both.
1076 If both are found, then you can use the -Xprefer option to instruct the
1077 compiler which to use.
1078
1079 If a search finds and uses a source file, then by default javac com‐
1080 piles that source file. This behavior can be altered with -implicit.
1081
1082 The compiler might not discover the need for some type information un‐
1083 til after annotation processing completes. When the type information
1084 is found in a source file and no -implicit option is specified, the
1085 compiler gives a warning that the file is being compiled without being
1086 subject to annotation processing. To disable the warning, either spec‐
1087 ify the file on the command line (so that it will be subject to annota‐
1088 tion processing) or use the -implicit option to specify whether or not
1089 class files should be generated for such source files.
1090
1091 The way that javac locates the declarations of those types depends on
1092 whether the reference exists within code for a module or not.
1093
1094 Searching Package Oriented Paths
1095 When searching for a source or class file on a path composed of package
1096 oriented locations, javac will check each location on the path in turn
1097 for the possible presence of the file. The first occurrence of a par‐
1098 ticular file shadows (hides) any subsequent occurrences of like-named
1099 files. This shadowing does not affect any search for any files with a
1100 different name. This can be convenient when searching for source
1101 files, which may be grouped in different locations, such as shared
1102 code, platform-specific code and generated code. It can also be useful
1103 when injecting alternate versions of a class file into a package, to
1104 debugging or other instrumentation reasons. But, it can also be dan‐
1105 gerous, such as when putting incompatible different versions of a li‐
1106 brary on the class path.
1107
1108 Searching Module Oriented Paths
1109 Prior to scanning any module paths for any package or type declara‐
1110 tions, javac will lazily scan the following paths and locations to de‐
1111 termine the modules that will be used in the compilation.
1112
1113 • The module source path (see the --module-source-path option)
1114
1115 • The path for upgradeable modules (see the --upgrade-module-path op‐
1116 tion)
1117
1118 • The system modules (see the --system option)
1119
1120 • The user module path ( see the --module-path option)
1121
1122 For any module, the first occurrence of the module during the scan com‐
1123 pletely shadows (hides) any subsequent appearance of a like-named mod‐
1124 ule. While locating the modules, javac is able to determine the pack‐
1125 ages exported by the module and to associate with each module a package
1126 oriented path for the contents of the module. For any previously com‐
1127 piled module, this path will typically be a single entry for either a
1128 directory or a file that provides an internal directory-like hierarchy,
1129 such as a JAR file. Thus, when searching for a type that is in a pack‐
1130 age that is known to be exported by a module, javac can locate the dec‐
1131 laration directly and efficiently.
1132
1133 Searching for the Declaration of a Module
1134 If the module has been previously compiled, the module declaration is
1135 located in a file named module-info.class in the root of the package
1136 hierarchy for the content of the module.
1137
1138 If the module is one of those currently being compiled, the module dec‐
1139 laration will be either the file named module-info.class in the root of
1140 the package hierarchy for the module in the class output directory, or
1141 the file named module-info.java in one of the locations on the source
1142 path or one the module source path for the module.
1143
1144 Searching for the Declaration of a Type When the Reference is not in
1145 a Module
1146
1147 When searching for a type that is referenced in code that is not in a
1148 module, javac will look in the following places:
1149
1150 • The platform classes (or the types in exported packages of the plat‐
1151 form modules) (This is for compiled class files only.)
1152
1153 • Types in exported packages of any modules on the module path, if ap‐
1154 plicable. (This is for compiled class files only.)
1155
1156 • Types in packages on the class path and/or source path:
1157
1158 • If both are specified, javac looks for compiled class files on the
1159 class path and for source files on the source path.
1160
1161 • If the class path is specified, but not source path, javac looks
1162 for both compiled class files and source files on the class path.
1163
1164 • If the class path is not specified, it defaults to the current di‐
1165 rectory.
1166
1167 When looking for a type on the class path and/or source path, if both a
1168 compiled class file and a source file are found, the most recently mod‐
1169 ified file will be used by default. If the source file is newer, it
1170 will be compiled and will may override any previously compiled version
1171 of the file. You can use the -Xprefer option to override the default
1172 behavior.
1173
1174 Searching for the Declaration of a Type When the Reference is in a
1175 Module
1176
1177 When searching for a type that is referenced in code in a module, javac
1178 will examine the declaration of the enclosing module to determine if
1179 the type is in a package that is exported from another module that is
1180 readable by the enclosing module. If so, javac will simply and direct‐
1181 ly go to the definition of that module to find the definition of the
1182 required type. Unless the module is another of the modules being com‐
1183 piled, javac will only look for compiled class files files. In other
1184 words, javac will not look for source files in platform modules or mod‐
1185 ules on the module path.
1186
1187 If the type being referenced is not in some other readable module,
1188 javac will examine the module being compiled to try and find the decla‐
1189 ration of the type. javac will look for the declaration of the type as
1190 follows:
1191
1192 • Source files specified on the command line or on the source path or
1193 module source path.
1194
1195 • Previously compiled files in the output directory.
1196
1198 javac generally assumes that source files and compiled class files will
1199 be organized in a file system directory hierarchy or in a type of file
1200 that supports in an internal directory hierarchy, such as a JAR file.
1201 Three different kinds of hierarchy are supported: a package hierarchy,
1202 a module hierarchy, and a module source hierarchy.
1203
1204 While javac is fairly relaxed about the organization of source code,
1205 beyond the expectation that source will be organized in one or package
1206 hierarchies, and can generally accommodate organizations prescribed by
1207 development environments and build tools, Java tools in general, and
1208 javac and the Java launcher in particular, are more stringent regarding
1209 the organization of compiled class files, and will be organized in
1210 package hierarchies or module hierarchies, as appropriate.
1211
1212 The location of these hierarchies are specified to javac with com‐
1213 mand-line options, whose names typically end in "path", like
1214 --source-path or --class-path. Also as a general rule, path options
1215 whose name includes the word module, like --module-path, are used to
1216 specify module hierarchies, although some module-related path options
1217 allow a package hierarchy to be specified on a per-module basis. All
1218 other path options are used to specify package hierarchies.
1219
1220 Package Hierarchy
1221 In a package hierarchy, directories and subdirectories are used to rep‐
1222 resent the component parts of the package name, with the source file or
1223 compiled class file for a type being stored as a file with an extension
1224 of .java or .class in the most nested directory.
1225
1226 For example, in a package hierarchy, the source file for a class
1227 com.example.MyClass will be stored in the file com/example/MyClass.java
1228
1229 Module Hierarchy
1230 In a module hierarchy, the first level of directories are named for the
1231 modules in the hierarchy; within each of those directories the contents
1232 of the module are organized in package hierarchies.
1233
1234 For example, in a module hierarchy, the compiled class file for a type
1235 called com.example.MyClass in a module called my.library will be stored
1236 in my.library/com/example/MyClass.class.
1237
1238 The various output directories used by javac (the class output directo‐
1239 ry, the source output directory, and native header output directory)
1240 will all be organized in a module hierarchy when multiple modules are
1241 being compiled.
1242
1243 Module Source Hierarchy
1244 Although the source for each individual module should always be orga‐
1245 nized in a package hierarchy, it may be convenient to group those hier‐
1246 archies into a module source hierarchy. This is similar to a module
1247 hierarchy, except that there may be intervening directories between the
1248 directory for the module and the directory that is the root of the
1249 package hierarchy for the source code of the module.
1250
1251 For example, in a module source hierarchy, the source file for a type
1252 called com.example.MyClass in a module called my.library may be stored
1253 in a file such as my.library/src/main/java/com/example/MyClass.java.
1254
1256 The --module-source-path option has two forms: a module-specific form,
1257 in which a package path is given for each module containing code to be
1258 compiled, and a module-pattern form, in which the source path for each
1259 module is specified by a pattern. The module-specific form is general‐
1260 ly simpler to use when only a small number of modules are involved; the
1261 module-pattern form may be more convenient when the number of modules
1262 is large and the modules are organized in a regular manner that can be
1263 described by a pattern.
1264
1265 Multiple instances of the --module-source-path option may be given,
1266 each one using either the module-pattern form or the module-specific
1267 form, subject to the following limitations:
1268
1269 • the module-pattern form may be used at most once
1270
1271 • the module-specific form may be used at most once for any given mod‐
1272 ule
1273
1274 If the module-specific form is used for any module, the associated
1275 search path overrides any path that might otherwise have been inferred
1276 from the module-pattern form.
1277
1278 Module-specific form
1279 The module-specific form allows an explicit search path to be given for
1280 any specific module. This form is:
1281
1282 • --module-source-path module-name=file-path (path-separator
1283 file-path)*
1284
1285 The path separator character is ; on Windows, and : otherwise.
1286
1287 Note: this is similar to the form used for the --patch-module option.
1288
1289 Module-pattern form
1290 The module-pattern form allows a concise specification of the module
1291 source path for any number of modules organized in regular manner.
1292
1293 • --module-source-path pattern
1294
1295 The pattern is defined by the following rules, which are applied in or‐
1296 der:
1297
1298 • The argument is considered to be a series of segments separated by
1299 the path separator character (; on Windows, and : otherwise).
1300
1301 • Each segment containing curly braces of the form
1302
1303 string1{alt1 ( ,alt2 )* } string2
1304
1305 is considered to be replaced by a series of segments formed by "ex‐
1306 panding" the braces:
1307
1308 string1 alt1 string2
1309 string1 alt2 string2
1310 and so on...
1311
1312 The braces may be nested.
1313
1314 This rule is applied for all such usages of braces.
1315
1316 • Each segment must have at most one asterisk (*). If a segment does
1317 not contain an asterisk, it is considered to be as though the file
1318 separator character and an asterisk are appended.
1319
1320 For any module M, the source path for that module is formed from the
1321 series of segments obtained by substituting the module name M for the
1322 asterisk in each segment.
1323
1324 Note: in this context, the asterisk is just used as a special marker,
1325 to denote the position in the path of the module name. It should not
1326 be confused with the use of * as a file name wildcard character, as
1327 found on most operating systems.
1328
1330 javac allows any content, whether in source or compiled form, to be
1331 patched into any module using the --patch-module option. You may want
1332 to do this to compile alternative implementations of a class to be
1333 patched at runtime into a JVM, or to inject additional classes into the
1334 module, such as when testing.
1335
1336 The form of the option is:
1337
1338 • --patch-module module-name=file-path (path-separator file-path )*
1339
1340 The path separator character is ; on Windows, and : otherwise. The
1341 paths given for the module must specify the root of a package hierarchy
1342 for the contents of the module
1343
1344 The option may be given at most once for any given module. Any content
1345 on the path will hide any like-named content later in the path and in
1346 the patched module.
1347
1348 When patching source code into more than one module, the --mod‐
1349 ule-source-path must also be used, so that the output directory is or‐
1350 ganized in a module hierarchy, and capable of holding the compiled
1351 class files for the modules being compiled.
1352
1354 The javac command provides direct support for annotation processing.
1355
1356 The API for annotation processors is defined in the javax.annota‐
1357 tion.processing and javax.lang.model packages and subpackages.
1358
1359 How Annotation Processing Works
1360 Unless annotation processing is disabled with the -proc:none option,
1361 the compiler searches for any annotation processors that are available.
1362 The search path can be specified with the -processorpath option. If no
1363 path is specified, then the user class path is used. Processors are
1364 located by means of service provider-configuration files named
1365 META-INF/services/javax.annotation.processing. Processor on the search
1366 path. Such files should contain the names of any annotation processors
1367 to be used, listed one per line. Alternatively, processors can be
1368 specified explicitly, using the -processor option.
1369
1370 After scanning the source files and classes on the command line to de‐
1371 termine what annotations are present, the compiler queries the proces‐
1372 sors to determine what annotations they process. When a match is
1373 found, the processor is called. A processor can claim the annotations
1374 it processes, in which case no further attempt is made to find any pro‐
1375 cessors for those annotations. After all of the annotations are
1376 claimed, the compiler does not search for additional processors.
1377
1378 If any processors generate new source files, then another round of an‐
1379 notation processing occurs: Any newly generated source files are
1380 scanned, and the annotations processed as before. Any processors
1381 called on previous rounds are also called on all subsequent rounds.
1382 This continues until no new source files are generated.
1383
1384 After a round occurs where no new source files are generated, the anno‐
1385 tation processors are called one last time, to give them a chance to
1386 complete any remaining work. Finally, unless the -proc:only option is
1387 used, the compiler compiles the original and all generated source
1388 files.
1389
1390 If you use an annotation processor that generates additional source
1391 files to be included in the compilation, you can specify a default mod‐
1392 ule to be used for the newly generated files, for use when a module
1393 declaration is not also generated. In this case, use the --de‐
1394 fault-module-for-created-files option.
1395
1396 Compilation Environment and Runtime Environment.
1397 The declarations in source files and previously compiled class files
1398 are analyzed by javac in a compilation environment that is distinct
1399 from the runtime environment used to execute javac itself. Although
1400 there is a deliberate similarity between many javac options and
1401 like-named options for the Java launcher, such as --class-path, --mod‐
1402 ule-path and so on, it is important to understand that in general the
1403 javac options just affect the environment in which the source files are
1404 compiled, and do not affect the operation of javac itself.
1405
1406 The distinction between the compilation environment and runtime envi‐
1407 ronment is significant when it comes to using annotation processors.
1408 Although annotations processors process elements (declarations) that
1409 exist in the compilation environment, the annotation processor itself
1410 is executed in the runtime environment. If an annotation processor has
1411 dependencies on libraries that are not in modules, the libraries can be
1412 placed, along with the annotation processor itself, on the processor
1413 path. (See the --processor-path option.) If the annotation processor
1414 and its dependencies are in modules, you should use the processor mod‐
1415 ule path instead. (See the --processor-module-path option.) When those
1416 are insufficient, it may be necessary to provide further configuration
1417 of the runtime environment. This can be done in two ways:
1418
1419 1. If javac is invoked from the command line, options can be passed to
1420 the underlying runtime by prefixing the option with -J.
1421
1422 2. You can start an instance of a Java Virtual Machine directly and use
1423 command line options and API to configure an environment in which
1424 javac can be invoked via one of its APIs.
1425
1427 javac can compile code that is to be used on other releases of the
1428 platform, using either the --release option, or the --source/-source
1429 and --target/-target options, together with additional options to spec‐
1430 ify the platform classes.
1431
1432 Depending on the desired platform release, there are some restrictions
1433 on some of the options that can be used.
1434
1435 • When compiling for JDK 8 and earlier releases, you cannot use any op‐
1436 tion that is intended for use with the module system. This includes
1437 all of the following options:
1438
1439 • --module-source-path, --upgrade-module-path, --system, --mod‐
1440 ule-path, --add-modules, --add-exports, --add-opens, --add-reads,
1441 --limit-modules, --patch-module
1442
1443 If you use the --source/-source or --target/-target options, you
1444 should also set the appropriate platform classes using the boot class
1445 path family of options.
1446
1447 • When compiling for JDK 9 and later releases, you cannot use any op‐
1448 tion that is intended to configure the boot class path. This in‐
1449 cludes all of the following options:
1450
1451 • -Xbootclasspath/p:, -Xbootclasspath, -Xbootclasspath/a:, -endorsed‐
1452 dirs, -Djava.endorsed.dirs, -extdirs, -Djava.ext.dirs, -profile
1453
1454 If you use the --source/-source or --target/-target options, you
1455 should also set the appropriate platform classes using the --system
1456 option to give the location of an appropriate installed release of
1457 JDK.
1458
1459 When using the --release option, only the supported documented API for
1460 that release may be used; you cannot use any options to break encapsu‐
1461 lation to access any internal classes.
1462
1464 The javac compiler can be invoked using an API in three different ways:
1465
1466 The Java Compiler API
1467 This provides the most flexible way to invoke the compiler, in‐
1468 cluding the ability to compile source files provided in memory
1469 buffers or other non-standard file systems.
1470
1471 The ToolProvider API
1472 A ToolProvider for javac can be obtained by calling Tool‐
1473 Provider.findFirst("javac"). This returns an object with the
1474 equivalent functionality of the command-line tool.
1475
1476 Note: This API should not be confused with the like-named API in
1477 the javax.tools package.
1478
1479 The javac Legacy API
1480 This API is retained for backward compatibility only. All new
1481 code should use either the Java Compiler API or the ToolProvider
1482 API.
1483
1484 Note: All other classes and methods found in a package with names that
1485 start with com.sun.tools.javac (subpackages of com.sun.tools.javac) are
1486 strictly internal and subject to change at any time.
1487
1489 cast Warns about unnecessary and redundant casts, for example:
1490
1491 String s = (String) "Hello!"
1492
1493 classfile
1494 Warns about issues related to class file contents.
1495
1496 deprecation
1497 Warns about the use of deprecated items. For example:
1498
1499 java.util.Date myDate = new java.util.Date();
1500 int currentDay = myDate.getDay();
1501
1502 The method java.util.Date.getDay has been deprecated since JDK
1503 1.1.
1504
1505 dep-ann
1506 Warns about items that are documented with the @deprecated
1507 Javadoc comment, but do not have the @Deprecated annotation, for
1508 example:
1509
1510 /**
1511 * @deprecated As of Java SE 7, replaced by {@link #newMethod()}
1512 */
1513 public static void deprecatedMethod() { }
1514 public static void newMethod() { }
1515
1516 divzero
1517 Warns about division by the constant integer 0, for example:
1518
1519 int divideByZero = 42 / 0;
1520
1521 empty Warns about empty statements after ifstatements, for example:
1522
1523 class E {
1524 void m() {
1525 if (true) ;
1526 }
1527 }
1528
1529 fallthrough
1530 Checks the switch blocks for fall-through cases and provides a
1531 warning message for any that are found. Fall-through cases are
1532 cases in a switch block, other than the last case in the block,
1533 whose code does not include a break statement, allowing code ex‐
1534 ecution to fall through from that case to the next case. For
1535 example, the code following the case 1 label in this switch
1536 block does not end with a break statement:
1537
1538 switch (x) {
1539 case 1:
1540 System.out.println("1");
1541 // No break statement here.
1542 case 2:
1543 System.out.println("2");
1544 }
1545
1546 If the -Xlint:fallthrough option was used when compiling this
1547 code, then the compiler emits a warning about possible
1548 fall-through into case, with the line number of the case in
1549 question.
1550
1551 finally
1552 Warns about finally clauses that cannot be completed normally,
1553 for example:
1554
1555 public static int m() {
1556 try {
1557 throw new NullPointerException();
1558 } catch (NullPointerException(); {
1559 System.err.println("Caught NullPointerException.");
1560 return 1;
1561 } finally {
1562 return 0;
1563 }
1564 }
1565
1566 The compiler generates a warning for the finally block in this
1567 example. When the int method is called, it returns a value of
1568 0. A finally block executes when the try block exits. In this
1569 example, when control is transferred to the catch block, the int
1570 method exits. However, the finally block must execute, so it's
1571 executed, even though control was transferred outside the meth‐
1572 od.
1573
1574 options
1575 Warns about issues that related to the use of command-line op‐
1576 tions. See Compiling for Earlier Releases of the Platform.
1577
1578 overrides
1579 Warns about issues related to method overrides. For example,
1580 consider the following two classes:
1581
1582 public class ClassWithVarargsMethod {
1583 void varargsMethod(String... s) { }
1584 }
1585
1586 public class ClassWithOverridingMethod extends ClassWithVarargsMethod {
1587 @Override
1588 void varargsMethod(String[] s) { }
1589 }
1590
1591 The compiler generates a warning similar to the following:.
1592
1593 warning: [override] varargsMethod(String[]) in ClassWithOverridingMethod
1594 overrides varargsMethod(String...) in ClassWithVarargsMethod; overriding
1595 method is missing '...'
1596
1597 When the compiler encounters a varargs method, it translates the
1598 varargs formal parameter into an array. In the method Class‐
1599 WithVarargsMethod.varargsMethod, the compiler translates the
1600 varargs formal parameter String... s to the formal parameter
1601 String[] s, an array that matches the formal parameter of the
1602 method ClassWithOverridingMethod.varargsMethod. Consequently,
1603 this example compiles.
1604
1605 path Warns about invalid path elements and nonexistent path directo‐
1606 ries on the command line (with regard to the class path, the
1607 source path, and other paths). Such warnings cannot be sup‐
1608 pressed with the @SuppressWarnings annotation. For example:
1609
1610 • Linux and macOS: javac -Xlint:path -classpath /nonexistent‐
1611 path Example.java
1612
1613 • Windows: javac -Xlint:path -classpath C:\nonexistentpath Exam‐
1614 ple.java
1615
1616 processing
1617 Warns about issues related to annotation processing. The com‐
1618 piler generates this warning when you have a class that has an
1619 annotation, and you use an annotation processor that cannot han‐
1620 dle that type of annotation. For example, the following is a
1621 simple annotation processor:
1622
1623 Source file AnnoProc.java:
1624
1625 import java.util.*;
1626 import javax.annotation.processing.*;
1627 import javax.lang.model.*;
1628 import javax.lang.model.element.*;
1629
1630 @SupportedAnnotationTypes("NotAnno")
1631 public class AnnoProc extends AbstractProcessor {
1632 public boolean process(Set<? extends TypeElement> elems, RoundEnvironment renv){
1633 return true;
1634 }
1635
1636 public SourceVersion getSupportedSourceVersion() {
1637 return SourceVersion.latest();
1638 }
1639 }
1640
1641 Source file AnnosWithoutProcessors.java:
1642
1643 @interface Anno { }
1644
1645 @Anno
1646 class AnnosWithoutProcessors { }
1647
1648 The following commands compile the annotation processor Anno‐
1649 Proc, then run this annotation processor against the source file
1650 AnnosWithoutProcessors.java:
1651
1652 javac AnnoProc.java
1653 javac -cp . -Xlint:processing -processor AnnoProc -proc:only AnnosWithoutProcessors.java
1654
1655 When the compiler runs the annotation processor against the
1656 source file AnnosWithoutProcessors.java, it generates the fol‐
1657 lowing warning:
1658
1659 warning: [processing] No processor claimed any of these annotations: Anno
1660
1661 To resolve this issue, you can rename the annotation defined and
1662 used in the class AnnosWithoutProcessors from Anno to NotAnno.
1663
1664 rawtypes
1665 Warns about unchecked operations on raw types. The following
1666 statement generates a rawtypes warning:
1667
1668 void countElements(List l) { ... }
1669
1670 The following example does not generate a rawtypes warning:
1671
1672 void countElements(List<?> l) { ... }
1673
1674 List is a raw type. However, List<?> is an unbounded wildcard
1675 parameterized type. Because List is a parameterized interface,
1676 always specify its type argument. In this example, the List
1677 formal argument is specified with an unbounded wildcard (?) as
1678 its formal type parameter, which means that the countElements
1679 method can accept any instantiation of the List interface.
1680
1681 serial Warns about missing serialVersionUID definitions on serializable
1682 classes. For example:
1683
1684 public class PersistentTime implements Serializable
1685 {
1686 private Date time;
1687
1688 public PersistentTime() {
1689 time = Calendar.getInstance().getTime();
1690 }
1691
1692 public Date getTime() {
1693 return time;
1694 }
1695 }
1696
1697 The compiler generates the following warning:
1698
1699 warning: [serial] serializable class PersistentTime has no definition of
1700 serialVersionUID
1701
1702 If a serializable class does not explicitly declare a field
1703 named serialVersionUID, then the serialization runtime environ‐
1704 ment calculates a default serialVersionUID value for that class
1705 based on various aspects of the class, as described in the Java
1706 Object Serialization Specification. However, it's strongly rec‐
1707 ommended that all serializable classes explicitly declare seri‐
1708 alVersionUID values because the default process of computing se‐
1709 rialVersionUID values is highly sensitive to class details that
1710 can vary depending on compiler implementations. As a result,
1711 this might cause an unexpected InvalidClassExceptions during de‐
1712 serialization. To guarantee a consistent serialVersionUID value
1713 across different Java compiler implementations, a serializable
1714 class must declare an explicit serialVersionUID value.
1715
1716 static Warns about issues relating to the use of static variables, for
1717 example:
1718
1719 class XLintStatic {
1720 static void m1() { }
1721 void m2() { this.m1(); }
1722 }
1723
1724 The compiler generates the following warning:
1725
1726 warning: [static] static method should be qualified by type name,
1727 XLintStatic, instead of by an expression
1728
1729 To resolve this issue, you can call the static method m1 as fol‐
1730 lows:
1731
1732 XLintStatic.m1();
1733
1734 Alternately, you can remove the static keyword from the declara‐
1735 tion of the method m1.
1736
1737 try Warns about issues relating to the use of try blocks, including
1738 try-with-resources statements. For example, a warning is gener‐
1739 ated for the following statement because the resource ac de‐
1740 clared in the try block is not used:
1741
1742 try ( AutoCloseable ac = getResource() ) { // do nothing}
1743
1744 unchecked
1745 Gives more detail for unchecked conversion warnings that are
1746 mandated by the Java Language Specification, for example:
1747
1748 List l = new ArrayList<Number>();
1749 List<String> ls = l; // unchecked warning
1750
1751 During type erasure, the types ArrayList<Number> and
1752 List<String> become ArrayList and List, respectively.
1753
1754 The ls command has the parameterized type List<String>. When
1755 the List referenced by l is assigned to ls, the compiler gener‐
1756 ates an unchecked warning. At compile time, the compiler and
1757 JVM cannot determine whether l refers to a List<String> type.
1758 In this case, l does not refer to a List<String> type. As a re‐
1759 sult, heap pollution occurs.
1760
1761 A heap pollution situation occurs when the List object l, whose
1762 static type is List<Number>, is assigned to another List object,
1763 ls, that has a different static type, List<String>. However,
1764 the compiler still allows this assignment. It must allow this
1765 assignment to preserve backward compatibility with releases of
1766 Java SE that do not support generics. Because of type erasure,
1767 List<Number> and List<String> both become List. Consequently,
1768 the compiler allows the assignment of the object l, which has a
1769 raw type of List, to the object ls.
1770
1771 varargs
1772 Warns about unsafe use of variable arguments (varargs) methods,
1773 in particular, those that contain non-reifiable arguments, for
1774 example:
1775
1776 public class ArrayBuilder {
1777 public static <T> void addToList (List<T> listArg, T... elements) {
1778 for (T x : elements) {
1779 listArg.add(x);
1780 }
1781 }
1782 }
1783
1784 A non-reifiable type is a type whose type information is not
1785 fully available at runtime.
1786
1787 The compiler generates the following warning for the definition
1788 of the method ArrayBuilder.addToList:
1789
1790 warning: [varargs] Possible heap pollution from parameterized vararg type T
1791
1792 When the compiler encounters a varargs method, it translates the
1793 varargs formal parameter into an array. However, the Java pro‐
1794 gramming language does not permit the creation of arrays of pa‐
1795 rameterized types. In the method ArrayBuilder.addToList, the
1796 compiler translates the varargs formal parameter T... elements
1797 to the formal parameter T[] elements, an array. However, be‐
1798 cause of type erasure, the compiler converts the varargs formal
1799 parameter to Object[] elements. Consequently, there's a possi‐
1800 bility of heap pollution.
1801
1802
1803
1804JDK 18 2022 JAVAC(1)