javac(1) Basic Tools javac(1) (NAME) javac - . (SYNOPSIS) javac [ options ] [ sourcefiles ] [ classes] [ @argfiles ] : options . <<>> . sourcefiles ( MyClass.java). classes ( MyPackage.MyClass). @argfiles . -J . << >> . (DESCRIPTION) javac . javac . javac . o . o . at (@) javac . .java .class . MyClass MyClass.java MyClass.class . . MyClass$MyInnerClass.class. (package) . /workspace com.mysoft.mypack.MyClass /workspace/com/mysoft/mypack/MyClass.java . . -d ..SH " (OPTIONS)" . . -X . o << >> (Cross-Compilation Options) o << >> (Nonstandard Options) (STANDARD OPTIONS) -Akey[=value] . javac . key (.) . -cp path -classpath path ( ) . CLASSPATH (override) . CLASSPATH -cp -classpath class path . Setting the Class Path . -sourcepath . -processorpath . -Djava.ext.dirs=directories . -Djava.endorsed.dirs=directories . -d directory . javac . javac . -d /home/myclasses com.mypackage.MyClass /home/myclasses/com/mypackage/MyClass.class . -d javac . : -d . -deprecation (override) (deprecated) . -deprecation javac . -deprecation -Xlint:deprecation . -encoding encoding EUC-JP UTF-8. -encoding . -endorseddirs directories . -extdirs directories ext . directories (:) . JAR . JAR . ( bootstrap Java) . Cross-Compilation Options . -g . . -g:none . -g:[keyword list] . : source . lines . vars . -help . -implicit:[class, none] . -implicit:class . -implicit:none . . . -implicit . Searching for Types . -Joption option (JVM) option . -J-Xms48m . java(1) . : CLASSPATH -classpath -bootclasspath -extdirs javac . . -J Java . -nowarn . -Xlint:none . -parameters java.lang.reflect.Executable.getParameters Reflection API . -proc: [none, only] . -proc:none . -proc:only . -processor class1 [,class2,class3...] . . -processorpath path . . -s dir . javac . . -s /home/mysrc com.mypackage.MyClass /home/mysrc/com/mypackage/MyClass.java . -source release . release : 1.3 assertion generic Java SE 1.3 . 1.4 assertion Java SE 1.4 . 1.5 generic Java SE 5 . 5 1.5. 1.6 Java SE 6 . Java Platform, Standard Edition . 6 1.6. 1.7 Java SE 7 . 7 1.7. 1.8 . Java SE 8 . 8 1.8. -sourcepath sourcepath (interface) . Oracle Solaris (:) Windows (;) JAR ZIP . . : . Searching for Types . -verbose . -version . -werror . -X ..SS " (CROSS-COMPILATION OPTIONS)" (bootstrap) (extension) javac . javac Java . -bootclasspath -extdirs . -target version . JVM . 1.1 1.2 1.3 1.4 1.5 ( 5) 1.6 ( 6) 1.7 ( 7) 1.8 ( 8). -target -source : o -source -target 1.8 o -source 1.2 -target 1.4 o -source 1.3 -target 1.4 o -source 1.5 -target 1.8 o -source 1.6 -target 1.8 o -source 1.7 -target 1.8 o -source -target -source . -bootclasspath bootclasspath . (:) JAR ZIP . (COMPACT PROFILE OPTION) JDK 8 javac (compact profiles) . Java . . Java JRE . . compact1 compact2 compact3 . . API . -profile . : javac -profile compact1 Hello.java javac API Java SE . : cd jdk1.8.0/bin ./javac -profile compact1 Paint.java Paint.java:5: error: Applet is not available in profile 'compact1' import java.applet.Applet; Applet . -profile . API Java SE . ( Applet .) -bootclasspath rt.jar . -profile . . (NONSTANDARD OPTIONS) -Xbootclasspath/p:path (bootstrap class path) . -Xbootclasspath/a:path . -Xbootclasspath/:path . -Xdoclint:[-]group [/access] group : accessibility syntax reference html missing. -Xdoclint javadoc . -Xdoclint javac . access -Xdoclint . ( ): public protected package private. ( ) protected ( protected package public ): -Xdoclint:all/protected HTML package ( package public) : -Xdoclint:all,-html/package -Xdoclint:none . -Xdoclint:all[/access] . -Xlint . . -Xlint:all . . -Xlint:none . -Xlint:name name . Enable or Disable Warnings with the -Xlint Option . -Xlint:-name name . Enable or Disable Warnings with the -Xlint Option -Xlint . -Xmaxerrs number . -Xmaxwarns number . -Xstdout filename . System.err . -Xprefer:[newer,source] . ( Searching for Types ). -Xprefer:newer (). -Xprefer:source . SOURCE -Xprefer:source . -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 package-info.class . : package-info.java RetentionPolicy.SOURCE package-info.class . nonempty package- info.class package-info.java RetentionPolicy.CLASS RetentionPolicy.RUNTIME . -Xprint . . ..TP -XprintProcessorInfo (annotations) . -XprintRounds (annotation processing rounds) . -XLINT (ENABLE OR DISABLE WARNINGS WITH THE -XLINT OPTION) name -Xlint:name name . -Xlint:-name: . cast (casts) : 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 @Deprecated : /** * @deprecated As of Java SE 7, replaced by {@link #newMethod()} */ public static void deprecatedMethood() { } public static void newMethod() { } divzero : int divideByZero = 42 / 0; empty if : class E { void m() { if (true) ; } } fallthrough switch (fall- through) . fall-through switch ( case ) break case case . 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 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 . Cross-Compilation Options . overrides (method overrides) . :.sp 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 : javac -Xlint:path -classpath /nonexistentpath Example.java processing (annotation processing) . . : AnnocProc.java: import java.util.*; import javax.annotation.processing.*; import javax.lang.model.*; import.javaz.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 (raw type) . List (unbounded wildcard parameterized type) . 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 serialVersionUID Java Object Serialization Specification . serialVersionUID serialVersionUID InvalidClassExceptions . serialVersionUID Java serialVersionUID . static (statics) : 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 warnings) Java Language Specification : List l = new ArrayList(); List ls = l; // unchecked warning (type erasure) ArrayList List ArrayList List . ls List . List l ls (unchecked warning) . JVM l List . l List . (heap pollution) . List l List List ls List . . Java SE (generics) . List List List . l, 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 .sp warning: [varargs] Possible heap pollution from parameterized vararg type T varargs varargs . Java (parameterized types) . ArrayBuilder.addToList varargs T... elements T[] elements . (type erasure) varargs Object[] elements . (heap pollution) . (COMMAND-LINE ARGUMENT FILES) javac javac ( -J) . javac . javac . (newline) . (double quotation marks) . . (*) ( *.java). (@) . -J (launcher) . javac (@) . javac (@) . : (Example 1 Single Argument File) argfile javac : javac @argfile . : (Example 2 Two Argument Files) : javac . . options : -d classes -g -sourcepath /java/pubs/ws/1.3/src/share/classes classes : MyClass1.java MyClass2.java MyClass3.java javac : javac @options @classes : (Example 3 Argument Files with Paths) ( path1 path2): javac @path1/options @path2/classes javac apt . (API) javax.annotation.processing javax.lang.model . (HOW ANNOTATION PROCESSING WORKS) -proc:none . -processorpath . (user class path) . META-INF/services/javax.annotation.processing.Processor . . -processor . . . . . : . . . . -proc:only . (IMPLICITLY LOADED SOURCE FILES) . << >> . . . -implicit . (SEARCHING FOR TYPES) (type) . (interface) . . java.applet.Applet Applet : java.awt.Panel java.awt.Container java.awt.Component java.lang.Object. . (bootstrap) (extension) ( ) . CLASSPATH -classpath . -sourcepath . . -bootclasspath -extdirs . << (Cross-Compilation Options)>> . . -Xprefer . newer . source . newer . ( -Xprefer) . . -implicit . none . class . . -implicit . ( ) -implicit . (PROGRAMMATIC INTERFACE) javac (Java Compiler API) javax.tools . (EXAMPLE) : JavaCompiler javac = ToolProvider.getSystemJavaCompiler(); javac . javax.tools.JavaCompiler . (OLD INTERFACE) : API . (Java Compiler API) . com.sun.tools.javac.Main : public static int compile(String[] args); public static int compile(String[] args, PrintWriter out); args . out . (return) (exit) javac . : com.sun.tools.javac ( com.sun.tools.javac) . (EXAMPLES) : Hello.java greetings . Hello.java greetings.Hello . greetings . . -d . Hello.java: package greetings; public class Hello { public static void main(String[] args) { for (int i=0; i < args.length; i++) { System.out.println("Hello " + args[i]); } } } greetings.Hello: javac greetings/Hello.java greetings.Hello: java greetings.Hello World Universe Everyone Hello World Hello Universe Hello Everyone : Aloha.java GutenTag.java Hello.java Hi.java greetings . % javac greetings/*.java % ls greetings Aloha.class GutenTag.class Hello.class Hi.class Aloha.java GutenTag.java Hello.java Hi.java : : pwd /examples javac greetings/Hi.java greetings.Hi greetings . . examples CLASSPATH . -classpath . javac -classpath /examples /examples/greetings/Hi.java greetings.Hi (banner utility) . javac -classpath /examples:/lib/Banners.jar \ /examples/greetings/Hi.java greetings greetings greetings . java -classpath /examples:/lib/Banners.jar greetings.Hi javac JVM 1.7 . javac -source 1.7 -target 1.7 -bootclasspath jdk1.7.0/lib/rt.jar \ -extdirs "" OldCode.java -source 1.7 OldCode.java 1.7 ( 7) . -target 1.7 JVM 1.7 . -target -source -target . -bootclasspath ( rt.jar) . : javac -source 1.7 OldCode.java warning: [options] bootstrap class path not set in conjunction with -source 1.7 ( 1.7 ) ( Java SE 7) . javac JVM 1.7 . javac -source 1.7 -target 1.7 -bootclasspath jdk1.7.0/lib/rt.jar \ -extdirs "" OldCode.java -source 1.7 OldCode.java 1.7 ( 7) . -target 1.7 JVM 1.7 . -bootclasspath ( rt.jar) . : javac -source 1.7 OldCode.java warning: [options] bootstrap class path not set in conjunction with -source 1.7 . ( Java SE 7) . 1.7 ..SH " (SEE ALSO)" o java(1) o jdb(1) o javah(1) o javadoc(1) o jar(1) o jdb(1) JDK 8 03 March 2015 javac(1)