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:

check.png 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.

check.png You can list more than one filename in the javac command. The following command compiles three files:

javac TestProgram1.java TestProgram2.java TestProgram3.java

check.png You can use a wildcard to compile all the files in a folder, like this:

javac *.java

check.png 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

-bootclasspath <path>

Overrides locations of bootstrap class files. (The bootstrap class files are the classes that implement the Java runtime. You will rarely use this option.)

-classpath <path>

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.

-cp <path>

Same as classpath.

-d <directory>

Specifies where to place generated class files.

-deprecation

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.

-encoding <encoding>

Specifies character encoding used by source files.

-endorseddirs <dirs>

Overrides location of endorsed standards path.

-extdirs <dirs>

Overrides locations of installed extensions.

-g

Generates all debugging info.

-g:{lines,vars,source}

Generates only some debugging info.

-g:none

Generates no debugging info.

-help

Prints a synopsis of standard options.

-J<flag>

Passes <flag> directly to the runtime system.

-nowarn

Generates no warnings.

-source <release>

Provides source compatibility with specified release.

-sourcepath <path>

Specifies where to find input source files.

-target <release>

Generates class files for specific virtual machine version.

-verbose

Outputs messages about what the compiler is doing.

-version

Provides version information.

-X

Prints a synopsis of nonstandard options.

tip.eps A class file is a compiled Java program that can be executed by the java command. The Java compiler reads source files and creates class files.

tip.eps A deprecated API is a feature that is considered obsolete.

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

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset