javac Compiler

javac, the Java compiler, converts Java source code into one or more class files of bytecode that can be run by a Java interpreter. Java source code is stored in a file with the .java file extension. This file can be created with any text editor or word processor that can save a document without any special formatting codes. The terminology varies depending on the text-editingsoftware being used, but these files are often called plain text, ASCII text, DOS text, or something similar. A Java source code file can contain more than one class, but only one of the classes can be declared to be public. A class can contain no public classes at all if desired, although this isn't possible with applets because of the rules of inheritance. If a source code file contains a class that has been declared to be public, the name of the file must match the name of that class. For example, the source code for a public class called BuyItem must be stored in a file called To compile a file, the javac tool is run with the name of the source code file as an argument, as in the following:


You can compile more than one source file by including each separate filename as a command-line argument, such as this command:


You also can use javac, the Java compiler,wildcard characters such as * and ?. Use the following command to compile all .java files in a folder:

javac *.java

When you compile one or more Java source code files, a separate .class file will be created for each Java class that compiles successfully. If you are compiling a program that makes use of assertions, you must use the -source 1.5 option, as in this command:

javac -source 1.5

If the -source option is not used and you try to compile a program that contains assertions, javac displays an error message and won't compile the file. Another useful option when running the compiler is -deprecation, which causes the compiler to describe any deprecated methods that are being employed in a Java program. A deprecated method is one that Oracle has replaced with a better alternative, either in the same class or a different class entirely. Although the deprecated method works, at some point Sun may decide to remove it from the class—the deprecation warning is a strong suggestion to stop using that method as soon as you can. Normally, the compiler will issue a single warning if it finds any deprecated methods in a program. The -deprecation option causes the compiler to list each method that has been deprecated, as in the following command:

javac -deprecation

If you're more concerned with the speed of a Java program than the size of its class files, you can compile its source code with the -O option. This creates class files that have been optimized for faster performance. Methods that are static, final, or private might be compiled inline, a technique that makes the class file larger but causes the methods to be executed more quickly. If you are going to use a debugger to look for bugs in a Java class, compile the source with the -g option to put all debugging information in the class file, including references to line numbers, local variables, and source code. (To keep all this out of a class, compile with the -g:none option.) Normally, the Java compiler doesn't provide a lot of information as it creates class files. In fact, if the source code compiles successfully and no deprecated methods are employed, you won't see any output from the compiler at all. No news is good news in this case. If you want to see more information on what the javac tool is doing as it compiles source code, use the -verbose option. The more verbose compiler will describe the time it takes to complete different functions, the classes that are being loaded, and the overall time required.