The Java Compiler (Javac) is a command line tool that reads java source code files and compiles them into executable Java bytecode classes. The Java source code must be contained in files whose file names end with .java extension.
The Java compiler takes files with this extension and generates executable class files with .class extension. It is possible to define more than one class in a single source file and if it happens the Java compiler will generate exactly one class file for each class defined in the source file. The syntax for the Java Compiler is as follows.
javac [options] [Filename]
The Filename argument specifies the name of the source code file you want to compile. The options argument specifies options related to how the compiler creates the executable Java classes.
Consider an example in which you want to compile Test. java. In order to do so type
at the command prompt.
If there are no errors in the program, you will get a file named Test. class. This file is made up of bytecodes.
Some commonly used options that can be used with javac are as follows.
Classpath <path>: Specifies where to find user class files. It overrides the default or CLASSPATH environment variable if it set. Directories are separated by semi colons. E.g. javac-classpath ;C;\testdir Test.java
If neither CLASSPATH nor -classpath is specified, the user classpath will be the current directory.
-d<directory> It determines the root directory where compile classes are stored. E.g.
javac -d C:\testdir Test.java
On execution, the class file (Test. class) for the classes in the Test. java source file will be saved in the directory testdir. If -d is not specified. javac puts the class file in the same directory as the source file.
-nowarn Disables all compiler warnings messages.
-verbose Causes the compiler and linker to print out messages about what source files are being compiled and what class files are being loaded.
-help Print a synopsis of standard options.
-sourcepath<path> Specifies where to find input source files. As with the user class path. source path entries are separated by semi colons (;) and can be directories (.jar or .zip files)