annotate src/linux/doc/man/javac.1 @ 0:37a05a11f281

Initial load
author duke
date Sat, 01 Dec 2007 00:00:00 +0000
parents
children b056c42ea5b4
rev   line source
duke@0 1 .'" t
duke@0 2 ."
duke@0 3 ." Copyright 2000-2006 Sun Microsystems, Inc. All Rights Reserved.
duke@0 4 ." DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
duke@0 5 ."
duke@0 6 ." This code is free software; you can redistribute it and/or modify it
duke@0 7 ." under the terms of the GNU General Public License version 2 only, as
duke@0 8 ." published by the Free Software Foundation.
duke@0 9 ."
duke@0 10 ." This code is distributed in the hope that it will be useful, but WITHOUT
duke@0 11 ." ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
duke@0 12 ." FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
duke@0 13 ." version 2 for more details (a copy is included in the LICENSE file that
duke@0 14 ." accompanied this code).
duke@0 15 ."
duke@0 16 ." You should have received a copy of the GNU General Public License version
duke@0 17 ." 2 along with this work; if not, write to the Free Software Foundation,
duke@0 18 ." Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
duke@0 19 ."
duke@0 20 ." Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
duke@0 21 ." CA 95054 USA or visit www.sun.com if you need additional information or
duke@0 22 ." have any questions.
duke@0 23 ."
duke@0 24 ." `
duke@0 25 .TH javac 1 "07 Aug 2006"
duke@0 26 ." Generated by html2man
duke@0 27
duke@0 28 .LP
duke@0 29 .SH NAME
duke@0 30 javac \- Java programming language compiler
duke@0 31 .LP
duke@0 32 .SH "SYNOPSIS"
duke@0 33 .LP
duke@0 34
duke@0 35 .LP
duke@0 36 .nf
duke@0 37 \f3
duke@0 38 .fl
duke@0 39 \fP\f3javac\fP [ options ] [ sourcefiles ] [ @argfiles ]
duke@0 40 .fl
duke@0 41 .fi
duke@0 42
duke@0 43 .LP
duke@0 44 .LP
duke@0 45 Arguments may be in any order.
duke@0 46 .LP
duke@0 47 .RS 3
duke@0 48
duke@0 49 .LP
duke@0 50 .RS 3
duke@0 51 .TP 3
duke@0 52 options
duke@0 53 Command\-line options.
duke@0 54 .TP 3
duke@0 55 sourcefiles
duke@0 56 One or more source files to be compiled (such as MyClass.java).
duke@0 57 .TP 3
duke@0 58 @argfiles
duke@0 59 One or more files that lists options and source files. The \f2\-J\fP options are not allowed in these files.
duke@0 60 .RE
duke@0 61
duke@0 62 .LP
duke@0 63 .RE
duke@0 64 .SH "DESCRIPTION"
duke@0 65 .LP
duke@0 66
duke@0 67 .LP
duke@0 68 .LP
duke@0 69 The \f3javac\fP tool reads class and interface definitions, written in the Java programming language, and compiles them into bytecode class files.
duke@0 70 .LP
duke@0 71 .LP
duke@0 72 There are two ways to pass source code filenames to \f3javac\fP:
duke@0 73 .LP
duke@0 74 .RS 3
duke@0 75 .TP 2
duke@0 76 o
duke@0 77 For a small number of source files, simply list the file names on the command line.
duke@0 78 .TP 2
duke@0 79 o
duke@0 80 For 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.
duke@0 81 .RE
duke@0 82
duke@0 83 .LP
duke@0 84 .LP
duke@0 85 Source 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.
duke@0 86 .LP
duke@0 87 .LP
duke@0 88 Inner class definitions produce additional class files. These class files have names combining the inner and outer class names, such as \f2MyClass$MyInnerClass.class\fP.
duke@0 89 .LP
duke@0 90 .LP
duke@0 91 You 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.
duke@0 92 .LP
duke@0 93 .LP
duke@0 94 By 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).
duke@0 95 .LP
duke@0 96 .SH "SEARCHING FOR TYPES"
duke@0 97 .LP
duke@0 98
duke@0 99 .LP
duke@0 100 .LP
duke@0 101 When 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.
duke@0 102 .LP
duke@0 103 .LP
duke@0 104 For 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.
duke@0 105 .LP
duke@0 106 .LP
duke@0 107 When 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).
duke@0 108 .LP
duke@0 109 .LP
duke@0 110 If 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.
duke@0 111 .LP
duke@0 112 .LP
duke@0 113 You can specify different bootstrap or extension classes with the \f3\-bootclasspath\fP and \f3\-extdirs\fP options; see Cross\-Compilation Options below.
duke@0 114 .LP
duke@0 115 .LP
duke@0 116 A successful type search may produce a class file, a source file, or both. Here is how \f3javac\fP handles each situation:
duke@0 117 .LP
duke@0 118 .RS 3
duke@0 119 .TP 2
duke@0 120 o
duke@0 121 \f2Search produces a class file but no source file:\fP \f3javac\fP uses the class file.
duke@0 122 .TP 2
duke@0 123 o
duke@0 124 \f2Search produces a source file but no class file:\fP \f3javac\fP compiles the source file and uses the resulting class file.
duke@0 125 .TP 2
duke@0 126 o
duke@0 127 \f2Search produces both a source file and a class file:\fP \f3javac\fP determines whether the class file is out of date. If the class file is out of date, \f3javac\fP recompiles the source file and uses the updated class file. Otherwise, \f3javac\fP just uses the class file.
duke@0 128 .LP
duke@0 129 \f3javac\fP considers a class file out of date only if it is older than the source file.
duke@0 130 .RE
duke@0 131
duke@0 132 .LP
duke@0 133 .LP
duke@0 134 \f3Note:\fP \ \f3javac\fP can silently compile source files not mentioned on the command line. Use the \f3\-verbose\fP option to trace automatic compilation.
duke@0 135 .LP
duke@0 136 .SH "OPTIONS"
duke@0 137 .LP
duke@0 138
duke@0 139 .LP
duke@0 140 .LP
duke@0 141 The 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.
duke@0 142 .LP
duke@0 143 .SS
duke@0 144 Standard Options
duke@0 145 .LP
duke@0 146 .RS 3
duke@0 147
duke@0 148 .LP
duke@0 149 .RS 3
duke@0 150 .TP 3
duke@0 151 \-classpath classpath
duke@0 152 Set the user class path, overriding the user class path in the \f3CLASSPATH\fP environment variable. If neither \f3CLASSPATH\fP or \f3\-classpath\fP is specified, the user class path consists of the current directory. See Setting the Class Path for more details.
duke@0 153 .LP
duke@0 154 If the \f3\-sourcepath\fP option is not specified, the user class path is searched for both source files and class files.
duke@0 155 .LP
duke@0 156 As a special convenience, a class path element containing a basename of \f2*\fP is considered equivalent to specifying a list of all the files in the directory with the extension \f2.jar\fP or \f2.JAR\fP (a java program cannot tell the difference between the two invocations).
duke@0 157 .br
duke@0 158 .br
duke@0 159 For example, if directory \f2foo\fP contains \f2a.jar\fP and \f2b.JAR\fP, then the class path element \f2foo/*\fP is expanded to a \f2A.jar:b.JAR\fP, except that the order of jar files is unspecified. All jar files in the specified directory, even hidden ones, are included in the list. A classpath entry consisting simply of \f2*\fP expands to a list of all the jar files in the current directory. The \f2CLASSPATH\fP environment variable, where defined, will be similarly expanded. Any classpath wildcard expansion occurs before the Java virtual machine is started \-\- no Java program will ever see unexpanded wildcards except by querying the environment. For example; by invoking \f2System.getenv("CLASSPATH")\fP.
duke@0 160 .TP 3
duke@0 161 \-Djava.ext.dirs=directories
duke@0 162 Override the location of installed extensions.
duke@0 163 .TP 3
duke@0 164 \-Djava.endorsed.dirs=directories
duke@0 165 Override the location of endorsed standards path.
duke@0 166 .TP 3
duke@0 167 \-d directory
duke@0 168 Set the destination directory for class files. The destination directory must already exist; javac will not create the destination directory. 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.
duke@0 169 .LP
duke@0 170 If \f3\-d\fP is not specified, \f3javac\fP puts the class file in the same directory as the source file.
duke@0 171 .LP
duke@0 172 \f3Note:\fP \ The directory specified by \f3\-d\fP is not automatically added to your user class path.
duke@0 173 .TP 3
duke@0 174 \-deprecation
duke@0 175 Show a description of each use or override of a deprecated member or class. Without \f3\-deprecation\fP, \f3javac\fP shows the names of source files that use or override deprecated members or classes. \f3\-deprecation\fP is shorthand for \f3\-Xlint:deprecation\fP.
duke@0 176 .TP 3
duke@0 177 \-encoding encoding
duke@0 178 Set 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.
duke@0 179 .TP 3
duke@0 180 \-g
duke@0 181 Generate all debugging information, including local variables. By default, only line number and source file information is generated.
duke@0 182 .TP 3
duke@0 183 \-g:none
duke@0 184 Do not generate any debugging information.
duke@0 185 .TP 3
duke@0 186 \-g:{keyword list}
duke@0 187 Generate only some kinds of debugging information, specified by a comma separated list of keywords. Valid keywords are:
duke@0 188 .RS 3
duke@0 189 .TP 3
duke@0 190 source
duke@0 191 Source file debugging information
duke@0 192 .TP 3
duke@0 193 lines
duke@0 194 Line number debugging information
duke@0 195 .TP 3
duke@0 196 vars
duke@0 197 Local variable debugging information
duke@0 198 .RE
duke@0 199 .TP 3
duke@0 200 \-help
duke@0 201 Print a synopsis of standard options.
duke@0 202 .TP 3
duke@0 203 \-nowarn
duke@0 204 Disable warning messages. This has the same meaning as \f3\-Xlint:none\fP.
duke@0 205 .LP
duke@0 206 .TP 3
duke@0 207 \-source release
duke@0 208 Specifies the version of source code accepted. The following values for \f2release\fP are allowed:
duke@0 209 .RS 3
duke@0 210 .TP 3
duke@0 211 1.3
duke@0 212 The compiler does \f2not\fP support assertions, generics, or other language features introduced after JDK 1.3.
duke@0 213 .TP 3
duke@0 214 1.4
duke@0 215 The compiler accepts code containing assertions, which were introduced in JDK 1.4.
duke@0 216 .TP 3
duke@0 217 1.5
duke@0 218 The compiler accepts code containing generics and other language features introduced in JDK 5. This is the default.
duke@0 219 .TP 3
duke@0 220 5
duke@0 221 Synonym for 1.5
duke@0 222 .RE
duke@0 223 \f3Note:\fP No language changes were introduced in JDK 6, so the values \f31.6\fP and \f36\fP are not valid.
duke@0 224 .TP 3
duke@0 225 \-sourcepath sourcepath
duke@0 226 Specify 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.
duke@0 227 .LP
duke@0 228 \f3Note:\fP \ Classes found through the classpath are subject to automatic recompilation if their sources are found.
duke@0 229 .TP 3
duke@0 230 \-verbose
duke@0 231 Verbose output. This includes information about each class loaded and each source file compiled.
duke@0 232 .TP 3
duke@0 233 \-X
duke@0 234 Display information about non\-standard options and exit.
duke@0 235 .RE
duke@0 236
duke@0 237 .LP
duke@0 238 .RE
duke@0 239 .SS
duke@0 240 Cross\-Compilation Options
duke@0 241 .LP
duke@0 242 .RS 3
duke@0 243
duke@0 244 .LP
duke@0 245 .LP
duke@0 246 By 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.
duke@0 247 .LP
duke@0 248 .RS 3
duke@0 249 .TP 3
duke@0 250 \-target version
duke@0 251 Generate 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) and \f31.6\fP (also \f36\fP).
duke@0 252 .LP
duke@0 253 The default for \f3\-target\fP depends on the value of \f3\-source\fP:
duke@0 254 .RS 3
duke@0 255 .TP 2
duke@0 256 o
duke@0 257 If \-source is \f3not specified\fP, the value of \-target is \f31.6\fP
duke@0 258 .TP 2
duke@0 259 o
duke@0 260 If \-source is \f31.3\fP, the value of \-target is \f31.1\fP
duke@0 261 .TP 2
duke@0 262 o
duke@0 263 For \f3all other values\fP of \-source, the value of \-target is the value of \f3\-source\fP.
duke@0 264 .RE
duke@0 265 .TP 3
duke@0 266 \-bootclasspath bootclasspath
duke@0 267 Cross\-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.
duke@0 268 .TP 3
duke@0 269 \-extdirs directories
duke@0 270 Cross\-compile against the specified extension directories. \f2Directories\fP is a colon\-separated list of directories. Each JAR archive in the specified directories is searched for class files.
duke@0 271 .RE
duke@0 272
duke@0 273 .LP
duke@0 274 .RE
duke@0 275 .SS
duke@0 276 Non\-Standard Options
duke@0 277 .LP
duke@0 278 .RS 3
duke@0 279
duke@0 280 .LP
duke@0 281 .RS 3
duke@0 282 .TP 3
duke@0 283 \-Xbootclasspath/p:path
duke@0 284 Prepend to the bootstrap class path.
duke@0 285 .TP 3
duke@0 286 \-Xbootclasspath/a:path
duke@0 287 Append to the bootstrap class path.
duke@0 288 .TP 3
duke@0 289 \-Xbootclasspath/:path
duke@0 290 Override location of bootstrap class files.
duke@0 291 .TP 3
duke@0 292 \-Xlint
duke@0 293 Enable all recommended warnings. In this release, all available warnings are recommended.
duke@0 294 .TP 3
duke@0 295 \-Xlint:none
duke@0 296 Disable all warnings not mandated by the Java Language Specification.
duke@0 297 .TP 3
duke@0 298 \-Xlint:\-xxx
duke@0 299 Disable warning \f3xxx\fP, where \f3xxx\fP is one of the warning names supported for \f3\-Xlint:xxx\fP, below
duke@0 300 .TP 3
duke@0 301 \-Xlint:unchecked
duke@0 302 Give more detail for unchecked conversion warnings that are mandated by the Java Language Specification.
duke@0 303 .TP 3
duke@0 304 \-Xlint:path
duke@0 305 Warn about nonexistent path (classpath, sourcepath, etc) directories.
duke@0 306 .TP 3
duke@0 307 \-Xlint:serial
duke@0 308 Warn about missing \f2serialVersionUID\fP definitions on serializable classes.
duke@0 309 .TP 3
duke@0 310 \-Xlint:finally
duke@0 311 Warn about \f2finally\fP clauses that cannot complete normally.
duke@0 312 .TP 3
duke@0 313 \-Xlint:fallthrough
duke@0 314 Check \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 contain a \f2break\fP statement:
duke@0 315 .RS 3
duke@0 316
duke@0 317 .LP
duke@0 318 .nf
duke@0 319 \f3
duke@0 320 .fl
duke@0 321 switch (x) {
duke@0 322 .fl
duke@0 323 case 1:
duke@0 324 .fl
duke@0 325 System.out.println("1");
duke@0 326 .fl
duke@0 327 // No break; statement here.
duke@0 328 .fl
duke@0 329 case 2:
duke@0 330 .fl
duke@0 331 System.out.println("2");
duke@0 332 .fl
duke@0 333 }
duke@0 334 .fl
duke@0 335 \fP
duke@0 336 .fi
duke@0 337 .RE
duke@0 338 If 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.
duke@0 339 .TP 3
duke@0 340 \-Xmaxerrors number
duke@0 341 Set the maximum number of errors to print.
duke@0 342 .TP 3
duke@0 343 \-Xmaxwarns number
duke@0 344 Set the maximum number of warnings to print.
duke@0 345 .TP 3
duke@0 346 \-Xstdout filename
duke@0 347 Send compiler messages to the named file. By default, compiler messages go to \f2System.err\fP.
duke@0 348 .RE
duke@0 349
duke@0 350 .LP
duke@0 351 .RE
duke@0 352 .SS
duke@0 353 The \-J Option
duke@0 354 .LP
duke@0 355 .RS 3
duke@0 356
duke@0 357 .LP
duke@0 358 .RS 3
duke@0 359 .TP 3
duke@0 360 \-Joption
duke@0 361 Pass \f2option\fP to the \f3java\fP launcher called by \f3javac\fP. For example, \f3\-J\-Xms48m\fP sets the startup memory to 48 megabytes. Although it does not begin with \f3\-X\fP, it is not a `standard option' of \f3javac\fP. It is a common convention for \f3\-J\fP to pass options to the underlying VM executing applications written in Java.
duke@0 362 .LP
duke@0 363 \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.
duke@0 364 .RE
duke@0 365
duke@0 366 .LP
duke@0 367 .RE
duke@0 368 .SH "COMMAND LINE ARGUMENT FILES"
duke@0 369 .LP
duke@0 370
duke@0 371 .LP
duke@0 372 .LP
duke@0 373 To 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.
duke@0 374 .LP
duke@0 375 .LP
duke@0 376 An 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.
duke@0 377 .LP
duke@0 378 .LP
duke@0 379 Filenames 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.
duke@0 380 .LP
duke@0 381 .LP
duke@0 382 When 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.
duke@0 383 .LP
duke@0 384 .SS
duke@0 385 Example \- Single Arg File
duke@0 386 .LP
duke@0 387 .LP
duke@0 388 You could use a single argument file named "\f2argfile\fP" to hold all javac arguments:
duke@0 389 .LP
duke@0 390 .nf
duke@0 391 \f3
duke@0 392 .fl
duke@0 393 C:> \fP\f3javac @argfile\fP
duke@0 394 .fl
duke@0 395 .fi
duke@0 396
duke@0 397 .LP
duke@0 398 .LP
duke@0 399 This argument file could contain the contents of both files shown in the next example.
duke@0 400 .LP
duke@0 401 .SS
duke@0 402 Example \- Two Arg Files
duke@0 403 .LP
duke@0 404 .LP
duke@0 405 You 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.)
duke@0 406 .LP
duke@0 407 .LP
duke@0 408 Create a file named "\f2options\fP" containing:
duke@0 409 .LP
duke@0 410 .nf
duke@0 411 \f3
duke@0 412 .fl
duke@0 413 \-d classes
duke@0 414 .fl
duke@0 415 \-g
duke@0 416 .fl
duke@0 417 \-sourcepath \\java\\pubs\\ws\\1.3\\src\\share\\classes
duke@0 418 .fl
duke@0 419 \fP
duke@0 420 .fi
duke@0 421
duke@0 422 .LP
duke@0 423 .LP
duke@0 424 Create a file named "\f2classes\fP" containing:
duke@0 425 .LP
duke@0 426 .nf
duke@0 427 \f3
duke@0 428 .fl
duke@0 429 MyClass1.java
duke@0 430 .fl
duke@0 431 MyClass2.java
duke@0 432 .fl
duke@0 433 MyClass3.java
duke@0 434 .fl
duke@0 435 \fP
duke@0 436 .fi
duke@0 437
duke@0 438 .LP
duke@0 439 .LP
duke@0 440 You would then run javac with:
duke@0 441 .LP
duke@0 442 .nf
duke@0 443 \f3
duke@0 444 .fl
duke@0 445 % \fP\f3javac @options @classes\fP
duke@0 446 .fl
duke@0 447 .fi
duke@0 448
duke@0 449 .LP
duke@0 450 .SS
duke@0 451 Example \- Arg Files with Paths
duke@0 452 .LP
duke@0 453 .LP
duke@0 454 The argument files can have paths, but any filenames inside the files are relative to the current working directory (not \f2path1\fP or \f2path2\fP):
duke@0 455 .LP
duke@0 456 .nf
duke@0 457 \f3
duke@0 458 .fl
duke@0 459 % \fP\f3javac @path1/options @path2/classes\fP
duke@0 460 .fl
duke@0 461 .fi
duke@0 462
duke@0 463 .LP
duke@0 464 .SH "PROGRAMMATIC INTERFACE"
duke@0 465 .LP
duke@0 466 .LP
duke@0 467 The \f2com.sun.tools.javac.Main\fP class provides two static methods to invoke the compiler from a program:
duke@0 468 .LP
duke@0 469 .nf
duke@0 470 \f3
duke@0 471 .fl
duke@0 472 public static int compile(String[] args);
duke@0 473 .fl
duke@0 474 public static int compile(String[] args, PrintWriter out);
duke@0 475 .fl
duke@0 476 \fP
duke@0 477 .fi
duke@0 478
duke@0 479 .LP
duke@0 480 .LP
duke@0 481 The \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.
duke@0 482 .LP
duke@0 483 .LP
duke@0 484 The \f2out\fP parameter indicates where the compiler's diagnostic output is directed.
duke@0 485 .LP
duke@0 486 .LP
duke@0 487 The return value is equivalent to the exit value from javac.
duke@0 488 .LP
duke@0 489 .LP
duke@0 490 Note 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.
duke@0 491 .LP
duke@0 492 .SH "EXAMPLES"
duke@0 493 .LP
duke@0 494 .SS
duke@0 495 Compiling a Simple Program
duke@0 496 .LP
duke@0 497 .RS 3
duke@0 498
duke@0 499 .LP
duke@0 500 .LP
duke@0 501 One 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.
duke@0 502 .LP
duke@0 503 .RS 3
duke@0 504
duke@0 505 .LP
duke@0 506 .nf
duke@0 507 \f3
duke@0 508 .fl
duke@0 509 % \fP\f3ls\fP
duke@0 510 .fl
duke@0 511 greetings/
duke@0 512 .fl
duke@0 513 % \f3ls greetings\fP
duke@0 514 .fl
duke@0 515 Hello.java
duke@0 516 .fl
duke@0 517 % \f3cat greetings/Hello.java\fP
duke@0 518 .fl
duke@0 519 package greetings;
duke@0 520 .fl
duke@0 521
duke@0 522 .fl
duke@0 523 public class Hello {
duke@0 524 .fl
duke@0 525 public static void main(String[] args) {
duke@0 526 .fl
duke@0 527 for (int i=0; i < args.length; i++) {
duke@0 528 .fl
duke@0 529 System.out.println("Hello " + args[i]);
duke@0 530 .fl
duke@0 531 }
duke@0 532 .fl
duke@0 533 }
duke@0 534 .fl
duke@0 535 }
duke@0 536 .fl
duke@0 537 % \f3javac greetings/Hello.java\fP
duke@0 538 .fl
duke@0 539 % \f3ls greetings\fP
duke@0 540 .fl
duke@0 541 Hello.class Hello.java
duke@0 542 .fl
duke@0 543 % \f3java greetings.Hello World Universe Everyone\fP
duke@0 544 .fl
duke@0 545 Hello World
duke@0 546 .fl
duke@0 547 Hello Universe
duke@0 548 .fl
duke@0 549 Hello Everyone
duke@0 550 .fl
duke@0 551 .fi
duke@0 552 .RE
duke@0 553
duke@0 554 .LP
duke@0 555 .RE
duke@0 556 .SS
duke@0 557 Compiling Multiple Source Files
duke@0 558 .LP
duke@0 559 .RS 3
duke@0 560
duke@0 561 .LP
duke@0 562 .LP
duke@0 563 This example compiles all the source files in the package \f2greetings\fP.
duke@0 564 .LP
duke@0 565 .RS 3
duke@0 566
duke@0 567 .LP
duke@0 568 .nf
duke@0 569 \f3
duke@0 570 .fl
duke@0 571 % \fP\f3ls\fP
duke@0 572 .fl
duke@0 573 greetings/
duke@0 574 .fl
duke@0 575 % \f3ls greetings\fP
duke@0 576 .fl
duke@0 577 Aloha.java GutenTag.java Hello.java Hi.java
duke@0 578 .fl
duke@0 579 % \f3javac greetings/*.java\fP
duke@0 580 .fl
duke@0 581 % \f3ls greetings\fP
duke@0 582 .fl
duke@0 583 Aloha.class GutenTag.class Hello.class Hi.class
duke@0 584 .fl
duke@0 585 Aloha.java GutenTag.java Hello.java Hi.java
duke@0 586 .fl
duke@0 587
duke@0 588 .fl
duke@0 589 .fi
duke@0 590 .RE
duke@0 591
duke@0 592 .LP
duke@0 593 .RE
duke@0 594 .SS
duke@0 595 Specifying a User Class Path
duke@0 596 .LP
duke@0 597 .RS 3
duke@0 598
duke@0 599 .LP
duke@0 600 .LP
duke@0 601 Having changed one of the source files in the previous example, we recompile it:
duke@0 602 .LP
duke@0 603 .RS 3
duke@0 604
duke@0 605 .LP
duke@0 606 .nf
duke@0 607 \f3
duke@0 608 .fl
duke@0 609 % \fP\f3pwd\fP
duke@0 610 .fl
duke@0 611 /examples
duke@0 612 .fl
duke@0 613 % \f3javac greetings/Hi.java\fP
duke@0 614 .fl
duke@0 615 .fi
duke@0 616 .RE
duke@0 617
duke@0 618 .LP
duke@0 619 .LP
duke@0 620 Since \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.
duke@0 621 .LP
duke@0 622 .RS 3
duke@0 623
duke@0 624 .LP
duke@0 625 .nf
duke@0 626 \f3
duke@0 627 .fl
duke@0 628 % \fP\f3javac \-classpath /examples /examples/greetings/Hi.java\fP
duke@0 629 .fl
duke@0 630 .fi
duke@0 631 .RE
duke@0 632
duke@0 633 .LP
duke@0 634 .LP
duke@0 635 If we change \f2greetings.Hi\fP again, to use a banner utility, that utility also needs to be accessible through the user class path.
duke@0 636 .LP
duke@0 637 .RS 3
duke@0 638
duke@0 639 .LP
duke@0 640 .nf
duke@0 641 \f3
duke@0 642 .fl
duke@0 643 % \fP\f3javac \-classpath /examples:/lib/Banners.jar \\
duke@0 644 .fl
duke@0 645 /examples/greetings/Hi.java\fP
duke@0 646 .fl
duke@0 647 .fi
duke@0 648 .RE
duke@0 649
duke@0 650 .LP
duke@0 651 .LP
duke@0 652 To execute a class in \f2greetings\fP, we need access both to \f2greetings\fP and to the classes it uses.
duke@0 653 .LP
duke@0 654 .RS 3
duke@0 655
duke@0 656 .LP
duke@0 657 .nf
duke@0 658 \f3
duke@0 659 .fl
duke@0 660 % \fP\f3java \-classpath /examples:/lib/Banners.jar greetings.Hi\fP
duke@0 661 .fl
duke@0 662 .fi
duke@0 663 .RE
duke@0 664
duke@0 665 .LP
duke@0 666 .RE
duke@0 667 .SS
duke@0 668 Separating Source Files and Class Files
duke@0 669 .LP
duke@0 670 .RS 3
duke@0 671
duke@0 672 .LP
duke@0 673 .LP
duke@0 674 It 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.
duke@0 675 .LP
duke@0 676 .RS 3
duke@0 677
duke@0 678 .LP
duke@0 679 .nf
duke@0 680 \f3
duke@0 681 .fl
duke@0 682 % \fP\f3ls\fP
duke@0 683 .fl
duke@0 684 classes/ lib/ src/
duke@0 685 .fl
duke@0 686 % \f3ls src\fP
duke@0 687 .fl
duke@0 688 farewells/
duke@0 689 .fl
duke@0 690 % \f3ls src/farewells\fP
duke@0 691 .fl
duke@0 692 Base.java GoodBye.java
duke@0 693 .fl
duke@0 694 % \f3ls lib\fP
duke@0 695 .fl
duke@0 696 Banners.jar
duke@0 697 .fl
duke@0 698 % \f3ls classes\fP
duke@0 699 .fl
duke@0 700 % \f3javac \-sourcepath src \-classpath classes:lib/Banners.jar \\
duke@0 701 .fl
duke@0 702 src/farewells/GoodBye.java \-d classes\fP
duke@0 703 .fl
duke@0 704 % \f3ls classes\fP
duke@0 705 .fl
duke@0 706 farewells/
duke@0 707 .fl
duke@0 708 % \f3ls classes/farewells\fP
duke@0 709 .fl
duke@0 710 Base.class GoodBye.class
duke@0 711 .fl
duke@0 712
duke@0 713 .fl
duke@0 714 .fi
duke@0 715 .RE
duke@0 716
duke@0 717 .LP
duke@0 718 .LP
duke@0 719 \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.
duke@0 720 .LP
duke@0 721 .RE
duke@0 722 .SS
duke@0 723 Cross\-Compilation Example
duke@0 724 .LP
duke@0 725 .RS 3
duke@0 726
duke@0 727 .LP
duke@0 728 .LP
duke@0 729 Here we use \f3javac\fP to compile code that will run on a 1.4 VM.
duke@0 730 .LP
duke@0 731 .RS 3
duke@0 732
duke@0 733 .LP
duke@0 734 .nf
duke@0 735 \f3
duke@0 736 .fl
duke@0 737 % \fP\f3javac \-target 1.4 \-bootclasspath jdk1.4.2/lib/classes.zip \\
duke@0 738 .fl
duke@0 739 \-extdirs "" OldCode.java\fP
duke@0 740 .fl
duke@0 741 .fi
duke@0 742 .RE
duke@0 743
duke@0 744 .LP
duke@0 745 .LP
duke@0 746 The \f3\-target 1.4\fP option ensures that the generated class files will be compatible with 1.4 VMs. By default, \f3javac\fP compiles for JDK 6.
duke@0 747 .LP
duke@0 748 .LP
duke@0 749 The Java Platform JDK's \f3javac\fP would also by default compile against its own bootstrap classes, so we need to tell \f3javac\fP to compile against JDK 1.4 bootstrap classes instead. We do this with \f3\-bootclasspath\fP and \f3\-extdirs\fP. Failing to do this might allow compilation against a Java Platform API that would not be present on a 1.4 VM and would fail at runtime.
duke@0 750 .LP
duke@0 751 .RE
duke@0 752 .SH "SEE ALSO"
duke@0 753 .LP
duke@0 754
duke@0 755 .LP
duke@0 756 .RS 3
duke@0 757 .TP 2
duke@0 758 o
duke@0 759 java \- the Java Application Launcher
duke@0 760 .TP 2
duke@0 761 o
duke@0 762 jdb \- Java Application Debugger
duke@0 763 .TP 2
duke@0 764 o
duke@0 765 javah \- C Header and Stub File Generator
duke@0 766 .TP 2
duke@0 767 o
duke@0 768 javap \- Class File Disassembler
duke@0 769 .TP 2
duke@0 770 o
duke@0 771 javadoc \- API Documentation Generator
duke@0 772 .TP 2
duke@0 773 o
duke@0 774 jar \- JAR Archive Tool
duke@0 775 .TP 2
duke@0 776 o
duke@0 777 .na
duke@0 778 \f2The Java Extensions Framework\fP @
duke@0 779 .fi
duke@0 780 http://java.sun.com/javase/6/docs/technotes/guides/extensions/index.html
duke@0 781 .RE
duke@0 782
duke@0 783 .LP
duke@0 784
duke@0 785 .LP
duke@0 786