1javac(1)                    General Commands Manual                   javac(1)
2
3
4

NAME

6       javac - Java programming language compiler
7

SYNOPSIS

9       javac [ options ] [ sourcefiles ] [ @argfiles ]
10
11
12       Arguments may be in any order.
13
14             options
15                Command-line options.
16
17             sourcefiles
18                One   or   more   source   files   to  be  compiled  (such  as
19                MyClass.java).
20
21             @argfiles
22                One or more files that lists options and source files. The  -J
23                options are not allowed in these files.
24
25

DESCRIPTION

27       The  javac  tool  reads class and interface definitions, written in the
28       Java programming language, and compiles them into bytecode class files.
29
30       There are two ways to pass source code filenames to javac:
31
32          o For a small number of source files, simply list the file names  on
33            the command line.
34
35          o For a large number of source files, list the file names in a file,
36            separated by blanks or line breaks. Then use the list file name on
37            the javac command line, preceded by an @ character.
38
39
40       Source  code file names must have .java suffixes, class file names must
41       have .class suffixes, and both source and class files  must  have  root
42       names  that  identify  the  class.  For example, a class called MyClass
43       would be written in a source file called MyClass.java and compiled into
44       a bytecode class file called MyClass.class.
45
46       Inner  class  definitions  produce  additional class files. These class
47       files have names combining the inner and outer  class  names,  such  as
48       MyClass$MyInnerClass.class.
49
50       You should arrange source files in a directory tree that reflects their
51       package tree. For example,  if  you  keep  all  your  source  files  in
52       /workspace,  the source code for com.mysoft.mypack.MyClass should be in
53       /workspace/com/mysoft/mypack/MyClass.java.
54
55       By default, the compiler puts each class file in the same directory  as
56       its  source file. You can specify a separate destination directory with
57       -d (see Options, below).
58

SEARCHING FOR TYPES

60       When compiling a source file,  the  compiler  often  needs  information
61       about  a type whose definition did not appear in the source files given
62       on the command line. The compiler  needs  type  information  for  every
63       class  or  interface used, extended, or implemented in the source file.
64       This includes classes and interfaces not explicitly  mentioned  in  the
65       source file but which provide information through inheritance.
66
67       For  example,  when you subclass java.applet.Applet, you are also using
68       Applet's   ancestor   classes:   java.awt.Panel,    java.awt.Container,
69       java.awt.Component, and java.lang.Object.
70
71       When the compiler needs type information, it looks for a source file or
72       class file which defines the type.  The  compiler  searches  for  class
73       files  first  in  the bootstrap and extension classes, then in the user
74       class path (which by default is the current directory). The user  class
75       path  is  defined  by  setting the CLASSPATH environment variable or by
76       using the -classpath command line option. (For details, see Setting the
77       Class Path).
78
79       If  you set the -sourcepath option, the compiler searches the indicated
80       path for source files; otherwise the compiler searches the  user  class
81       path for both class files and source files.
82
83       You  can  specify  different  bootstrap  or  extension classes with the
84       -bootclasspath and  -extdirs  options;  see  Cross-Compilation  Options
85       below.
86
87       A  successful  type  search may produce a class file, a source file, or
88       both. Here is how javac handles each situation:
89
90          o Search produces a class file but no source file:  javac  uses  the
91            class file.
92
93          o Search  produces  a  source file but no class file: javac compiles
94            the source file and uses the resulting class file.
95
96          o Search produces both a source file and a class file: javac  deter‐
97            mines  whether the class file is out of date. If the class file is
98            out of date, javac recompiles the source file and uses the updated
99            class file. Otherwise, javac just uses the class file.
100
101          javac  considers  a  class file out of date only if it is older than
102          the source file.
103
104
105       Note:   javac can silently compile source files not  mentioned  on  the
106       command line. Use the -verbose option to trace automatic compilation.
107

OPTIONS

