JAVAC(1) JDK Commands JAVAC(1) (NAME) javac - (SYNOPSIS) javac [options] [sourcefiles-or-classnames] options . sourcefiles-or-classnames ( Shape.java) ( geometry.MyShape). (DESCRIPTION) javac . javac . .java . .class . . Shape Shape.java Shape.class . javac : o . o @filename . Standard Options javac Command-Line Argument Files . . javac . javac Arrangement of Source Code . javac . javac . javac . javac . "path" Standard Options Configuring a Compilation Searching for Module, Package and Type Declarations . javac . -d . javac : o JDK. o (debugger). o . o javadoc (/** ... */). o . o . javac Compiling for Earlier Releases Of The Platform API ..SH " (OPTIONS)" javac . . javac () . (-) (:) . (--) (=) . : -Aname="J. Duke" -proc:only -d myDirectory --module-version 3 --module-version=3 path ( ; Windows : ) . JAR JMOD . (Standard Options) @filename . javac javac ( -J) . javac . Command-Line Argument Files . -Akey[=value] (annotation processors) . javac . key (.) . --add-modules module,module (resolve) module ALL-MODULE-PATH . --boot-class-path path -bootclasspath path bootstrap () . : JDK 9 . --release -source -target . JDK 9 --system . --class-path path -classpath path -cp path . CLASSPATH . o --class-path -classpath -cp CLASSPATH ( ) . o --source-path -sourcepath` . o -processorpath . -d directory ( ) . (package) javac ( ) . . -d javac . . . : . --class-path . -deprecation (override) (deprecated) . -deprecation javac . -deprecation -Xlint:deprecation . --enable-preview . -source --release . -encoding encoding EUC-JP UTF-8 . -encoding . -endorseddirs directories (endorsed standards path) . : JDK 9 . --release -source -target . -extdirs directories (extensions) . directories (; Windows :) . JAR . JAR . (Extension Mechanism) . [Compiling for Other Releases of the Platform] . : JDK 9 . --release -source -target . -g . . -g:[lines, vars, source] . : lines . vars . source . -g:none . -h directory (native) . java.lang.annotation.Native . ( ) . . --help -help -? . --help-extra -X . --help-lint -Xlint . -implicit:[none, class] : o -implicit:class --- . o -implicit:none --- . . . -implicit . Searching for Module, Package and Type Declarations . -Joption option (runtime) option java . -J-Xms48m . : CLASSPATH -classpath -bootclasspath -extdirs javac . . -J . --limit-modules module,module* . --module module-name (,module-name)* -m module-name (,module-name)* . --module-path path -p path . --module-source-path module-source-path . [Compilation Modes] The Module Source Path Option . --module-version version . -nowarn . -Xlint:none . -parameters (reflection) . java.lang.reflect.Executable.getParameters Reflection API . -proc:[none, only] . -proc:none . -proc:only . -processor class1[,class2,class3...] . . --processor-module-path path . --processor-path path -processorpath path . (class path) . -profile profile API . : JDK 9 . --release -source -target . --release release (release) Java SE . API Java SE JDK . release Java SE . Java SE API java.* javax.* org.* Java SE (export) JDK API com.* jdk.* JDK javax.* Java SE . Java SE API JDK API . : --release --source/-source --target/-target . : --release Java Platform Module System --add-exports Java SE JDK . -s directory . ( ) . . . . --source release -source release Java Java SE . release Java SE . Java Java SE . --source-path path -sourcepath path . (class) (interface) . : (class path) . Searching for Module, Package and Type Declarations . --system jdk | none . --target release -target release class Java SE . release Java SE . : (target release) (source release) . ( --source .) --upgrade-module-path path . -verbose . . --version -version . -Werror . (Extra Options) --add-exports module/package=other-module(,other-module)* other-module ALL-UNNAMED (unnamed modules) (exported) . --add-reads module=other-module(,other-module)* . --default-module-for-created-files module-name (fallback) (annotation processors) . -Djava.endorsed.dirs=dirs (endorsed standards path) . : JDK 9 . --release -source -target . -Djava.ext.dirs=dirs . : JDK 9 . --release -source -target . --patch-module module=path JAR . -Xbootclasspath:path (bootstrap class files) . : JDK 9 . --release -source -target . -Xbootclasspath/a:path (bootstrap class path) . : JDK 9 . --release -source -target . -Xbootclasspath/p:path (bootstrap class path) . : JDK 9 . --release -source -target . -Xdiags:[compact, verbose] . -Xdoclint javadoc . -Xdoclint:(all|none|[-]group)[/access] group : o accessibility o html o missing o reference o syntax access (visibility) -Xdoclint . ( ): o public o protected o package o private access private . -Xdoclint javadoc . -Xdoclint javac . ( ) protected ( protected public ) : -Xdoclint:all/protected HTML package ( package protected public ) : -Xdoclint:all,-html/package -Xdoclint/package:[-]packages(,[-]package)* . package (qualified name) .* . package (-) . -Xlint . . -Xlint:[-]key(,[-]key)* . (-) . key : o all: . o auxiliaryclass: (auxiliary class) . o cast: (cast) . o classfile: classfile . o deprecation: (deprecated) . o dep-ann: javadoc @Deprecated . o divzero: 0 . o empty: if . o exports: (module exports) . o fallthrough: (fall- through) switch . o finally: finally . o module: . o opens: (module opens) . o options: . o overloads: (method overloads) . o overrides: (method overrides) . o path: . o processing: (annotation processing) . o rawtypes: (raw types) . o removal: API . o requires-automatic: (automatic modules) requires . o requires-transitive-automatic: requires transitive . o serial: (serializable) . . o static: (static) . o try: try ( try-with-resources) . o unchecked: (unchecked) . o varargs: vararg . o none: . Examples of Using -Xlint keys . -Xmaxerrs number . -Xmaxwarns number . -Xpkginfo:[always, legacy, nonempty] javac package-info.class package-info.java : always package-info.java package-info.class . Ant .java .class . legacy package-info.class package-info.java (annotations) . package-info.java (comments) package-info.class . : package-info.class package-info.java RetentionPolicy.SOURCE . nonempty package-info.class package-info.java RetentionPolicy.CLASS RetentionPolicy.RUNTIME . -Xplugin:name args (plug-in) . args name args (escape) . API API jdk.compiler/com.sun.source.util.Plugin . -Xprefer:[source, newer] . Searching for Module, Package and Type Declarations . o -Xprefer:newer: (). o -Xprefer:source : . -Xprefer:source SOURCE . -Xprint . . . -XprintProcessorInfo . -XprintRounds . -Xstdout filename . System.err . CLASSPATH --class-path CLASSPATH . --class-path . JDK_JAVAC_OPTIONS JDK_JAVAC_OPTIONS ( ) (\n \t \r \f) javac . javac . JDK_JAVAC_OPTIONS . (') (") . . (launcher) . @files . @files (wildcard) . : export JDK_JAVAC_OPTIONS='@"C:\white spaces\argfile"' export JDK_JAVAC_OPTIONS='"@C:\white spaces\argfile"' export JDK_JAVAC_OPTIONS='@C:\"white spaces"\argfile' (COMMAND-LINE ARGUMENT FILES) . . . . (*) ( *.java). (@) . -J . javac (@) . javac (@) . javac @filename argfile javac : javac @argfile . : javac . . options : macOS: -d classes -g -sourcepath /java/pubs/ws/1.3/src/share/classes : -d classes -g -sourcepath C:\java\pubs\ws\1.3\src\share\classes classes : MyClass1.java MyClass2.java MyClass3.java javac : javac @options @classes ( path1 path2): javac @path1/options @path2/classes Java (interfaces) (packages) . javac . . (subpackage) . : o p p . o p.q -- q p -- q p . p.q p\q p/q . o p.q.r p\q\r ( ) p/q/r ( ) . .java . : o X p X.java p . o Y p.q Y.java q p . o Z p.q.r Z.java r p\q ( ) p/q ( ) . javac . Java . .java module-info.java : 1. 2. ( ) 3. ( ). module-info.java . module-info.java ( ) . (IDE) . : o a.b.c a.b.c . o module-info.java a.b.c . o p.q.r a.b.c p\q\r ( ) p/q/r ( ) . javac . : o a.b.c a.b.c o a.b.c src\main\java ( ) src/main/java ( ) . (CONFIGURING A COMPILATION) javac . Configuring the Module System . o . . (argument files) . --module -m . o -d . . (module path) (launcher) (class path) ..SS . . . o --class-path . . : -classpath -cp. o --module-path . . : -p. Configuring the Module System . : . . . : . o --source-path javac . . : -sourcepath. o --module-source-path javac . . The Module Source Path Option . -implicit . : . Aloha.java GutenTag.java Hello.java Hi.java greetings . macOS: % javac greetings/*.java % ls greetings Aloha.class GutenTag.class Hello.class Hi.class Aloha.java GutenTag.java Hello.java Hi.java : C:\>javac greetings\*.java C:\>dir greetings Aloha.class GutenTag.class Hello.class Hi.class Aloha.java GutenTag.java Hello.java Hi.java : macOS: pwd /examples javac greetings/Hi.java : C:\>cd \examples C:\>javac greetings\Hi.java greetings.Hi greetings . . examples CLASSPATH . -classpath . macOS: javac -classpath /examples /examples/greetings/Hi.java : C:\>javac -classpath \examples \examples\greetings\Hi.java greetings.Hi . macOS: javac -classpath /examples:/lib/Banners.jar \ /examples/greetings/Hi.java : C:\>javac -classpath \examples;\lib\Banners.jar ^ \examples\greetings\Hi.java greetings greetings greetings . macOS: java -classpath /examples:/lib/Banners.jar greetings.Hi : C:\>java -classpath \examples;\lib\Banners.jar greetings.Hi --add-modules . (automatic module) API . --limit-modules . . (encapsulation) (exported) --add-exports . (white-box testing) API . --add-reads . API . --patch-module . [Patching a Module] . (SEARCHING FOR MODULE, PACKAGE AND TYPE DECLARATIONS) (type) . javac (interface) (extended) (implemented) . . java.awt.Window Window : java.awt.Container java.awt.Component java.lang.Object. . . -Xprefer . javac . -implicit . (annotation processing) . -implicit . ( ) -implicit . javac . (Searching Package Oriented Paths) javac . () . . -- -- . (instrumentation) . (class path) . (Searching Module Oriented Paths) javac (lazy) : o ( --module-source-path ) o ( --upgrade-module-path ) o ( --system ) o ( --module-path ) () . javac . JAR. javac . (Searching for the Declaration of a Module) module-info.class . module-info.class module-info.java ..SS " " javac : o ( ) ( .) o (module path) . ( .) o (class path) / (source path): o javac . o javac . o . / . . -Xprefer . javac . javac . javac . javac . javac . javac : o (source path) (module source path). o . (DIRECTORY HIERARCHIES) javac ( JAR) . : (package hierarchy) (module hierarchy) (module source hierarchy). javac ( ) javac . javac <> --source-path --class-path. module --module-path . . (Package Hierarchy) .java .class . com.example.MyClass com/example/MyClass.java . (Module Hierarchy) . com.example.MyClass my.library my.library/com/example/MyClass.class . javac ( /native header) . (Module Source Hierarchy) . . com.example.MyClass my.library my.library/src/main/java/com/example/MyClass.java ..SH " (THE MODULE SOURCE PATH OPTION)" --module-source-path : (module-specific) (module-pattern) . . --module-source-path : o o . (Module-specific form) . : o --module-source-path module-name=file-path (path-separator file-path)* ; : . : --patch-module . (Module-pattern form) . o --module-source-path pattern : o (; :) . o string1{alt1 ( ,alt2 )* } string2 << >> : string1 alt1 string2 string1 alt2 string2 and so on... . . o (*) . . M M . : . * (wildcard) . (PATCHING MODULES) javac --patch-module . JVM ( ) . : o --patch-module module-name=file-path (path-separator file-path )* ; : . . . . --module-source-path . (ANNOTATION PROCESSING) javac (annotation processing) . (API) javax.annotation.processing javax.lang.model ..SS (How Annotation Processing Works) -proc:none . -processorpath . (user class path) . META-INF/services/javax.annotation.processing.Processor . . -processor . . . (claim) . . : . . . . -proc:only . . --default-module-for-created-files . (Compilation Environment and Runtime Environment) javac javac . javac launcher --class-path --module-path javac javac . . () . . ( --processor-path .) . ( --processor-module-path .) . : 1. javac -J . 2. Java Virtual Machine API javac API . (COMPILING FOR EARLIER RELEASES OF THE PLATFORM) javac --release --source/-source --target/-target . . o JDK 8 . : o --module-source-path, --upgrade-module-path, --system, --module-path, --add-modules, --add-exports, --add-opens, --add-reads, --limit-modules, --patch-module --source/-source --target/-target boot class path . o JDK 9 boot class path . : o -Xbootclasspath/p:, -Xbootclasspath, -Xbootclasspath/a:, -endorseddirs, -Djava.endorsed.dirs, -extdirs, -Djava.ext.dirs, -profile --source/-source --target/-target --system JDK . --release API ..SH " (APIS)" javac API : Java Compiler API . ToolProvider API ToolProvider javac ToolProvider.findFirst("javac") . . : API API javax.tools . javac (Legacy API) API . Java Compiler API ToolProvider API . : com.sun.tools.javac ( com.sun.tools.javac) . -XLINT (EXAMPLES OF USING -XLINT KEYS) cast (cast) : String s = (String) "Hello!" classfile (class file) . deprecation (deprecated) . : java.util.Date myDate = new java.util.Date(); int currentDay = myDate.getDay(); java.util.Date.getDay JDK 1.1 . dep-ann Javadoc @deprecated (annotation) @Deprecated : /** * @deprecated As of Java SE 7, replaced by {@link #newMethod()} */ public static void deprecatedMethod() { } public static void newMethod() { } divzero 0 : int divideByZero = 42 / 0; empty if : class E { void m() { if (true) ; } } fallthrough switch fall-through . fall-through switch case break case (fall through). case 1 switch break : switch (x) { case 1: System.out.println("1"); // No break statement here. case 2: System.out.println("2"); } -Xlint:fallthrough fall-through case . finally finally : public static int m() { try { throw new NullPointerException(); } catch (NullPointerException(); { System.err.println("Caught NullPointerException."); return 1; } finally { return 0; } } finally . int 0 . finally try . catch int . finally . options . Compiling for Earlier Releases of the Platform . overrides (override) . : public class ClassWithVarargsMethod { void varargsMethod(String... s) { } } public class ClassWithOverridingMethod extends ClassWithVarargsMethod { @Override void varargsMethod(String[] s) { } } : warning: [override] varargsMethod(String[]) in ClassWithOverridingMethod overrides varargsMethod(String...) in ClassWithVarargsMethod; overriding method is missing '...' varargs varargs . ClassWithVarargsMethod.varargsMethod varargs String... s String[] s ClassWithOverridingMethod.varargsMethod . . path ( ) . @SuppressWarnings . : o Linux macOS: javac -Xlint:path -classpath /nonexistentpath Example.java o Windows: javac -Xlint:path -classpath C:\nonexistentpath Example.java processing (annotation processing) . . : AnnoProc.java: import java.util.*; import javax.annotation.processing.*; import javax.lang.model.*; import javax.lang.model.element.*; @SupportedAnnotationTypes("NotAnno") public class AnnoProc extends AbstractProcessor { public boolean process(Set elems, RoundEnvironment renv){ return true; } public SourceVersion getSupportedSourceVersion() { return SourceVersion.latest(); } } AnnosWithoutProcessors.java: @interface Anno { } @Anno class AnnosWithoutProcessors { } AnnoProc AnnosWithoutProcessors.java : javac AnnoProc.java javac -cp . -Xlint:processing -processor AnnoProc -proc:only AnnosWithoutProcessors.java AnnosWithoutProcessors.java : warning: [processing] No processor claimed any of these annotations: Anno AnnosWithoutProcessors Anno NotAnno . rawtypes (raw types) . rawtypes : void countElements(List l) { ... } rawtypes : void countElements(List l) { ... } List . List (unbounded wildcard) . List (interface) . List (?) countElements List . serial serialVersionUID (serializable) . : public class PersistentTime implements Serializable { private Date time; public PersistentTime() { time = Calendar.getInstance().getTime(); } public Date getTime() { return time; } } : warning: [serial] serializable class PersistentTime has no definition of serialVersionUID serialVersionUID ( Java Object Serialization Specification ) serialVersionUID . serialVersionUID serialVersionUID . InvalidClassExceptions (deserialization) . serialVersionUID serialVersionUID . static (static) : class XLintStatic { static void m1() { } void m2() { this.m1(); } } : warning: [static] static method should be qualified by type name, XLintStatic, instead of by an expression static m1 : XLintStatic.m1(); static m1 . try try try-with-resources . ac try : try ( AutoCloseable ac = getResource() ) { // do nothing} unchecked (unchecked conversion) Java Language Specification : List l = new ArrayList(); List ls = l; // unchecked warning (type erasure) ArrayList List ArrayList List . ls List . List l ls unchecked . JVM l List . l List . (heap pollution) . List l List List ls (List) . . (backward compatibility) Java SE generics . (type erasure) List List List . l (raw type) List ls . varargs (varargs) (non-reifiable) : public class ArrayBuilder { public static void addToList (List listArg, T... elements) { for (T x : elements) { listArg.add(x); } } } (non-reifiable) . ArrayBuilder.addToList : warning: [varargs] Possible heap pollution from parameterized vararg type T varargs varargs . . ArrayBuilder.addToList varargs T... elements T[] elements . (type erasure) varargs Object[] elements . (heap pollution) . JDK 17 2021 JAVAC(1)