javac Command
The javac
command compiles a program from a command prompt. It reads a Java source program from a text file and creates a compiled Java class file. The basic form of the javac
command is
javac filename [options]
For example, to compile a program named HelloWorld.java
, use this command:
javac HelloWorld.java
Normally, the javac
command compiles only the file that you specify on the command line, but you can coax javac
into compiling more than one file at a time by using any of the following techniques:
If the Java file you specify on the command line contains a reference to another Java class that’s defined by a java
file in the same folder, the Java compiler automatically compiles that class, too.
You can list more than one filename in the javac
command. The following command compiles three files:
javac TestProgram1.java TestProgram2.java TestProgram3.java
You can use a wildcard to compile all the files in a folder, like this:
javac *.java
If you need to compile a lot of files at the same time but don’t want to use a wildcard (perhaps you want to compile a large number of files but not all the files in a folder), you can create an argument file, which lists the files to compile. In the argument file, you can type as many filenames as you want, using spaces or line breaks to separate them. Here’s an argument file named TestPrograms
that lists three files to compile:
TestProgram1.java
TestProgram2.java
TestProgram3.java
You can compile all the programs in this file by using an @
character, followed by the name of the argument file on the javac
command line, like this:
javac @TestPrograms
The javac
command has a gaggle of options that you can use to influence how it compiles your programs.
Option |
Description |
|
Overrides locations of bootstrap class files. (The bootstrap class files are the classes that implement the Java runtime. You will rarely use this option.) |
|
Specifies where to find user class files. Use this option if your program makes use of class files that you’ve stored in a separate folder. |
|
Same as |
|
Specifies where to place generated class files. |
|
Outputs source locations where deprecated APIs are used. Use this option if you want the compiler to warn you whenever you use API methods that have been deprecated. |
|
Specifies character encoding used by source files. |
|
Overrides location of endorsed standards path. |
|
Overrides locations of installed extensions. |
|
Generates all debugging info. |
|
Generates only some debugging info. |
|
Generates no debugging info. |
|
Prints a synopsis of standard options. |
|
Passes |
|
Generates no warnings. |
|
Provides source compatibility with specified release. |
|
Specifies where to find input source files. |
|
Generates class files for specific virtual machine version. |
|
Outputs messages about what the compiler is doing. |
|
Provides version information. |
|
Prints a synopsis of nonstandard options. |
To use one or more of these options, type the option before or after the source filename. Either of the following commands, for example, compiles the HelloApp.java
file with the -verbose
and -deprecation
options enabled:
javac HelloWorld.java –verbose –deprecation
javac –verbose –deprecation HelloWorld.java