109       The  compiler  has  a set of standard options that are supported on the
110       current  development  environment  and  will  be  supported  in  future
111       releases. An additional set of non-standard options are specific to the
112       current virtual machine and compiler implementations and are subject to
113       change in the future. Non-standard options begin with -X.
114
115   Standard Options
116             -classpath classpath
117                Set the user class path, overriding the user class path in the
118                CLASSPATH  environment  variable.  If  neither  CLASSPATH   or
119                -classpath  is  specified, the user class path consists of the
120                current  directory.  See  Setting  the  Class  Path  for  more
121                details.
122
123             If  the  -sourcepath option is not specified, the user class path
124             is searched for both source files and class files.
125
126             As a special convenience, a class path element containing a base‐
127             name  of  *  is considered equivalent to specifying a list of all
128             the files in the directory with the extension  .jar  or  .JAR  (a
129             java  program  cannot tell the difference between the two invoca‐
130             tions).
131             For example, if directory foo contains a.jar and b.JAR, then  the
132             class  path  element  foo/*  is expanded to a A.jar:b.JAR, except
133             that the order of jar files is unspecified. All jar files in  the
134             specified  directory, even hidden ones, are included in the list.
135             A classpath entry consisting simply of * expands to a list of all
136             the jar files in the current directory. The CLASSPATH environment
137             variable, where defined, will be similarly expanded.  Any  class‐
138             path wildcard expansion occurs before the Java virtual machine is
139             started -- no Java program will  ever  see  unexpanded  wildcards
140             except by querying the environment. For example; by invoking Sys‐
141             tem.getenv("CLASSPATH").
142
143             -Djava.ext.dirs=directories
144                Override the location of installed extensions.
145
146             -Djava.endorsed.dirs=directories
147                Override the location of endorsed standards path.
148
149             -d directory
150                Set the destination directory for class files. The destination
151                directory must already exist; javac will not create the desti‐
152                nation directory. If a class is part of a package, javac  puts
153                the  class file in a subdirectory reflecting the package name,
154                creating directories as needed. For example, if you specify -d
155                /home/myclasses and the class is called com.mypackage.MyClass,
156                then the  class  file  is  called  /home/myclasses/com/mypack‐
157                age/MyClass.class.
158
159             If  -d  is  not  specified, javac puts the class file in the same
160             directory as the source file.
161
162             Note:   The directory specified by -d is not automatically  added
163             to your user class path.
164
165             -deprecation
166                Show  a  description  of  each use or override of a deprecated
167                member or class. Without -deprecation, javac shows  the  names
168                of  source  files  that  use or override deprecated members or
169                classes. -deprecation is shorthand for -Xlint:deprecation.
170
171             -encoding encoding
172                Set the source file encoding name, such as EUC-JP and  UTF-8..
173                If  -encoding is not specified, the platform default converter
174                is used.
175
176             -g Generate all debugging information, including local variables.
177                By  default,  only  line number and source file information is
178                generated.
179
180             -g:none
181                Do not generate any debugging information.
182
183             -g:{keyword list}
184                Generate only some kinds of debugging  information,  specified
185                by a comma separated list of keywords. Valid keywords are:
186
187                source
188                   Source file debugging information
189
190                lines
191                   Line number debugging information
192
193                vars
194                   Local variable debugging information
195
196             -help
197                Print a synopsis of standard options.
198
199             -nowarn
200                Disable  warning  messages.  This  has  the  same  meaning  as
201                -Xlint:none.
202
203             -source release
204                Specifies the version of source code accepted.  The  following
205                values for release are allowed:
206
207                1.3
208                   The  compiler  does  not  support  assertions, generics, or
209                   other language features introduced after JDK 1.3.
210
211                1.4
212                   The compiler accepts code containing assertions, which were
213                   introduced in JDK 1.4.
214
215                1.5
216                   The  compiler  accepts  code  containing generics and other
217                   language features introduced in JDK 5. This is the default.
218
219                5  Synonym for 1.5
220             Note: No language changes were introduced in JDK 6, so the values
221             1.6 and 6 are not valid.
222
223             -sourcepath sourcepath
224                Specify  the source code path to search for class or interface
225                definitions. As with the user class path, source path  entries
226                are  separated  by  colons (:) and can be directories, JAR ar‐
227                chives, or ZIP archives. If packages are used, the local  path
228                name  within the directory or archive must reflect the package
229                name.
230
231             Note:   Classes found through the classpath are subject to  auto‐
232             matic recompilation if their sources are found.
233
234             -verbose
235                Verbose  output.  This  includes  information about each class
236                loaded and each source file compiled.
237
238             -X Display information about non-standard options and exit.
239
240
241   Cross-Compilation Options
242          By default, classes are compiled against the bootstrap and extension
243          classes of the platform that javac shipped with. But javac also sup‐
244          ports cross-compiling, where classes are compiled  against  a  boot‐
245          strap and extension classes of a different Java platform implementa‐
246          tion. It is  important  to  use  -bootclasspath  and  -extdirs  when
247          cross-compiling; see Cross-Compilation Example below.
248
249             -target version
250                Generate  class  files  that target a specified version of the
251                VM. Class files will run on the specified target and on  later
252                versions, but not on earlier versions of the VM. Valid targets
253                are 1.1 1.2 1.3 1.4 1.5 (also 5) and 1.6 (also 6).
254
255             The default for -target depends on the value of -source:
256
257                o If -source is not specified, the value of -target is 1.6
258
259                o If -source is 1.3, the value of -target is 1.1
260
261                o For all other values of -source, the value of -target is the
262                  value of -source.
263
264             -bootclasspath bootclasspath
265                Cross-compile  against  the  specified set of boot classes. As
266                with the user class path, boot class path  entries  are  sepa‐
267                rated  by  colons (:) and can be directories, JAR archives, or
268                ZIP archives.
269
270             -extdirs directories
271                Cross-compile against  the  specified  extension  directories.
272                Directories is a colon-separated list of directories. Each JAR
273                archive in the specified directories  is  searched  for  class
274                files.
275
276
277   Non-Standard Options
278             -Xbootclasspath/p:path
279                Prepend to the bootstrap class path.
280
281             -Xbootclasspath/a:path
282                Append to the bootstrap class path.
283
284             -Xbootclasspath/:path
285                Override location of bootstrap class files.
286
287             -Xlint
288                Enable  all  recommended warnings. In this release, all avail‐
289                able warnings are recommended.
290
291             -Xlint:none
292                Disable all warnings not mandated by the Java Language  Speci‐
293                fication.
294
295             -Xlint:-xxx
296                Disable  warning  xxx,  where  xxx is one of the warning names
297                supported for -Xlint:xxx, below
298
299             -Xlint:unchecked
300                Give more detail for unchecked conversion  warnings  that  are
301                mandated by the Java Language Specification.
302
303             -Xlint:path
304                Warn  about  nonexistent  path  (classpath,  sourcepath,  etc)
305                directories.
306
307             -Xlint:serial
308                Warn about missing serialVersionUID definitions  on  serializ‐
309                able classes.
310
311             -Xlint:finally
312                Warn about finally clauses that cannot complete normally.
313
314             -Xlint:fallthrough
315                Check switch blocks for fall-through cases and provide a warn‐
316                ing message for any that are  found.  Fall-through  cases  are
317                cases  in  a  switch  block,  other  than the last case in the
318                block, whose code does not include a break statement, allowing
319                code  execution  to  "fall through" from that case to the next
320                case. For example, the code following the case 1 label in this
321                switch block does not contain a break statement:
322
323
324                switch (x) {
325                case 1:
326                       System.out.println("1");
327                       //  No  break;  statement here.
328                case 2:
329                       System.out.println("2");
330                }
331             If  the  -Xlint:fallthrough  flag  were  used when compiling this
332             code,  the  compiler  would  emit  a  warning   about   "possible
333             fall-through  into  case," along with the line number of the case
334             in question.
335
336             -Xmaxerrors number
337                Set the maximum number of errors to print.
338
339             -Xmaxwarns number
340                Set the maximum number of warnings to print.
341
342             -Xstdout filename
343                Send compiler messages to the named file. By default, compiler
344                messages go to System.err.
345
346
347   The -J Option
348             -Joption
349                Pass option to the java launcher called by javac. For example,
350                -J-Xms48m sets the startup memory to 48 megabytes. Although it
351                does  not  begin  with  -X,  it  is not a `standard option' of
352                javac. It is a common convention for -J to pass options to the
353                underlying VM executing applications written in Java.
354
355             Note:     CLASSPATH,  -classpath, -bootclasspath, and -extdirs do
356             not specify the classes used to  run  javac.  Fiddling  with  the
357             implementation  of  the compiler in this way is usually pointless
358             and always risky. If you do need to do this, use the -J option to
359             pass through options to the underlying java launcher.
360
361

COMMAND LINE ARGUMENT FILES

363       To  shorten  or simplify the javac command line, you can specify one or
364       more files that themselves  contain  arguments  to  the  javac  command
365       (except  -J  options). This enables you to create javac commands of any
366       length on any operating system.
367
368       An argument file can include javac options and source filenames in  any
369       combination. The arguments within a file can be space-separated or new‐
370       line-separated. If a filename contains embedded spaces, put  the  whole
371       filename in double quotes.
372
373       Filenames  within  an  argument file are relative to the current direc‐
374       tory, not the location of the argument  file.  Wildcards  (*)  are  not
375       allowed  in these lists (such as for specifying *.java). Use of the '@'
376       character to recursively interpret  files  is  not  supported.  The  -J
377       options  are  not  supported  because  they are passed to the launcher,
378       which does not support argument files.
379
380       When executing javac, pass in the path and name of each  argument  file
381       with  the  '@'  leading  character.  When  javac encounters an argument
382       beginning with the character `@', it expands the contents of that  file
383       into the argument list.
384
385   Example - Single Arg File
386       You  could use a single argument file named "argfile" to hold all javac
387       arguments:
388
389         C:> javac @argfile
390
391
392       This argument file could contain the contents of both  files  shown  in
393       the next example.
394
395   Example - Two Arg Files
396       You  can create two argument files -- one for the javac options and the
397       other for the source filenames: (Notice the  following  lists  have  no
398       line-continuation characters.)
399
400       Create a file named "options" containing:
401
402            -d classes
403            -g
404            -sourcepath \java\pubs\ws\1.3\src\share\classes
405
406
407       Create a file named "classes" containing:
408
409            MyClass1.java
410            MyClass2.java
411            MyClass3.java
412
413
414       You would then run javac with:
415
416         % javac @options @classes
417
418
419   Example - Arg Files with Paths
420       The  argument  files can have paths, but any filenames inside the files
421       are relative to the current working directory (not path1 or path2):
422
423         % javac @path1/options @path2/classes
424
425

PROGRAMMATIC INTERFACE

427       The com.sun.tools.javac.Main  class  provides  two  static  methods  to
428       invoke the compiler from a program:
429
430       public static int compile(String[] args);
431       public static int compile(String[] args, PrintWriter out);
432
433
434       The  args  parameter  represents any of the command line arguments that
435       would normally be passed to the javac program and are outlined  in  the
436       above Synopsis section.
437
438       The  out  parameter indicates where the compiler's diagnostic output is
439       directed.
440
441       The return value is equivalent to the exit value from javac.
442
443       Note that all other classes and methods found in a package  whose  name
444       starts  with  com.sun.tools.javac  (informally known as sub-packages of
445       com.sun.tools.javac) are strictly internal and subject to change at any
446       time.
447

EXAMPLES

449   Compiling a Simple Program
450          One source file, Hello.java, defines a class called greetings.Hello.
451          The greetings directory is the package directory both for the source
452          file  and  the  class  file  and  is off the current directory. This
453          allows us to use the default user  class  path.  It  also  makes  it
454          unnecessary to specify a separate destination directory with -d.
455
456             % ls
457             greetings/
458             % ls greetings
459             Hello.java
460             % cat greetings/Hello.java
461             package greetings;
462
463             public class Hello {
464                 public static void main(String[] args) {
465                     for (int i=0; i < args.length; i++) {
466                         System.out.println("Hello " + args[i]);
467                     }
468                 }
469             }
470             % javac greetings/Hello.java
471             % ls greetings
472             Hello.class   Hello.java
473             % java greetings.Hello World Universe Everyone
474             Hello World
475             Hello Universe
476             Hello Everyone
477
478
479   Compiling Multiple Source Files
480          This example compiles all the source files in the package greetings.
481
482             % ls
483             greetings/
484             % ls greetings
485             Aloha.java         GutenTag.java      Hello.java         Hi.java
486             % javac greetings/*.java
487             % ls greetings
488             Aloha.class         GutenTag.class      Hello.class         Hi.class
489             Aloha.java          GutenTag.java       Hello.java          Hi.java
490
491
492
493   Specifying a User Class Path
494          Having  changed  one of the source files in the previous example, we
495          recompile it:
496
497             % pwd
498             /examples
499             % javac greetings/Hi.java
500
501
502          Since greetings.Hi refers to other classes in the greetings package,
503          the  compiler  needs  to find these other classes. The example above
504          works, because our default user class path happens to be the  direc‐
505          tory containing the package directory. But suppose we want to recom‐
506          pile this file and not worry about which directory we're in? Then we
507          need to add /examples to the user class path. We can do this by set‐
508          ting CLASSPATH, but here we'll use the -classpath option.
509
510             % javac -classpath /examples /examples/greetings/Hi.java
511
512
513          If we change greetings.Hi again, to use a banner utility, that util‐
514          ity also needs to be accessible through the user class path.
515
516             % javac -classpath /examples:/lib/Banners.jar \
517                                /examples/greetings/Hi.java
518
519
520          To  execute  a  class in greetings, we need access both to greetings
521          and to the classes it uses.
522
523             % java -classpath /examples:/lib/Banners.jar greetings.Hi
524
525
526   Separating Source Files and Class Files
527          It often makes sense to keep source files and class files  in  sepa‐
528          rate  directories,  especially on large projects. We use -d to indi‐
529          cate the separate class file destination. Since the source files are
530          not  in the user class path, we use -sourcepath to help the compiler
531          find them.
532
533             % ls
534             classes/  lib/      src/
535             % ls src
536             farewells/
537             % ls src/farewells
538             Base.java      GoodBye.java
539             % ls lib
540             Banners.jar
541             % ls classes
542             % javac -sourcepath src -classpath classes:lib/Banners.jar \
543               src/farewells/GoodBye.java -d classes
544             % ls classes
545             farewells/
546             % ls classes/farewells
547             Base.class      GoodBye.class
548
549
550
551          Note:   The compiler compiled src/farewells/Base.java,  even  though
552          we  didn't  specify  it on the command line. To trace automatic com‐
553          piles, use the -verbose option.
554
555   Cross-Compilation Example
556          Here we use javac to compile code that will run on a 1.4 VM.
557
558             % javac -target 1.4 -bootclasspath jdk1.4.2/lib/classes.zip \
559                     -extdirs "" OldCode.java
560
561
562          The -target 1.4 option ensures that the generated class  files  will
563          be compatible with 1.4 VMs. By default, javac compiles for JDK 6.
564
565          The  Java Platform JDK's javac would also by default compile against
566          its own bootstrap classes, so we  need  to  tell  javac  to  compile
567          against  JDK  1.4  bootstrap classes instead. We do this with -boot‐
568          classpath and -extdirs. Failing to do this might  allow  compilation
569          against  a  Java  Platform API that would not be present on a 1.4 VM
570          and would fail at runtime.
571

SEE ALSO

573          o java - the Java Application Launcher
574
575          o jdb - Java Application Debugger
576
577          o javah - C Header and Stub File Generator
578
579          o javap - Class File Disassembler
580
581          o javadoc - API Documentation Generator
582
583          o jar - JAR Archive Tool
584
585          o The Java Extensions Framework @
586            http://java.sun.com/javase/6/docs/technotes/guides/exten
587            sions/index.html
588
589
590                                  07 Aug 2006                         javac(1)
Impressum