JAVAC(1) JDK Commands JAVAC(1) (NAME) javac - (SYNOPSIS) javac [options] [sourcefiles-or-classnames] options . sourcefiles-or-classnames ( Shape.java) ( geometry.MyShape). (DESCRIPTION) javac Java (JVM) . javac (annotations) . .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 () (extended) (implemented) . javac . javac . javac . "path" (Standard Options) (Configuring a Compilation) (Searching for Module, Package and Type Declarations) . javac . -d . javac : o JDK. o . 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 class files) . : 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 ( ) . javac ( ) . . -d javac . . . : . --class-path . -deprecation (override) . -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 . directories ( ; Windows : ) . JAR . JAR . (Extension Mechanism) . [Compiling for Other Releases of the Platform] . : JDK 9 . --release -source -target . -g . . -g:[lines, vars, source] . : lines ..TP vars . source . -g:none . -h directory (native header) . 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 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 API . -proc:[none, only, full] . -proc:none . -proc:only . -proc:full . -processor class1[,class2,class3...] . (discovery) . --processor-module-path path . --processor-path path -processorpath path . (class path) . -profile profile API (profile) . . : JDK 9 . --release -source -target . --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 (release) Java SE . release Java SE . Java Java SE . --source-path path -sourcepath path . (interface) . : (class path) . Searching for Module, Package and Type Declarations . --system jdk | none . --target release -target release class Java SE . release Java SE . : . (--source .) --upgrade-module-path path . -verbose . . --version -version . -Werror . (Extra Options) --add-exports module/package=other-module(,other-module)* other-module ALL-UNNAMED (export) . --add-reads module=other-module(,other-module)* . --default-module-for-created-files module-name (fallback) (annotation processors) . -Djava.endorsed.dirs=dirs (endorsed standards) . : JDK 9 . --release -source -target . -Djava.ext.dirs=dirs . : JDK 9 . --release -source -target . --patch-module module=path JAR . -Xbootclasspath:path (bootstrap) . : JDK 9 . --release -source -target . -Xbootclasspath/a:path . : JDK 9 . --release -source -target . -Xbootclasspath/p:path . : JDK 9 . --release -source -target . -Xdiags:[compact, verbose] . -Xdoclint . -Xdoclint:(all|none|[-]group)[/access] . group : accessibility html missing reference syntax. access (visibility) -Xdoclint . ( ): public protected package private. access private . doclint: group all @SuppressWarnings . DocLint javadoc . -Xdoclint javac . ( ) protected ( protected public): -Xdoclint:all/protected HTML package ( package protected public) : -Xdoclint:all,-html/package -Xdoclint/package:[-]packages(,[-]package)* . package .* . package (-) . DocLint javadoc . -Xlint . . -Xlint:[-]key(,[-]key)* . (-) . key : o all: . o auxiliaryclass: . o cast: (cast) . o classfile: (classfile) . o deprecation: . o dep-ann: javadoc @Deprecated . o divzero: 0 . o empty: if . o exports: (module exports) . o fallthrough: (fall-through) switch . o finally: finally . o lossy-conversions: . o missing-explicit-ctor: public protected . o module: . o opens: (module opens) . o options: . o output-file-clash: . . o overloads: (method overloads) . o overrides: (method overrides) . o path: . o preview: . o processing: (annotation processing) . o rawtypes: (raw types) . o removal: API . o requires-automatic: requires . o requires-transitive-automatic: requires transitive . o serial: (serializable) (serial version ID) . . o static: (static) . o strictfp: strictfp . o synchronization: . o text-blocks: . o this-escape: this . o try: try ( try-with-resources) . o unchecked: (unchecked) . o varargs: vararg . o none: . all none @SuppressWarnings . 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 . package-info.java (comments) package-info.class . : package-info.java RetentionPolicy.SOURCE package-info.class . nonempty package-info.class package-info.java RetentionPolicy.CLASS RetentionPolicy.RUNTIME . -Xplugin:name args . 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 : . SOURCE -Xprefer:source . -Xprint . . . -XprintProcessorInfo . -XprintRounds . -Xstdout filename . System.err . (ENVIRONMENT VARIABLES) 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 (@) ..SS javac @filename argfile javac : javac @argfile . : javac . . options : Linux macOS: -d classes -g -sourcepath /java/pubs/ws/1.3/src/share/classes Windows: -d classes -g -sourcepath C:\java\pubs\ws\1.3\src\share\classes sources : MyClass1.java MyClass2.java MyClass3.java javac : javac @options @sources ( path1 path2): javac @path1/options @path2/sources (ARRANGEMENT OF SOURCE CODE) Java (interfaces) . javac . ..SS . : 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 ( )..SH " (CONFIGURING A COMPILATION)" javac . Configuring the Module System . (Source Files) o . . . --module -m . (Output Directory) o -d . . (module path) (class path) . (Precompiled Code) . . . o --class-path . . : -classpath -cp. o --module-path . . : -p. Configuring the Module System . : . (Additional Source Files) . (source path) . : (module source path) . o --source-path javac . . : -sourcepath. o --module-source-path javac . . The Module Source Path Option . -implicit . : ..SS 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 (User Class Path) : 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 banner . 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 . . (exported) --add-exports . (white-box testing) API (production) . --add-reads . API . --patch-module (patch) . [Patching a Module] . (SEARCHING FOR MODULE, PACKAGE AND TYPE DECLARATIONS) (type) (declaration) . javac (interface) (extended) (implemented) . . (subclass) 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 . () . . . . (class path) ..SS javac (lazy) : o ( --module-source-path ) o ( --upgrade-module-path ) o ( --system ) o ( --module-path ) ( ). javac . JAR. javac . module-info.class . module-info.class module-info.java . javac : o ( ) ( .) o . ( .) o (class path) / (source path): o javac . o javac . o . / . . -Xprefer . javac . javac . javac . javac . javac . javac : o . o ..SH " (DIRECTORY HIERARCHIES)" javac ( JAR) . : (package hierarchy) (module hierarchy) (module source hierarchy). javac ( ) Java javac Java . javac "path" --source-path --class-path. module --module-path . . (Package Hierarchy) (type) .java .class . com.example.MyClass com/example/MyClass.java . (Module Hierarchy) . com.example.MyClass my.library my.library/com/example/MyClass.class . javac ( ) . (Module Source Hierarchy) . . com.example.MyClass my.library my.library/src/main/java/com/example/MyClass.java . (THE MODULE SOURCE PATH OPTION) --module-source-path : . . --module-source-path : o o ..SS " (Module-specific form)" . : o --module-source-path module-name=file-path (path-separator file- path)* Windows ; : . : --patch-module . (Module-pattern form) . o --module-source-path pattern : o (; Windows :) . 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 )* Windows ; : . (package hierarchy) . . . --module-source-path ..SH " (ANNOTATION PROCESSING)" javac . (API) javax.annotation.processing javax.lang.model . -proc:none . -processorpath . (user class path) . META-INF/services/javax.annotation.processing.Processor . . -processor . . . . . : . . . . -proc:only . . --default-module-for-created-files . javac javac . javac launcher ( --class-path --module-path ) javac javac . . () . . ( --processor-path .) . ( --processor-module-path .) . : 1. javac -J . 2. Java Virtual Machine API javac APIs ..SH " (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 . (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) ..SH " -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 @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 cases) . switch ( ) break (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 case 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 (method overrides) . : 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 ( class path source path ) . @SuppressWarnings . : o macOS: javac -Xlint:path -classpath /nonexistentpath Example.java.IP o 2 : 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 . (deserialization) InvalidClassExceptions . serialVersionUID Java 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 . this-escape this . : public class MyClass { public MyClass() { System.out.println(this.hashCode()); } } : MyClass.java:3: warning: [this-escape] possible 'this' escape before subclass is fully initialized System.out.println(this.hashCode()); ^ 'this' . . MyClass hashCode() . ( ) . final public . 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 . 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); } } } (runtime) . ArrayBuilder.addToList : warning: [varargs] Possible heap pollution from parameterized vararg type T varargs varargs . Java . ArrayBuilder.addToList varargs T... elements T[] elements . varargs Object[] elements . . JDK 21 2023 JAVAC(1)