blob: 2e9c76a343d3dfd21c7c2c18625f259085ca1ba0 [file] [log] [blame]
alanb0d058232012-11-02 15:50:11 +00001." Copyright (c) 1994, 2012, Oracle and/or its affiliates. All rights reserved.
michaelm5ac8c152012-03-06 20:34:38 +00002." DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
3."
4." This code is free software; you can redistribute it and/or modify it
5." under the terms of the GNU General Public License version 2 only, as
6." published by the Free Software Foundation.
7."
8." This code is distributed in the hope that it will be useful, but WITHOUT
9." ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
10." FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
11." version 2 for more details (a copy is included in the LICENSE file that
12." accompanied this code).
13."
14." You should have received a copy of the GNU General Public License version
15." 2 along with this work; if not, write to the Free Software Foundation,
16." Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
17."
18." Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
19." or visit www.oracle.com if you need additional information or have any
20." questions.
21."
22.TH javac 1 "10 May 2011"
23
24.LP
25.SH "Name"
26javac \- Java programming language compiler
27.LP
28.SH "SYNOPSIS"
29.LP
30.nf
31\f3
32.fl
33 \fP\f3javac\fP [ options ] [ sourcefiles ] [ classes ] [ @argfiles ]
34.fl
35
36.fl
37.fi
38
39.LP
40.LP
41Arguments may be in any order.
42.LP
43.RS 3
44.TP 3
45options
46Command\-line options.
47.TP 3
48sourcefiles
49One or more source files to be compiled (such as MyClass.java).
50.TP 3
51classes
52One or more classes to be processed for annotations (such as MyPackage.MyClass).
53.TP 3
54@argfiles
55One or more files that lists options and source files. The \f2\-J\fP options are not allowed in these files.
56.RE
57
58.LP
59.SH "DESCRIPTION"
60.LP
61.LP
62The \f3javac\fP tool reads class and interface definitions, written in the Java programming language, and compiles them into bytecode class files. It can also process annotations in Java source files and classes.
63.LP
64.LP
65There are two ways to pass source code file names to \f3javac\fP:
66.LP
67.RS 3
68.TP 2
69o
70For a small number of source files, simply list the file names on the command line.
71.TP 2
72o
73For a large number of source files, list the file names in a file, separated by blanks or line breaks. Then use the list file name on the \f3javac\fP command line, preceded by an \f3@\fP character.
74.RE
75
76.LP
77.LP
78Source code file names must have \f2.java\fP suffixes, class file names must have \f2.class\fP suffixes, and both source and class files must have root names that identify the class. For example, a class called \f2MyClass\fP would be written in a source file called \f2MyClass.java\fP and compiled into a bytecode class file called \f2MyClass.class\fP.
79.LP
80.LP
81Inner class definitions produce additional class files. These class files have names combining the inner and outer class names, such as \f2MyClass$MyInnerClass.class\fP.
82.LP
83.LP
84You should arrange source files in a directory tree that reflects their package tree. For example, if you keep all your source files in \f3/workspace\fP, the source code for \f2com.mysoft.mypack.MyClass\fP should be in \f3/workspace/com/mysoft/mypack/MyClass.java\fP.
85.LP
86.LP
87By default, the compiler puts each class file in the same directory as its source file. You can specify a separate destination directory with \f3\-d\fP (see Options, below).
88.LP
89.SH "OPTIONS"
90.LP
91.LP
92The compiler has a set of standard options that are supported on the current development environment and will be supported in future releases. An additional set of non\-standard options are specific to the current virtual machine and compiler implementations and are subject to change in the future. Non\-standard options begin with \f3\-X\fP.
93.LP
94.SS
95Standard Options
96.LP
97.RS 3
98.TP 3
99\-Akey[=value]
100Options to pass to annotation processors. These are not interpreted by javac directly, but are made available for use by individual processors. \f2key\fP should be one or more identifiers separated by ".".
101.TP 3
102\-cp path or \-classpath path
103Specify where to find user class files, and (optionally) annotation processors and source files. This class path overrides the user class path in the \f3CLASSPATH\fP environment variable. If neither \f3CLASSPATH\fP, \f3\-cp\fP nor \f3\-classpath\fP is specified, the user class path consists of the current directory. See Setting the Class Path for more details.
104.br
105.br
106>If the \f3\-sourcepath\fP option is not specified, the user class path is also searched for source files.
107.br
108.br
109If the \f3\-processorpath\fP option is not specified, the class path is also searched for annotation processors.
110.TP 3
111\-Djava.ext.dirs=directories
112Override the location of installed extensions.
113.TP 3
114\-Djava.endorsed.dirs=directories
115Override the location of endorsed standards path.
116.TP 3
117\-d directory
118Set the destination directory for class files. The directory must already exist; \f3javac\fP will not create it. If a class is part of a package, \f3javac\fP puts the class file in a subdirectory reflecting the package name, creating directories as needed. For example, if you specify \f3\-d /home/myclasses\fP and the class is called \f2com.mypackage.MyClass\fP, then the class file is called \f2/home/myclasses/com/mypackage/MyClass.class\fP.
119.br
120.br
121If \f3\-d\fP is not specified, \f3javac\fP puts each class files in the same directory as the source file from which it was generated.
122.br
123.br
124\f3Note:\fP The directory specified by \f3\-d\fP is not automatically added to your user class path.
125.TP 3
126\-deprecation
127Show a description of each use or override of a deprecated member or class. Without \f3\-deprecation\fP, \f3javac\fP shows a summary of the source files that use or override deprecated members or classes. \f3\-deprecation\fP is shorthand for \f3\-Xlint:deprecation\fP.
128.TP 3
129\-encoding encoding
130Set the source file encoding name, such as \f2EUC\-JP and UTF\-8\fP. If \f3\-encoding\fP is not specified, the platform default converter is used.
131.TP 3
132\-endorseddirs directories
133Override the location of endorsed standards path.
134.TP 3
135\-extdirs directories
136Overrides the location of the \f2ext\fP directory. The \f2directories\fP variable is a colon\-separated list of directories. Each JAR archive in the specified directories is searched for class files. All JAR archives found are automatically part of the class path.
137.br
138.br
139If you are cross\-compiling (compiling classes against bootstrap and extension classes of a different Java platform implementation), this option specifies the directories that contain the extension classes. See Cross\-Compilation Options for more information.
140.TP 3
141\-g
142Generate all debugging information, including local variables. By default, only line number and source file information is generated.
143.TP 3
144\-g:none
145Do not generate any debugging information.
146.TP 3
147\-g:{keyword list}
148Generate only some kinds of debugging information, specified by a comma separated list of keywords. Valid keywords are:
149.RS 3
150.TP 3
151source
152Source file debugging information
153.TP 3
154lines
155Line number debugging information
156.TP 3
157vars
158Local variable debugging information
159.RE
160.TP 3
161\-help
162Print a synopsis of standard options.
163.TP 3
164\-implicit:{class,none}
165Controls the generation of class files for implicitly loaded source files. To automatically generate class files, use \f3\-implicit:class\fP. To suppress class file generation, use \f3\-implicit:none\fP. If this option is not specified, the default is to automatically generate class files. In this case, the compiler will issue a warning if any such class files are generated when also doing annotation processing. The warning will not be issued if this option is set explicitly. See Searching For Types.
166.TP 3
167\-Joption
168Pass \f2option\fP to the \f3java\fP launcher called by \f3javac\fP. For example, \f3\-J\-Xms48m\fP sets the startup memory to 48 megabytes. It is a common convention for \f3\-J\fP to pass options to the underlying VM executing applications written in Java.
169.br
170.br
171\f3Note:\fP \f3CLASSPATH\fP, \f3\-classpath\fP, \f3\-bootclasspath\fP, and \f3\-extdirs\fP do \f2not\fP specify the classes used to run \f3javac\fP. Fiddling with the implementation of the compiler in this way is usually pointless and always risky. If you do need to do this, use the \f3\-J\fP option to pass through options to the underlying \f3java\fP launcher.
172.TP 3
173\-nowarn
174Disable warning messages. This has the same meaning as \f3\-Xlint:none\fP.
175.TP 3
176\-proc: {none,only}
177Controls whether annotation processing and/or compilation is done. \f3\-proc:none\fP means that compilation takes place without annotation processing. \f3\-proc:only\fP means that only annotation processing is done, without any subsequent compilation.
178.TP 3
179\-processor class1[,class2,class3...]
180Names of the annotation processors to run. This bypasses the default discovery process.
181.TP 3
182\-processorpath path
183Specify where to find annotation processors; if this option is not used, the class path will be searched for processors.
184.TP 3
185\-s dir
186Specify the directory where to place generated source files. The directory must already exist; \f3javac\fP will not create it. If a class is part of a package, the compiler puts the source file in a subdirectory reflecting the package name, creating directories as needed. For example, if you specify \f3\-s /home/mysrc\fP and the class is called \f2com.mypackage.MyClass\fP, then the source file will be placed in \f2/home/mysrc/com/mypackage/MyClass.java\fP.
187.TP 3
188\-source release
189Specifies the version of source code accepted. The following values for \f2release\fP are allowed:
190.RS 3
191.TP 3
1921.3
193The compiler does \f2not\fP support assertions, generics, or other language features introduced after JDK 1.3.
194.TP 3
1951.4
196The compiler accepts code containing assertions, which were introduced in JDK 1.4.
197.TP 3
1981.5
199The compiler accepts code containing generics and other language features introduced in JDK 5.
200.TP 3
2015
202Synonym for 1.5.
203.TP 3
2041.6
205This is the default value. No language changes were introduced in Java SE 6. However, encoding errors in source files are now reported as errors, instead of warnings, as previously.
206.TP 3
2076
208Synonym for 1.6.
209.TP 3
2101.7
211The compiler accepts code with features introduced in JDK 7.
212.TP 3
2137
214Synonym for 1.7.
215.RE
216.TP 3
217\-sourcepath sourcepath
218Specify the source code path to search for class or interface definitions. As with the user class path, source path entries are separated by colons (\f3:\fP) and can be directories, JAR archives, or ZIP archives. If packages are used, the local path name within the directory or archive must reflect the package name.
219.br
220.br
221\f3Note:\fP Classes found through the class path may be subject to automatic recompilation if their sources are also found. See Searching For Types.
222.TP 3
223\-verbose
224Verbose output. This includes information about each class loaded and each source file compiled.
225.TP 3
226\-version
227Print version information.
228.TP 3
229\-Werror
230Terminate compilation if warnings occur.
231.TP 3
232\-X
233Display information about non\-standard options and exit.
234.RE
235
236.LP
237.SS
238Cross\-Compilation Options
239.LP
240.LP
241By default, classes are compiled against the bootstrap and extension classes of the platform that \f3javac\fP shipped with. But \f3javac\fP also supports \f2cross\-compiling\fP, where classes are compiled against a bootstrap and extension classes of a different Java platform implementation. It is important to use \f3\-bootclasspath\fP and \f3\-extdirs\fP when cross\-compiling; see Cross\-Compilation Example below.
242.LP
243.RS 3
244.TP 3
245\-target version
246Generate class files that target a specified version of the VM. Class files will run on the specified target and on later versions, but not on earlier versions of the VM. Valid targets are \f31.1\fP \f31.2\fP \f31.3\fP \f31.4\fP \f31.5\fP (also \f35\fP) \f31.6\fP (also \f36\fP) and \f31.7\fP (also \f37\fP).
247.br
248.br
249The default for \f3\-target\fP depends on the value of \f3\-source\fP:
250.RS 3
251.TP 2
252o
253If \-source is \f3not specified\fP, the value of \-target is \f31.7\fP
254.TP 2
255o
256If \-source is \f31.2\fP, the value of \-target is \f31.4\fP
257.TP 2
258o
259If \-source is \f31.3\fP, the value of \-target is \f31.4\fP
260.TP 2
261o
262For \f3all other values\fP of \-source, the value of \f3\-target\fP is the value of \f3\-source\fP.
263.RE
264.TP 3
265\-bootclasspath bootclasspath
266Cross\-compile against the specified set of boot classes. As with the user class path, boot class path entries are separated by colons (\f3:\fP) and can be directories, JAR archives, or ZIP archives.
267.RE
268
269.LP
270.SS
271Non\-Standard Options
272.LP
273.RS 3
274.TP 3
275\-Xbootclasspath/p:path
276Prepend to the bootstrap class path.
277.TP 3
278\-Xbootclasspath/a:path
279Append to the bootstrap class path.
280.TP 3
281\-Xbootclasspath/:path
282Override location of bootstrap class files.
283.TP 3
284\-Xlint
285Enable all recommended warnings. In this release, enabling all available warnings is recommended.
286.TP 3
287\-Xlint:all
288Enable all recommended warnings. In this release, enabling all available warnings is recommended.
289.TP 3
290\-Xlint:none
291Disable all warnings.
292.TP 3
293\-Xlint:name
294Enable warning \f2name\fP. See the section Warnings That Can Be Enabled or Disabled with \-Xlint Option for a list of warnings you can enable with this option.
295.TP 3
296\-Xlint:\-name
297Disable warning \f2name\fP. See the section Warnings That Can Be Enabled or Disabled with \-Xlint Option for a list of warnings you can disable with this option.
298.TP 3
299\-Xmaxerrs number
300Set the maximum number of errors to print.
301.TP 3
302\-Xmaxwarns number
303Set the maximum number of warnings to print.
304.TP 3
305\-Xstdout filename
306Send compiler messages to the named file. By default, compiler messages go to \f2System.err\fP.
307.TP 3
308\-Xprefer:{newer,source}
309Specify which file to read when both a source file and class file are found for a type. (See Searching For Types). If \f2\-Xprefer:newer\fP is used, it reads the newer of the source or class file for a type (default). If the \f2\-Xprefer:source\fP option is used, it reads source file. Use \f2\-Xprefer:source\fP when you want to be sure that any annotation processors can access annotations declared with a retention policy of \f2SOURCE\fP.
310.TP 3
311\-Xpkginfo:{always,legacy,nonempty}
312Specify handling of package\-info files
313.TP 3
314\-Xprint
315Print out textual representation of specified types for debugging purposes; perform neither annotation processing nor compilation. The format of the output may change.
316.TP 3
317\-XprintProcessorInfo
318Print information about which annotations a processor is asked to process.
319.TP 3
320\-XprintRounds
321Print information about initial and subsequent annotation processing rounds.
322.RE
323
324.LP
325.SS
326Warnings That Can Be Enabled or Disabled with \-Xlint Option
327.LP
328.LP
329Enable warning \f2name\fP with the option \f3\-Xlint:\fP\f2name\fP, where \f2name\fP is one of the following warning names. Similarly, you can disable warning \f2name\fP with the option \f3\-Xlint:\-\fP\f2name\fP:
330.LP
331.RS 3
332.TP 3
333cast
334Warn about unnecessary and redundant casts. For example:
335.nf
336\f3
337.fl
338String s = (String)"Hello!"
339.fl
340\fP
341.fi
342.TP 3
343classfile
344Warn about issues related to classfile contents.
345.TP 3
346deprecation
347Warn about use of deprecated items. For example:
348.nf
349\f3
350.fl
351 java.util.Date myDate = new java.util.Date();
352.fl
353 int currentDay = myDate.getDay();
354.fl
355\fP
356.fi
357The method \f2java.util.Date.getDay\fP has been deprecated since JDK 1.1.
358.TP 3
359dep\-ann
360Warn about items that are documented with an \f2@deprecated\fP Javadoc comment, but do not have a \f2@Deprecated\fP annotation. For example:
361.nf
362\f3
363.fl
364 /**
365.fl
366 * @deprecated As of Java SE 7, replaced by {@link #newMethod()}
367.fl
368 */
369.fl
370
371.fl
372 public static void deprecatedMethood() { }
373.fl
374
375.fl
376 public static void newMethod() { }
377.fl
378\fP
379.fi
380.TP 3
381divzero
382Warn about division by constant integer 0. For example:
383.nf
384\f3
385.fl
386 int divideByZero = 42 / 0;
387.fl
388\fP
389.fi
390.TP 3
391empty
392Warn about empty statements after \f2if\fP statements. For example:
393.nf
394\f3
395.fl
396class E {
397.fl
398 void m() {
399.fl
400 if (true) ;
401.fl
402 }
403.fl
404}
405.fl
406\fP
407.fi
408.TP 3
409fallthrough
410Check \f2switch\fP blocks for fall\-through cases and provide a warning message for any that are found. Fall\-through cases are cases in a \f2switch\fP block, other than the last case in the block, whose code does not include a \f2break\fP statement, allowing code execution to "fall through" from that case to the next case. For example, the code following the \f2case 1\fP label in this \f2switch\fP block does not end with a \f2break\fP statement:
411.nf
412\f3
413.fl
414switch (x) {
415.fl
416case 1:
417.fl
418 System.out.println("1");
419.fl
420 // No break statement here.
421.fl
422case 2:
423.fl
424 System.out.println("2");
425.fl
426}
427.fl
428\fP
429.fi
430If the \f2\-Xlint:fallthrough\fP flag were used when compiling this code, the compiler would emit a warning about "possible fall\-through into case," along with the line number of the case in question.
431.TP 3
432finally
433Warn about \f2finally\fP clauses that cannot complete normally. For example:
434.nf
435\f3
436.fl
437 public static int m() {
438.fl
439 try {
440.fl
441 throw new NullPointerException();
442.fl
443 } catch (NullPointerException e) {
444.fl
445 System.err.println("Caught NullPointerException.");
446.fl
447 return 1;
448.fl
449 } finally {
450.fl
451 return 0;
452.fl
453 }
454.fl
455 }
456.fl
457\fP
458.fi
459The compiler generates a warning for \f2finally\fP block in this example. When this method is called, it returns a value of \f20\fP, not \f21\fP. A \f2finally\fP block always executes when the \f2try\fP block exits. In this example, if control is transferred to the \f2catch\fP, then the method exits. However, the \f2finally\fP block must be executed, so it is executed, even though control has already been transferred outside the method.
460.TP 3
461options
462Warn about issues relating to the use of command line options. See Cross\-Compilation Example for an example of this kind of warning.
463.TP 3
464overrides
465Warn about issues regarding method overrides. For example, consider the following two classes:
466.nf
467\f3
468.fl
469public class ClassWithVarargsMethod {
470.fl
471 void varargsMethod(String... s) { }
472.fl
473}
474.fl
475\fP
476.fi
477.nf
478\f3
479.fl
480public class ClassWithOverridingMethod extends ClassWithVarargsMethod {
481.fl
482 @Override
483.fl
484 void varargsMethod(String[] s) { }
485.fl
486}
487.fl
488\fP
489.fi
490The compiler generates a warning similar to the following:
491.br
492.br
493\f2warning: [override] varargsMethod(String[]) in ClassWithOverridingMethod overrides varargsMethod(String...) in ClassWithVarargsMethod; overriding method is missing '...'\fP
494.br
495.br
496When the compiler encounters a varargs method, it translates the varargs formal parameter into an array. In the method \f2ClassWithVarargsMethod.varargsMethod\fP, the compiler translates the varargs formal parameter \f2String... s\fP to the formal parameter \f2String[] s\fP, an array, which matches the formal parameter of the method \f2ClassWithOverridingMethod.varargsMethod\fP. Consequently, this example compiles.
497.TP 3
498path
499Warn about invalid path elements and nonexistent path directories on the command line (with regards to the class path, the source path, and other paths). Such warnings cannot be suppressed with the \f2@SuppressWarnings\fP annotation. For example:
500.nf
501\f3
502.fl
503javac \-Xlint:path \-classpath /nonexistentpath Example.java
504.fl
505\fP
506.fi
507.TP 3
508processing
509Warn about issues regarding annotation processing. The compiler generates this warning if you have a class that has an annotation, and you use an annotation processor that cannot handle that type of exception. For example, the following is a simple annotation processor:
510.br
511.br
512\f3Source file \fP\f4AnnoProc.java\fP:
513.nf
514\f3
515.fl
516import java.util.*;
517.fl
518import javax.annotation.processing.*;
519.fl
520import javax.lang.model.*;
521.fl
522import javax.lang.model.element.*;
523.fl
524
525.fl
526@SupportedAnnotationTypes("NotAnno")
527.fl
528public class AnnoProc extends AbstractProcessor {
529.fl
530 public boolean process(Set<? extends TypeElement> elems, RoundEnvironment renv) {
531.fl
532 return true;
533.fl
534 }
535.fl
536
537.fl
538 public SourceVersion getSupportedSourceVersion() {
539.fl
540 return SourceVersion.latest();
541.fl
542 }
543.fl
544}
545.fl
546\fP
547.fi
548\f3Source file \fP\f4AnnosWithoutProcessors.java\fP\f3:\fP
549.nf
550\f3
551.fl
552@interface Anno { }
553.fl
554
555.fl
556@Anno
557.fl
558class AnnosWithoutProcessors { }
559.fl
560\fP
561.fi
562The following commands compile the annotation processor \f2AnnoProc\fP, then run this annotation processor against the source file \f2AnnosWithoutProcessors.java\fP:
563.nf
564\f3
565.fl
566% javac AnnoProc.java
567.fl
568% javac \-cp . \-Xlint:processing \-processor AnnoProc \-proc:only AnnosWithoutProcessors.java
569.fl
570\fP
571.fi
572When the compiler runs the annotation processor against the source file \f2AnnosWithoutProcessors.java\fP, it generates the following warning:
573.br
574.br
575\f2warning: [processing] No processor claimed any of these annotations: Anno\fP
576.br
577.br
578To resolve this issue, you can rename the annotation defined and used in the class \f2AnnosWithoutProcessors\fP from \f2Anno\fP to \f2NotAnno\fP.
579.TP 3
580rawtypes
581Warn about unchecked operations on raw types. The following statement generates a \f2rawtypes\fP warning:
582.nf
583\f3
584.fl
585void countElements(List l) { ... }
586.fl
587\fP
588.fi
589The following does not generate a \f2rawtypes\fP warning:
590.nf
591\f3
592.fl
593void countElements(List<?> l) { ... }
594.fl
595\fP
596.fi
597\f2List\fP is a raw type. However, \f2List<?>\fP is a unbounded wildcard parameterized type. Because \f2List\fP is a parameterized interface, you should always specify its type argument. In this example, the \f2List\fP formal argument is specified with a unbounded wildcard (\f2?\fP) as its formal type parameter, which means that the \f2countElements\fP method can accept any instantiation of the \f2List\fP interface.
598.TP 3
599serial
600Warn about missing \f2serialVersionUID\fP definitions on serializable classes. For example:
601.nf
602\f3
603.fl
604public class PersistentTime implements Serializable
605.fl
606{
607.fl
608 private Date time;
609.fl
610
611.fl
612 public PersistentTime() {
613.fl
614 time = Calendar.getInstance().getTime();
615.fl
616 }
617.fl
618
619.fl
620 public Date getTime() {
621.fl
622 return time;
623.fl
624 }
625.fl
626}
627.fl
628\fP
629.fi
630The compiler generates the following warning:
631.br
632.br
633\f2warning: [serial] serializable class PersistentTime has no definition of serialVersionUID\fP
634.br
635.br
636If a serializable class does not explicitly declare a field named \f2serialVersionUID\fP, then the serialization runtime will calculate a default \f2serialVersionUID\fP value for that class based on various aspects of the class, as described in the Java Object Serialization Specification. However, it is strongly recommended that all serializable classes explicitly declare \f2serialVersionUID\fP values because the default process of computing \f2serialVersionUID\fP vales is highly sensitive to class details that may vary depending on compiler implementations, and can thus result in unexpected \f2InvalidClassExceptions\fP during deserialization. Therefore, to guarantee a consistent \f2serialVersionUID\fP value across different Java compiler implementations, a serializable class must declare an explicit \f2serialVersionUID\fP value.
637.TP 3
638static
639Warn about issues relating to use of statics. For example:
640.nf
641\f3
642.fl
643class XLintStatic {
644.fl
645 static void m1() { }
646.fl
647 void m2() { this.m1(); }
648.fl
649}
650.fl
651\fP
652.fi
653The compiler generates the following warning:
654.nf
655\f3
656.fl
657warning: [static] static method should be qualified by type name, XLintStatic, instead of by an expression
658.fl
659\fP
660.fi
661To resolve this issue, you can call the static method \f2m1\fP as follows:
662.nf
663\f3
664.fl
665XLintStatic.m1();
666.fl
667\fP
668.fi
669Alternatively, you can remove the \f2static\fP keyword from the declaration of the method \f2m1\fP.
670.TP 3
671try
672Warn about issues relating to use of \f2try\fP blocks, including try\-with\-resources statements. For example, a warning is generated for the following statement because the resource \f2ac\fP declared in the \f2try\fP statement is not used:
673.nf
674\f3
675.fl
676try ( AutoCloseable ac = getResource() ) {
677.fl
678 // do nothing
679.fl
680}
681.fl
682\fP
683.fi
684.TP 3
685unchecked
686Give more detail for unchecked conversion warnings that are mandated by the Java Language Specification. For example:
687.nf
688\f3
689.fl
690 List l = new ArrayList<Number>();
691.fl
692 List<String> ls = l; // unchecked warning
693.fl
694\fP
695.fi
696During type erasure, the types \f2ArrayList<Number>\fP and \f2List<String>\fP become \f2ArrayList\fP and \f2List\fP, respectively.
697.br
698.br
699The variable \f2ls\fP has the parameterized type \f2List<String>\fP. When the \f2List\fP referenced by \f2l\fP is assigned to \f2ls\fP, the compiler generates an unchecked warning; the compiler is unable to determine at compile time, and moreover knows that the JVM will not be able to determine at runtime, if \f2l\fP refers to a \f2List<String>\fP type; it does not. Consequently, heap pollution occurs.
700.br
701.br
702In detail, a heap pollution situation occurs when the \f2List\fP object \f2l\fP, whose static type is \f2List<Number>\fP, is assigned to another \f2List\fP object, \f2ls\fP, that has a different static type, \f2List<String>\fP. However, the compiler still allows this assignment. It must allow this assignment to preserve backwards compatibility with versions of Java SE that do not support generics. Because of type erasure, \f2List<Number>\fP and \f2List<String>\fP both become \f2List\fP. Consequently, the compiler allows the assignment of the object \f2l\fP, which has a raw type of \f2List\fP, to the object \f2ls\fP.
703.TP 3
704varargs
705Warn about unsafe usages of variable arguments (varargs) methods, in particular, those that contain non\-reifiable arguments. For example:
706.nf
707\f3
708.fl
709public class ArrayBuilder {
710.fl
711 public static <T> void addToList (List<T> listArg, T... elements) {
712.fl
713 for (T x : elements) {
714.fl
715 listArg.add(x);
716.fl
717 }
718.fl
719 }
720.fl
721}
722.fl
723\fP
724.fi
725The compiler generates the following warning for the definition of the method \f2ArrayBuilder.addToList\fP:
726.nf
727\f3
728.fl
729warning: [varargs] Possible heap pollution from parameterized vararg type T
730.fl
731\fP
732.fi
733When the compiler encounters a varargs method, it translates the varargs formal parameter into an array. However, the Java programming language does not permit the creation of arrays of parameterized types. In the method \f2ArrayBuilder.addToList\fP, the compiler translates the varargs formal parameter \f2T... elements\fP to the formal parameter \f2T[] elements\fP, an array. However, because of type erasure, the compiler converts the varargs formal parameter to \f2Object[] elements\fP. Consequently, there is a possibility of heap pollution.
734.RE
735
736.LP
737.SH "COMMAND LINE ARGUMENT FILES"
738.LP
739.LP
740To shorten or simplify the javac command line, you can specify one or more files that themselves contain arguments to the \f2javac\fP command (except \f2\-J\fP options). This enables you to create javac commands of any length on any operating system.
741.LP
742.LP
743An argument file can include javac options and source filenames in any combination. The arguments within a file can be space\-separated or newline\-separated. If a filename contains embedded spaces, put the whole filename in double quotes.
744.LP
745.LP
746Filenames within an argument file are relative to the current directory, not the location of the argument file. Wildcards (*) are not allowed in these lists (such as for specifying \f2*.java\fP). Use of the '\f2@\fP' character to recursively interpret files is not supported. The \f2\-J\fP options are not supported because they are passed to the launcher, which does not support argument files.
747.LP
748.LP
749When executing javac, pass in the path and name of each argument file with the '\f2@\fP' leading character. When javac encounters an argument beginning with the character `\f2@\fP', it expands the contents of that file into the argument list.
750.LP
751.SS
752Example \- Single Arg File
753.LP
754.LP
755You could use a single argument file named "\f2argfile\fP" to hold all javac arguments:
756.LP
757.nf
758\f3
759.fl
760% \fP\f3javac @argfile\fP
761.fl
762.fi
763
764.LP
765.LP
766This argument file could contain the contents of both files shown in the next example.
767.LP
768.SS
769Example \- Two Arg Files
770.LP
771.LP
772You can create two argument files \-\- one for the javac options and the other for the source filenames: (Notice the following lists have no line\-continuation characters.)
773.LP
774.LP
775Create a file named "\f2options\fP" containing:
776.LP
777.nf
778\f3
779.fl
780 \-d classes
781.fl
782 \-g
783.fl
784 \-sourcepath /java/pubs/ws/1.3/src/share/classes
785.fl
786
787.fl
788\fP
789.fi
790
791.LP
792.LP
793Create a file named "\f2classes\fP" containing:
794.LP
795.nf
796\f3
797.fl
798 MyClass1.java
799.fl
800 MyClass2.java
801.fl
802 MyClass3.java
803.fl
804
805.fl
806\fP
807.fi
808
809.LP
810.LP
811You would then run \f3javac\fP with:
812.LP
813.nf
814\f3
815.fl
816 % \fP\f3javac @options @classes\fP
817.fl
818
819.fl
820.fi
821
822.LP
823.SS
824Example \- Arg Files with Paths
825.LP
826.LP
827The argument files can have paths, but any filenames inside the files are relative to the current working directory (not \f2path1\fP or \f2path2\fP):
828.LP
829.nf
830\f3
831.fl
832% \fP\f3javac @path1/options @path2/classes\fP
833.fl
834.fi
835
836.LP
837.SH "ANNOTATION PROCESSING"
838.LP
839.LP
840\f3javac\fP provides direct support for annotation processing, superseding the need for the separate annotation processing tool, \f3apt\fP.
841.LP
842.LP
843The API for annotation processors is defined in the \f2javax.annotation.processing\fP and \f2javax.lang.model\fP packages and subpackages.
844.LP
845.SS
846Overview of annotation processing
847.LP
848.LP
849Unless annotation processing is disabled with the \f3\-proc:none\fP option, the compiler searches for any annotation processors that are available. The search path can be specified with the \f3\-processorpath\fP option; if it is not given, the user class path is used. Processors are located by means of service provider\-configuration files named \f2META\-INF/services/javax.annotation.processing.Processor\fP on the search path. Such files should contain the names of any annotation processors to be used, listed one per line. Alternatively, processors can be specified explicitly, using the \f3\-processor\fP option.
850.LP
851.LP
852After scanning the source files and classes on the command line to determine what annotations are present, the compiler queries the processors to determine what annotations they process. When a match is found, the processor will be invoked. A processor may "claim" the annotations it processes, in which case no further attempt is made to find any processors for those annotations. Once all annotations have been claimed, the compiler does not look for additional processors.
853.LP
854.LP
855If any processors generate any new source files, another round of annotation processing will occur: any newly generated source files will be scanned, and the annotations processed as before. Any processors invoked on previous rounds will also be invoked on all subsequent rounds. This continues until no new source files are generated.
856.LP
857.LP
858After a round occurs where no new source files are generated, the annotation processors will be invoked one last time, to give them a chance to complete any work they may need to do. Finally, unless the \f3\-proc:only\fP option is used, the compiler will compile the original and all the generated source files.
859.LP
860.SS
861Implicitly loaded source files
862.LP
863.LP
864To compile a set of source files, the compiler may need to implicitly load additional source files. (See Searching For Types). Such files are currently not subject to annotation processing. By default, the compiler will give a warning if annotation processing has occurred and any implicitly loaded source files are compiled. See the \-implicit option for ways to suppress the warning.
865.LP
866.SH "SEARCHING FOR TYPES"
867.LP
868.LP
869When compiling a source file, the compiler often needs information about a type whose definition did not appear in the source files given on the command line. The compiler needs type information for every class or interface used, extended, or implemented in the source file. This includes classes and interfaces not explicitly mentioned in the source file but which provide information through inheritance.
870.LP
871.LP
872For example, when you subclass \f3java.applet.Applet\fP, you are also using \f3Applet's\fP ancestor classes: \f3java.awt.Panel\fP, \f3java.awt.Container\fP, \f3java.awt.Component\fP, and \f3java.lang.Object\fP.
873.LP
874.LP
875When the compiler needs type information, it looks for a source file or class file which defines the type. The compiler searches for class files first in the bootstrap and extension classes, then in the user class path (which by default is the current directory). The user class path is defined by setting the \f3CLASSPATH\fP environment variable or by using the \f3\-classpath\fP command line option. (For details, see Setting the Class Path).
876.LP
877.LP
878If you set the \-sourcepath option, the compiler searches the indicated path for source files; otherwise the compiler searches the user class path for both class files and source files.
879.LP
880.LP
881You can specify different bootstrap or extension classes with the \f3\-bootclasspath\fP and \f3\-extdirs\fP options; see Cross\-Compilation Options below.
882.LP
883.LP
884A successful type search may produce a class file, a source file, or both. If both are found, you can use the \-Xprefer option to instruct the compiler which to use. If \f3newer\fP is given, the compiler will use the newer of the two files. If \f3source\fP is given, it will use the source file. The default is \f3newer\fP.
885.LP
886.LP
887If a type search finds a source file for a required type, either by itself, or as a result of the setting for \f3\-Xprefer\fP, the compiler will read the source file to get the information it needs. In addition, it will by default compile the source file as well. You can use the \-implicit option to specify the behavior. If \f3none\fP is given, no class files will be generated for the source file. If \f3class\fP is given, class files will be generated for the source file.
888.LP
889.LP
890The compiler may not discover the need for some type information until after annotation processing is complete. If the type information is found in a source file and no \f3\-implicit\fP option is given, the compiler will give a warning that the file is being compiled without being subject to annotation processing. To disable the warning, either specify the file on the command line (so that it will be subject to annotation processing) or use the \f3\-implicit\fP option to specify whether or not class files should be generated for such source files.
891.LP
892.SH "PROGRAMMATIC INTERFACE"
893.LP
894.LP
895\f3javac\fP supports the new Java Compiler API defined by the classes and interfaces in the \f2javax.tools\fP package.
896.LP
897.SS
898Example
899.LP
900.LP
901To perform a compilation using arguments as you would give on the command line, you can use the following:
902.LP
903.nf
904\f3
905.fl
906JavaCompiler javac = ToolProvider.getSystemJavaCompiler();
907.fl
908int rc = javac.run(null, null, null, args);
909.fl
910\fP
911.fi
912
913.LP
914.LP
915This will write any diagnostics to the standard output stream, and return the exit code that \f3javac\fP would give when invoked from the command line.
916.LP
917.LP
918You can use other methods on the \f2javax.tools.JavaCompiler\fP interface to handle diagnostics, control where files are read from and written to, and so on.
919.LP
920.SS
921Old Interface
922.LP
923.LP
924\f3Note:\fP This API is retained for backwards compatibility only; all new code should use the Java Compiler API, described above.
925.LP
926.LP
927The \f2com.sun.tools.javac.Main\fP class provides two static methods to invoke the compiler from a program:
928.LP
929.nf
930\f3
931.fl
932public static int compile(String[] args);
933.fl
934public static int compile(String[] args, PrintWriter out);
935.fl
936\fP
937.fi
938
939.LP
940.LP
941The \f2args\fP parameter represents any of the command line arguments that would normally be passed to the javac program and are outlined in the above Synopsis section.
942.LP
943.LP
944The \f2out\fP parameter indicates where the compiler's diagnostic output is directed.
945.LP
946.LP
947The return value is equivalent to the exit value from \f3javac\fP.
948.LP
949.LP
950Note that all \f3other\fP classes and methods found in a package whose name starts with \f2com.sun.tools.javac\fP (informally known as sub\-packages of \f2com.sun.tools.javac\fP) are strictly internal and subject to change at any time.
951.LP
952.SH "EXAMPLES"
953.LP
954.SS
955Compiling a Simple Program
956.LP
957.LP
958One source file, \f2Hello.java\fP, defines a class called \f3greetings.Hello\fP. The \f2greetings\fP directory is the package directory both for the source file and the class file and is off the current directory. This allows us to use the default user class path. It also makes it unnecessary to specify a separate destination directory with \f3\-d\fP.
959.LP
960.nf
961\f3
962.fl
963% \fP\f3ls\fP
964.fl
965greetings/
966.fl
967% \f3ls greetings\fP
968.fl
969Hello.java
970.fl
971% \f3cat greetings/Hello.java\fP
972.fl
973package greetings;
974.fl
975
976.fl
977public class Hello {
978.fl
979 public static void main(String[] args) {
980.fl
981 for (int i=0; i < args.length; i++) {
982.fl
983 System.out.println("Hello " + args[i]);
984.fl
985 }
986.fl
987 }
988.fl
989}
990.fl
991% \f3javac greetings/Hello.java\fP
992.fl
993% \f3ls greetings\fP
994.fl
995Hello.class Hello.java
996.fl
997% \f3java greetings.Hello World Universe Everyone\fP
998.fl
999Hello World
1000.fl
1001Hello Universe
1002.fl
1003Hello Everyone
1004.fl
1005.fi
1006
1007.LP
1008.SS
1009Compiling Multiple Source Files
1010.LP
1011.LP
1012This example compiles all the source files in the package \f2greetings\fP.
1013.LP
1014.nf
1015\f3
1016.fl
1017% \fP\f3ls\fP
1018.fl
1019greetings/
1020.fl
1021% \f3ls greetings\fP
1022.fl
1023Aloha.java GutenTag.java Hello.java Hi.java
1024.fl
1025% \f3javac greetings/*.java\fP
1026.fl
1027% \f3ls greetings\fP
1028.fl
1029Aloha.class GutenTag.class Hello.class Hi.class
1030.fl
1031Aloha.java GutenTag.java Hello.java Hi.java
1032.fl
1033.fi
1034
1035.LP
1036.SS
1037Specifying a User Class Path
1038.LP
1039.LP
1040Having changed one of the source files in the previous example, we recompile it:
1041.LP
1042.nf
1043\f3
1044.fl
1045% \fP\f3pwd\fP
1046.fl
1047/examples
1048.fl
1049% \f3javac greetings/Hi.java\fP
1050.fl
1051.fi
1052
1053.LP
1054.LP
1055Since \f2greetings.Hi\fP refers to other classes in the \f2greetings\fP package, the compiler needs to find these other classes. The example above works, because our default user class path happens to be the directory containing the package directory. But suppose we want to recompile this file and not worry about which directory we're in? Then we need to add \f2/examples\fP to the user class path. We can do this by setting \f3CLASSPATH\fP, but here we'll use the \f3\-classpath\fP option.
1056.LP
1057.nf
1058\f3
1059.fl
1060% \fP\f3javac \-classpath /examples /examples/greetings/Hi.java\fP
1061.fl
1062.fi
1063
1064.LP
1065.LP
1066If we change \f2greetings.Hi\fP again, to use a banner utility, that utility also needs to be accessible through the user class path.
1067.LP
1068.nf
1069\f3
1070.fl
1071% \fP\f3javac \-classpath /examples:/lib/Banners.jar \\
1072.fl
1073 /examples/greetings/Hi.java\fP
1074.fl
1075.fi
1076
1077.LP
1078.LP
1079To execute a class in \f2greetings\fP, we need access both to \f2greetings\fP and to the classes it uses.
1080.LP
1081.nf
1082\f3
1083.fl
1084% \fP\f3java \-classpath /examples:/lib/Banners.jar greetings.Hi\fP
1085.fl
1086.fi
1087
1088.LP
1089.SS
1090Separating Source Files and Class Files
1091.LP
1092.LP
1093It often makes sense to keep source files and class files in separate directories, especially on large projects. We use \f3\-d\fP to indicate the separate class file destination. Since the source files are not in the user class path, we use \f3\-sourcepath\fP to help the compiler find them.
1094.LP
1095.nf
1096\f3
1097.fl
1098% \fP\f3ls\fP
1099.fl
1100classes/ lib/ src/
1101.fl
1102% \f3ls src\fP
1103.fl
1104farewells/
1105.fl
1106% \f3ls src/farewells\fP
1107.fl
1108Base.java GoodBye.java
1109.fl
1110% \f3ls lib\fP
1111.fl
1112Banners.jar
1113.fl
1114% \f3ls classes\fP
1115.fl
1116% \f3javac \-sourcepath src \-classpath classes:lib/Banners.jar \\
1117.fl
1118 src/farewells/GoodBye.java \-d classes\fP
1119.fl
1120% \f3ls classes\fP
1121.fl
1122farewells/
1123.fl
1124% \f3ls classes/farewells\fP
1125.fl
1126Base.class GoodBye.class
1127.fl
1128.fi
1129
1130.LP
1131.LP
1132\f3Note:\fP The compiler compiled \f2src/farewells/Base.java\fP, even though we didn't specify it on the command line. To trace automatic compiles, use the \f3\-verbose\fP option.
1133.LP
1134.SS
1135Cross\-Compilation Example
1136.LP
1137.LP
1138Here we use \f3javac\fP to compile code that will run on a 1.6 VM.
1139.LP
1140.nf
1141\f3
1142.fl
1143% \fP\f3javac \-source 1.6 \-target 1.6 \-bootclasspath jdk1.6.0/lib/rt.jar \\
1144.fl
1145 \-extdirs "" OldCode.java\fP
1146.fl
1147.fi
1148
1149.LP
1150.LP
1151The \f2\-source 1.6\fP option specifies that version 1.6 (or 6) of the Java programming language be used to compile \f2OldCode.java\fP. The option \f3\-target 1.6\fP option ensures that the generated class files will be compatible with 1.6 VMs. Note that in most cases, the value of the \f3\-target\fP option is the value of the \f3\-source\fP option; in this example, you can omit the \f3\-target\fP option.
1152.LP
1153.LP
1154You must specify the \f3\-bootclasspath\fP option to specify the correct version of the bootstrap classes (the \f2rt.jar\fP library). If not, the compiler generates a warning:
1155.LP
1156.nf
1157\f3
1158.fl
1159% \fP\f3javac \-source 1.6 OldCode.java\fP
1160.fl
1161warning: [options] bootstrap class path not set in conjunction with \-source 1.6
1162.fl
1163.fi
1164
1165.LP
1166.LP
1167If you do not specify the correct version of bootstrap classes, the compiler will use the old language rules (in this example, it will use version 1.6 of the Java programming language) combined with the new bootstrap classes, which can result in class files that do not work on the older platform (in this case, Java SE 6) because reference to non\-existent methods can get included.
1168.LP
1169.SH "SEE ALSO"
1170.LP
1171.RS 3
1172.TP 2
1173o
1174.na
1175\f2The javac Guide\fP @
1176.fi
1177http://download.oracle.com/javase/7/docs/technotes/guides/javac/index.html
1178.TP 2
1179o
1180java(1) \- the Java Application Launcher
1181.TP 2
1182o
1183jdb(1) \- Java Application Debugger
1184.TP 2
1185o
1186javah(1) \- C Header and Stub File Generator
1187.TP 2
1188o
1189javap(1) \- Class File Disassembler
1190.TP 2
1191o
1192javadoc(1) \- API Documentation Generator
1193.TP 2
1194o
1195jar(1) \- JAR Archive Tool
1196.TP 2
1197o
1198.na
1199\f2The Java Extensions Framework\fP @
1200.fi
1201http://download.oracle.com/javase/7/docs/technotes/guides/extensions/index.html
1202.RE
1203
1204.LP
1205